Article directory
Activiti project construction
Reference dependencies
<activiti.version>7.0.0.Beta1</activiti.version>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-engine</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 模型处理 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-model</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn json数据转换 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-json-converter</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- bpmn 布局 -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-bpmn-layout</artifactId>
<version>${activiti.version}</version>
</dependency>
<!-- activiti 云支持 -->
<dependency>
<groupId>org.activiti.cloud</groupId>
<artifactId>activiti-cloud-services-api</artifactId>
<version>${activiti.version}</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>commons-dbcp</groupId>
<artifactId>commons-dbcp</artifactId>
<version>1.4</version>
</dependency>
Activiti service initialization
@Configuration
public class ActivitiConfiguration {
@Autowired
@Qualifier("dataSource")
private BasicDataSource DataSource;
@Autowired
private PlatformTransactionManager platformTransactionManager;
/**
* 注册 activiti的配置信息
*/
@Bean("processEngineConfiguration")
public SpringProcessEngineConfiguration getProcessEngineConfiguration(){
SpringProcessEngineConfiguration configuration
= new SpringProcessEngineConfiguration();
configuration.setDataSource(DataSource); // 添加数据源
configuration.setTransactionManager(platformTransactionManager); // 添加事务
configuration.setDatabaseSchemaUpdate("true"); // 如果有表则不创建
configuration.setDbHistoryUsed(true); // 允许查看历史信息
configuration.buildProcessEngine(); //执行创建25张表,如果已经创建就不再创建了
// 自动部署 bpmn文件
Resource[] resources = null;
try {
resources= new PathMatchingResourcePatternResolver().getResources("classpath*:bpmn/*.bpmn20.xml");
} catch (IOException e) {
e.printStackTrace();
}
configuration.setDeploymentResources(resources);
return configuration;
}
/**
* 注册 ProcessEngineFactoryBean
*/
@Bean
public ProcessEngineFactoryBean processEngine(){
ProcessEngineFactoryBean factoryBean = new ProcessEngineFactoryBean();
factoryBean.setProcessEngineConfiguration(getProcessEngineConfiguration());
return factoryBean;
}
/**
* 注册 RepositoryService
*/
@Bean
public RepositoryService repositoryService() throws Exception{
return processEngine().getObject().getRepositoryService();
}
/**
* 注册 RuntimeService
*/
@Bean
public RuntimeService runtimeService() throws Exception{
return processEngine().getObject().getRuntimeService();
}
/**
* 注册 TaskService
*/
@Bean
public TaskService taskService() throws Exception{
return processEngine().getObject().getTaskService();
}
/**
* 注册 HistoryService
*/
@Bean
public HistoryService historyService() throws Exception{
return processEngine().getObject().getHistoryService();
}
}
Database configuration
@Configuration
public class DataSourceConfiguration {
@Bean("dataSource")
public BasicDataSource getDruidDataSource(
@Value("${spring.datasource.driver-class-name}")
String driverClassName, // 数据库驱动程序
@Value("${spring.datasource.url}")
String url, // 数据库连接地址
@Value("${spring.datasource.username}")
String username, // 数据库的用户名
@Value("${spring.datasource.password}")
String password, // 数据库的用户名
@Value("${spring.datasource.tomcat.min-idle}")
int minIdle, // 最小维持连接数
@Value("${spring.datasource.tomcat.max-active}")
int maxActive // 最大连接数
) {
BasicDataSource dataSource = new BasicDataSource(); // 实例化DataSource子类对象
dataSource.setDriverClassName(driverClassName); // 数据库驱动程序
dataSource.setUrl(url); // 数据库的连接地址
dataSource.setUsername(username); // 数据库用户名
dataSource.setPassword(password); // 数据库密码
dataSource.setMinIdle(minIdle); // 最小维持的连接数量
dataSource.setMaxActive(maxActive); // 最大的连接数量
return dataSource;
}
}
Configuration file
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
url: jdbc:mysql:///activiti_dev?nullCatalogMeansCurrent=true
username: root
password: 123456
tomcat:
max-active: 3
min-idle: 1
test demo
This is a test case created using springboot project
import org.activiti.engine.RepositoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.impl.persistence.entity.HistoricFormPropertyEntity;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.util.List;
@SpringBootTest
class BpmDemoApplicationTests {
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private RepositoryService repositoryService;
@Test
void contextLoads() {
}
/**
* @description: 创建审批
* @author: gepengjun
* @date: 2023/9/3 17:31
* @param: []
* @return: void
**/
@Test
public void testCreateProcessInstance(){
ProcessInstance test1 = runtimeService.startProcessInstanceByKey("test1");
System.out.println("流程示例id:"+test1.getId());
}
/**
* @description: 完成任务
* @author: gepengjun
* @date: 2023/9/3 17:31
* @param:
* @return: void
**/
@Test
public void testComplete(){
taskService.setVariable("37505","variable",true);
taskService.complete("37505");
}
/**
* @description: 设置流程实例变量
* @author: gepengjun
* @date: 2023/9/3 18:34
* @param: []
* @return: void
**/
@Test
public void testProcessInstanceVariable(){
runtimeService.setVariable("12506","taskTest","任务完成没有");
}
/**
* @description: 获取流程实例变量
* @author: gepengjun
* @date: 2023/9/3 18:34
* @param: []
* @return: void
**/
@Test
public void testGetProcessInstanceVariable(){
Object taskTest = runtimeService.getVariable("30001", "taskTest");
System.out.println("这是流程实例变量:"+taskTest.toString());
}
/**
* @description: 在使用task服务获取流程实例变量
* @author: gepengjun
* @date: 2023/9/3 18:34
* @param: []
* @return: void
**/
@Test
public void testTaskGetProcessInstanceVariable(){
Object taskTest = taskService.getVariable("30005", "taskTest");
System.out.println("使用Task服务获取流程实例变量:"+taskTest.toString());
}
/**
* @description: 查询对应节点任务
* @author: gepengjun
* @date: 2023/9/3 17:31
* @param: []
* @return: void
**/
@Test
public void testByUserName(){
//根据节点名称获取指定流程实例的任务
//List<Task> tasks = taskService.createTaskQuery().processInstanceId("37501").taskName("科长").list();
//获取所有节点名称为“科长”的任务
List<Task> tasks = taskService.createTaskQuery().taskName("高经理").list();
for (Task task : tasks) {
System.out.println("任务名称:"+task.getName());
System.out.println("任务ID:"+task.getId());
System.out.println("_______________________________");
}
}
/**
* @description: 删除流程模板
* @author: gepengjun
* @date: 2023/9/3 18:35
* @param: []
* @return: void
**/
@Test
public void testRepositoryService(){
repositoryService.deleteDeployment("1");
}
}
Activiti function service
RepositoryService:
RepositoryService is a service interface in Activiti, used to manage process definition and deployment operations. Through RepositoryService, you can perform the following common operations:
-
Deploy process definition: Use
createDeployment()
the method to create a new deployment object, and add the resource file of the process definition throughaddInputStream()
,addClasspathResource()
and other methods, and finally usedeploy()
the method to deploy the process definition to the engine for use. -
Query process definition: Use
createProcessDefinitionQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on the name, version number, etc. of the process definition. -
Obtain process definition resources: Use
getResourceAsStream()
the method to obtain process definition resource files, such as obtaining flow charts, process definition XML files, etc. -
Deleting a process definition: Use
deleteDeployment()
the method to delete a deployed process definition and its associated resources.
RuntimeService:
RuntimeService is a service interface in Activiti, used to manage the operations of process instances. Through RuntimeService, you can perform the following common operations:
-
Start a process instance: Use
startProcessInstanceByKey()
the method to start a new process instance based on the key of the process definition, or usestartProcessInstanceById()
the method to start a process instance based on the ID of the process definition. -
Query process instances: Use
createProcessInstanceQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on the ID of the process instance, the Key of the process definition, etc. -
Pause and resume process instances: Use
suspendProcessInstanceById()
the method to pause the process instance with the specified ID, and useactivateProcessInstanceById()
the method to resume the paused process instance. -
Query task: Use
createTaskQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on the ID of the task, the person in charge of the task, etc. -
Set and get process instance variables: Use
setVariable()
the method to set the variables of the process instance, and usegetVariable()
the method to get the variables of the process instance.
TaskService:
TaskService is a service interface in Activiti, used to manage task operations. Through TaskService, you can perform the following common task-related operations:
-
Query task: Use
createTaskQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on the task ID, person in charge, and the process instance to which it belongs. -
Complete the task: Use
complete()
the method to complete the task with the specified ID. You can also set the executor, variables and other information of the task at the same time. -
Assign tasks: Use
setAssignee()
the method to assign tasks to specified users or groups. -
Delegate tasks: Use
delegateTask()
the method to delegate tasks to others. -
Sign the task: Use
claim()
the method to sign the task and set the current user as the person in charge of the task. -
Set and get task variables: Use
setVariable()
the method to set the variables of the task, and usegetVariable()
the method to get the variables of the task. -
Create attachments and comments: Use
createAttachment()
the method to create attachments to the task, and useaddComment()
the method to add comments to the task.
HistoryService:
HistoryService is a service interface in Activiti, used to manage historical data operations. Through HistoryService, you can perform the following common historical data-related operations:
-
Query historical process instances: Use
createHistoricProcessInstanceQuery()
the method to create a query object, and then filter and sort according to different conditions, such as querying based on the ID of the process instance, the Key of the process definition, etc. -
Query historical tasks: Use
createHistoricTaskInstanceQuery()
the method to create a query object, and then filter and sort according to different conditions, such as querying based on the task ID, the process instance to which it belongs, the person in charge, etc. -
Query historical activities: Use
createHistoricActivityInstanceQuery()
the method to create a query object, and then filter and sort according to different conditions, such as querying based on the ID of the activity, the process instance to which it belongs, the activity type, etc. -
Query historical variables: Use
createHistoricVariableInstanceQuery()
the method to create a query object, and then filter and sort according to different conditions, such as querying based on the name of the variable, the process instance it belongs to, etc. -
Query historical form data: Use
createHistoricFormDataQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on the ID of the form, the process instance to which it belongs, etc.
ManagementService:
ManagementService is a service interface in Activiti, used for management engine-related operations. ManagementService lets you perform some common engine management operations:
-
Execution jobs: Use
executeJob()
the method to manually trigger execution jobs, such as scheduled tasks, asynchronous tasks, etc. -
Pause and resume jobs: Use
suspendJobById()
the method to pause the job with the specified ID, and useactivateJobById()
the method to resume the suspended job. -
Query jobs: Use
createJobQuery()
the method to create a query object, which can then be filtered and sorted according to different conditions, such as querying based on job ID, job status, job type, etc. -
Query engine table: Use
getTableName()
the method to get the database table name corresponding to the specified entity class, and usegetTableCount()
the method to get the number of records in the specified table. -
Force database operations: Use
executeCommand()
the method to execute customized database operation commands, such as SQL queries, SQL updates, etc. -
Obtain engine configuration information: Use
getProcessEngineConfiguration()
the method to obtain the current engine configuration information, including database connection information, process definition parser, etc.
DynamicBpmnService:
DynamicBpmnService is a service interface in Activiti, used to dynamically modify process definitions and related content of process instances. With DynamicBpmnService, you can perform some common dynamic process operations:
-
Dynamically modify the process definition: Use
changeDeploymentProcessDefinitionKey()
the method to modify the key of the process definition under the specified deployment ID. -
Dynamically modify the process instance: use
moveActivityIdTo()
the method to move the current active node to the specified target node. -
Dynamically add user task nodes: Use
addUserTask()
the method to dynamically add user task nodes in the process definition. -
Dynamically delete a node: Use
deleteActivity()
the method to delete the specified node and all its associated circulation information. -
Dynamically set node attributes: Use
setActivityProperties()
the method to set the attributes of the specified node, such as node name, node description, etc. -
Dynamically set process variables: Use
setProcessInstanceVariable()
the method to set the variable value of the process instance.
Activiti table classification
In Activiti, tables are used to store data and metadata related to processes. Activiti logically divides it into five categories.
- ACT_RE_*: Contains "RE" to indicate Repository. These tables store all data and metadata related to Activiti process definitions and process resources. For example, these tables include:
MN process
- defined data.
- ACT_RE_DEPLOYMENT: Save data of resource files such as BPMN files and flow charts.
-
ACT_RU_*: Contains "RU" for Runtime. These tables store runtime data generated during the running of the process. For example, these tables include:
- ACT_RU_TASK: Save the data of the currently pending tasks.
- ACT_RU_EXECUTION: Save process instances and execution data.
-
ACT_ID_*: Contains "ID" to represent Identity. These tables store authentication-related data such as users, groups, and permissions. For example, these tables include:
- ACT_ID_USER: Save user data.
- ACT_ID_GROUP: Save user group data.
-
ACT_HI_*: Contains "HI" to indicate History. These tables store the history and statistical data of process instances. For example, these tables include:
- ACT_HI_PROCINST: Save historical data of process instances.
- ACT_HI_TASKINST: Save historical data of task instances.
-
ACT_GE_*: Contains "GE" for Generic, these tables store various configuration data used by Activiti applications and general tools. For example, these tables include:
- ACT_GE_PROPERTY: Saves Activiti data and configured property data.
- ACT_GE_PROPERTY: Saves Activiti data and configured property data.