Spring Boot 集成 Activiti7(工作流引擎)完整版
介绍
Activiti 是一个工作流引擎,它可以将业务系统中复杂的业务流程抽取出来,使用专门的建模语言 BPMN2.0
进行定义,业务流程按照预先定义的流程进行执行,实现了系统的流程由 Activiti
进行管理,减少业务系统由于流程变更进行系统升级改造的工作量,从而提高系统的健壮性,同时也减少了系统开发维护成本。(salvio修改)
升级新api (ProcessRuntime) 和(TaskRuntime)
官方网站:/
环境搭建
安装插件
File` -> `Settings` -> `Plugins` -> `Activiti BPMN visualizer
引入依赖
<dependencies>
<!-- 引入Activiti7 -->
<dependency>
<groupId></groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M4</version>
<exclusions>
<exclusion>
<groupId></groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId></groupId>
<artifactId>activiti-dependencies</artifactId>
<version>7.1.0.M4</version>
<type>pom</type>
</dependency>
<!-- 生成流程图 -->
<dependency>
<groupId></groupId>
<artifactId>activiti-image-generator</artifactId>
<version>7.1.0.M4</version>
</dependency>
</dependencies>
1234567891011121314151617181920212223242526
配置文件
spring:
# 数据源配置
datasource:
driver-class-name:
url: jdbc:mysql://127.0.0.1:3306/activiti?useUnicode=true&useSSL=false&serverTimezone=UTC&characterEncoding=UTF8&nullCatalogMeansCurrent=true
username: "root"
password: "88888888"
type:
hikari:
# 等待连接池分配连接的最大时间(毫秒),超过这个时长还没有可用的连接,则会抛出SQLException
connection-timeout: 30000
# 最小连接数
minimum-idle: 5
# 最大连接数
maximum-pool-size: 20
# 自动提交
auto-commit: true
# 连接超时的最大时长(毫秒),超时则会被释放(retired)
idle-timeout: 600000
# 连接池的名字
pool-name: DataSourceHikariCP
# 连接池的最大生命时长(毫秒),超时则会被释放(retired)
max-lifetime: 18000000
# activiti7配置
activiti:
# 自动部署验证设置:true-开启(默认)、false-关闭
check-process-definitions: false
# 保存历史数据
history-level: full
# 检测历史表是否存在
db-history-used: true
# 关闭自动部署
deployment-mode: never-fail
# 对数据库中所有表进行更新操作,如果表不存在,则自动创建
# create_drop:启动时创建表,在关闭时删除表(必须手动关闭引擎,才能删除表)
# drop-create:启动时删除原来的旧表,然后在创建新表(不需要手动关闭引擎)
database-schema-update: true
# 解决频繁查询SQL问题
async-executor-activate: false
123456789101112131415161718192021222324252627282930313233343536373839
初始化数据表
修改配置文件后,进行项目启动,项目启动成功会自动生成 25
张数据表,如下所示:
注意:7.1.0.M4 版本自动生成的表字段不全,所以还需运行如下SQL:
-- ----------------------------
-- 修复Activiti7的M4版本缺失字段Bug
-- ----------------------------
alter table ACT_RE_DEPLOYMENT add column PROJECT_RELEASE_VERSION_ varchar(255) DEFAULT NULL;
alter table ACT_RE_DEPLOYMENT add column VERSION_ varchar(255) DEFAULT NULL;
12345
数据表解读
Activiti 使用到的表都是 ACT_
开头的,表名的第二部分表示用途。
- ACT_GE_ (
GE
) 表示 general 全局通用数据及设置,各种情况都使用的数据。 - ACT_HI_ (
HI
) 表示 history 历史数据表,包含着程执行的历史相关数据。 - ACT_RE_ (
RE
) 表示 repository 存储,包含的是静态信息。 - ACT_RU_ (
RU
) 表示 runtime 运行时,运行时的流程变量,用户任务,变量,职责(job)等运行时数据。Activiti 只存储实例执行期间的运行时数据,当流程实例结束时,将删除这些记录 (salvio修改)
全局通用数据(ACT_GE_*)
表名 | 解释 |
---|---|
ACT_GE_BYTEARRAY | 二进制数据表,存储通用的流程定义和流程资源。 |
ACT_GE_PROPERTY | 系统相关属性,属性数据表存储整个流程引擎级别的数据。 |
历史数据表(ACT_HI_*)
表名 | 解释 |
---|---|
ACT_HI_ACTINST | 历史节点表 |
ACT_HI_ATTACHMENT | 历史附件表 |
ACT_HI_COMMENT | 历史意见表 |
ACT_HI_DETAIL | 历史详情表,提供历史变量的查询 |
ACT_HI_IDENTITYLINK | 历史流程人员表 |
ACT_HI_PROCINST | 历史流程实例表 |
ACT_HI_TASKINST | 历史任务实例表 |
ACT_HI_VARINST | 历史变量表 |
静态信息表(ACT_RE_*)
表名 | 解释 |
---|---|
ACT_RE_DEPLOYMENT | 部署信息表 |
ACT_RE_MODEL | 流程设计模型部署表 |
ACT_RE_PROCDEF | 流程定义数据表 |
运行数据(ACT_RU_*)
表名 | 解释 |
---|---|
ACT_RU_DEADLETTER_JOB | 无法执行工作表: 如果一个任务执行了很多次,都无法执行,那么这个任务会写到 |
ACT_RU_EVENT_SUBSCR | 运行时事件 throwEvent、catchEvent 时间监听信息表 |
ACT_RU_EXECUTION | 运行时流程执行实例 |
ACT_RU_IDENTITYLINK | 运行时流程人员表,主要存储任务节点与参与者的相关信息 |
ACT_RU_INTEGRATION | 运行时积分表 |
ACT_RU_JOB | 运行时定时任务数据表 |
ACT_RU_SUSPENDED_JOB | 暂停的工作,流程中有一个定时任务,如果把这个任务停止工作了,这个任务会在act_ru_suspended_job中写入数据 |
ACT_RU_TASK | 运行时任务节点表 |
ACT_RU_TIMER_JOB | 运行时定时器作业表 |
ACT_RU_VARIABLE | 运行时流程变量数据表 |
其它表
表名 | 解释 |
---|---|
ACT_EVT_LOG | 事件日志 |
ACT_PROCDEF_INFO | 流程定义的动态变更信息 |
快速入门
流程部署
流程部署方式分为两种:自动部署与手动部署
自动部署:将 bpmn
文件放入 resources
资源目录下的 processes
文件夹中,项目启动时则进行自动部署。(salvio修改)
spring:
# activiti7配置
activiti:
# 自动部署验证设置:true-开启(默认)、false-关闭
check-process-definitions: true
# 保存历史数据
history-level: full
# 检测历史表是否存在
db-history-used: true
# 关闭SpringAutoDeployment
deployment-mode: never-fail
# 对数据库中所有表进行更新操作,如果表不存在,则自动创建
database-schema-update: true
# 解决频繁查询SQL问题
async-executor-activate: false
123456789101112131415
手动部署:新建测试类,并编写流程部署测试方法。
自定流程
- 创建 *. 文件:New -> New Activity BPMN file
- 流程设计页面:选中
*.
文件,右键view BPMN(Activity ) Diagram
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="/spec/BPMN/20100524/MODEL" xmlns:xsi="http:///2001/XMLSchema-instance" xmlns:xsd="http:///2001/XMLSchema" xmlns:activiti="/bpmn" xmlns:bpmndi="/spec/BPMN/20100524/DI" xmlns:omgdc="/spec/DD/20100524/DC" xmlns:omgdi="/spec/DD/20100524/DI" typeLanguage="http:///2001/XMLSchema" expressionLanguage="http:///1999/XPath" targetNamespace="/processdef">
<process id="Part1_Deployment" name="Part1_Deployment" isExecutable="true">
<startEvent id="sid-26d6e725-878e-411d-b23d-6df4ae3cbd24"/>
<endEvent id="sid-03d64ced-963c-4506-a5b3-21b8a9dc4279"/>
<userTask id="sid-192966db-031d-4a50-886d-379935dd9bc1" name="UserTask" activiti:assignee="admin"/>
<sequenceFlow id="sid-560ce1e7-a0f9-4a99-8a64-d2920292cd37" sourceRef="sid-26d6e725-878e-411d-b23d-6df4ae3cbd24" targetRef="sid-192966db-031d-4a50-886d-379935dd9bc1"/>
<sequenceFlow id="sid-da2d1cc1-7544-40f6-81ab-1d0c32c0064a" sourceRef="sid-192966db-031d-4a50-886d-379935dd9bc1" targetRef="sid-03d64ced-963c-4506-a5b3-21b8a9dc4279"/>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_Part1_Deployment">
<bpmndi:BPMNPlane bpmnElement="Part1_Deployment" id="BPMNPlane_Part1_Deployment">
<bpmndi:BPMNShape id="shape-6b3bc939-03fe-47d7-b1fb-44a959045633" bpmnElement="sid-26d6e725-878e-411d-b23d-6df4ae3cbd24">
<omgdc:Bounds x="-20.0" y="-85.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-8137d303-1a5b-4a34-b8d7-995a29ae534a" bpmnElement="sid-03d64ced-963c-4506-a5b3-21b8a9dc4279">
<omgdc:Bounds x="-20.0" y="105.00001" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-40199472-3308-4677-9a38-067b57a4a17b" bpmnElement="sid-192966db-031d-4a50-886d-379935dd9bc1">
<omgdc:Bounds x="-55.0" y="-15.0" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-05926a76-90de-4b3f-8fd5-d27d06dd6d6e" bpmnElement="sid-560ce1e7-a0f9-4a99-8a64-d2920292cd37">
<omgdi:waypoint x="-5.0" y="-55.0"/>
<omgdi:waypoint x="-5.0" y="-15.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-6b52ec5d-0f90-4972-a304-022f03ccf3cf" bpmnElement="sid-da2d1cc1-7544-40f6-81ab-1d0c32c0064a">
<omgdi:waypoint x="-5.0" y="65.0"/>
<omgdi:waypoint x="-5.0" y="105.00001"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
12345678910111213141516171819202122232425262728293031
单元测试
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.Deployment;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import java.io.InputStream;
import java.util.List;
import java.util.zip.ZipInputStream;
@SpringBootTest
public class DeploymentTest {
@Autowired
private RepositoryService repositoryService;
/**
* 流程部署
*/
@Test
public void initDeployment() {
String fileName = "bpmn/Part1_Deployment.";
Deployment deployment = this.repositoryService.createDeployment()
.addClasspathResource(fileName)
.name("流程部署测试")
.deploy();
System.out.println("流程部署名称:" + deployment.getName());
}
/**
* 流程部署(Zip包)
*/
@Test
public void initDeploymentByZip() {
InputStream inputStream = this.getClass()
.getClassLoader()
.getResourceAsStream("bpmn/Part1_Deployment.zip");
assert inputStream != null;
ZipInputStream zipInputStream = new ZipInputStream(inputStream);
Deployment deployment = this.repositoryService.createDeployment()
.addZipInputStream(zipInputStream)
.name("流程部署测试(Zip包)")
.deploy();
System.out.println("流部署名称:" + deployment.getName());
}
/**
* 查询流程部署列表
*/
@Test
public void listDeployments() {
List<Deployment> deployments = this.repositoryService.createDeploymentQuery().list();
if (!deployments.isEmpty()) {
deployments.forEach(deployment -> {
System.out.println("Id:" + deployment.getId());
System.out.println("Name:" + deployment.getName());
System.out.println("DeploymentTime:" + deployment.getDeploymentTime());
System.out.println("Key:" + deployment.getKey());
});
}
}
/**
* 删除对流程实例、历史流程实例和作业的给定部署和级联删除
*/
@Test
public void deleteDeployment() {
String deploymentId = "4c97f9ce-4774-11ed-930a-e4a8dfd43d4a";
this.repositoryService.deleteDeployment(deploymentId, false);
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
涉及数据表:act_re_deployment、act_re_procdef、act_ge_bytearray
act_re_deployment 和 act_re_procdef 属于一对多关系,即:一次部署可以部署多个流程定义,而只会在 act_re_deployment 生成一条记录(salvio修改)
流程定义
import org.activiti.engine.RepositoryService;
import org.activiti.engine.repository.ProcessDefinition;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import java.util.List;
@SpringBootTest
public class ProcessDefinitionTest {
@Autowired
private RepositoryService repositoryService;
/**
* 查询流程定义列表
*/
@Test
public void listProcessDefinitions() {
List<ProcessDefinition> processDefinitions = this.repositoryService.createProcessDefinitionQuery()
.list();
if (!CollectionUtils.isEmpty(processDefinitions)) {
processDefinitions.forEach(processDefinition -> {
System.out.println("Name:" + processDefinition.getName());
System.out.println("Key:" + processDefinition.getKey());
System.out.println("ResourceName:" + processDefinition.getResourceName());
System.out.println("DeploymentId:" + processDefinition.getDeploymentId());
System.out.println("Version:" + processDefinition.getVersion());
});
}
}
}
123456789101112131415161718192021222324252627282930313233
流程实例
注意:流程定义(ProcessDefinition)与流程实例(ProcessInstance)是一对多关系(salvio修改)
import org.activiti.engine.RuntimeService;
import org.activiti.engine.runtime.ProcessInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class ProcessInstanceTest {
@Autowired
private RuntimeService runtimeService;
/**
* 初始化流程实例
*/
@Test
public void initProcessInstance() {
// 流程定义KEY
String processDefinitionKey = "Part1_Deployment";
// 业务表KEY(用于把业务数据与Activiti7流程数据相关联)
String businessKey = "4208169753200945";
// 参数
Map<String, Object> variables = new HashMap<>(16);
ProcessInstance processInstance = this.runtimeService
.startProcessInstanceByKey(processDefinitionKey, businessKey, variables);
System.out.println("流程实例ID:" + processInstance.getProcessInstanceId());
}
/**
* 查询流程实例
*/
@Test
public void getProcessInstance() {
String processInstanceId = "354709ac-477f-11ed-abfa-e4a8dfd43d4a";
ProcessInstance processInstance = this.runtimeService.createProcessInstanceQuery()
.processInstanceId(processInstanceId)
.singleResult();
System.out.println("ProcessInstanceId:" + processInstance.getProcessInstanceId());
System.out.println("ProcessDefinitionId:" + processInstance.getProcessDefinitionId());
System.out.println("isEnded:" + processInstance.isEnded());
System.out.println("isSuspended:" + processInstance.isSuspended());
}
/**
* 查询流程实例列表
*/
@Test
public void listProcessInstances() {
List<ProcessInstance> processInstanceList = this.runtimeService.createProcessInstanceQuery().list();
if (!CollectionUtils.isEmpty(processInstanceList)) {
processInstanceList.forEach(processInstance -> {
System.out.println("ProcessInstanceId:" + processInstance.getProcessInstanceId());
System.out.println("ProcessDefinitionId:" + processInstance.getProcessDefinitionId());
System.out.println("isEnded:" + processInstance.isEnded());
System.out.println("isSuspended:" + processInstance.isSuspended());
});
}
}
/**
* 挂起流程实例
*/
@Test
public void suspendProcessInstance() {
String processInstanceId = "354709ac-477f-11ed-abfa-e4a8dfd43d4a";
this.runtimeService.suspendProcessInstanceById(processInstanceId);
}
/**
* 激活流程实例
*/
@Test
public void activeProcessInstance() {
String processInstanceId = "354709ac-477f-11ed-abfa-e4a8dfd43d4a";
this.runtimeService.activateProcessInstanceById(processInstanceId);
}
/**
* 删除流程实例
*/
@Test
public void deleteProcessInstance() {
String processInstanceId = "354709ac-477f-11ed-abfa-e4a8dfd43d4a";
String reason = "测试删除流程实例";
this.runtimeService.deleteProcessInstance(processInstanceId, reason);
}
}
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
涉及数据表:act_hi_actinst、act_hi_taskinst、act_hi_identitylink、act_hi_procinst、act_ru_execution、act_ru_identitylink、act_ru_task(salvio修改)
任务管理
- Assignee:执行人/代理人
- Candidate Users:候选人
- Candidate Groups:候选组
- Due Date:任务到期时间
import org.activiti.engine.TaskService;
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 org.springframework.util.CollectionUtils;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
@SpringBootTest
public class TaskTest {
@Autowired
private TaskService taskService;
/**
* 查询任务列表
*/
@Test
public void listTasks() {
List<Task> taskList = this.taskService.createTaskQuery().list();
if (!CollectionUtils.isEmpty(taskList)) {
taskList.forEach(task -> {
System.out.println("Id:" + task.getId());
System.out.println("Name:" + task.getName());
System.out.println("Assignee:" + task.getAssignee());
});
}
}
/**
* 查询我的代办任务
*/
@Test
public void listTasksByAssignee() {
String assignee = "admin";
List<Task> taskList = this.taskService.createTaskQuery()
.taskAssignee(assignee)
.list();
if (!CollectionUtils.isEmpty(taskList)) {
taskList.forEach(task -> {
System.out.println("Id:" + task.getId());
System.out.println("Name:" + task.getName());
System.out.println("Assignee:" + task.getAssignee());
});
}
}
/**
* 完成任务
*/
@Test
public void completeTask() {
String taskId = "354b9d90-477f-11ed-abfa-e4a8dfd43d4a";
Map<String, Object> variables = new HashMap<>(16);
this.taskService.complete(taskId, variables);
}
/**
* 拾取任务
*/
@Test
public void claimTask() {
String taskId = "16beabc1-479f-11ed-9c3a-e4a8dfd43d4a";
String userId = "jason";
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
taskService.claim(taskId, userId);
}
/**
* 归还任务
*/
@Test
public void returnTask() {
String taskId = "16beabc1-479f-11ed-9c3a-e4a8dfd43d4a";
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
// 归还任务
taskService.unclaim(taskId);
}
/**
* 交办任务
*/
@Test
public void handoverTask() {
String taskId = "16beabc1-479f-11ed-9c3a-e4a8dfd43d4a";
String userId = "jack";
Task task = this.taskService.createTaskQuery().taskId(taskId).singleResult();
// 交办任务
taskService.setAssignee(taskId, userId);
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
涉及数据表:act_hi_actinst、act_hi_taskinst、act_hi_identitylink、act_hi_procinst、act_ru_execution、act_ru_identitylink、act_ru_task(salvio修改)
历史记录
import org.activiti.engine.HistoryService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.util.CollectionUtils;
import java.util.List;
@SpringBootTest
public class HistoricTest {
@Autowired
private HistoryService historyService;
/**
* 根据用户名查询历史记录
*/
@Test
public void listHistoricTasksByAssignee() {
String assignee = "admin";
List<HistoricTaskInstance> historicTasks = this.historyService.createHistoricTaskInstanceQuery()
.orderByHistoricTaskInstanceEndTime()
.asc()
.taskAssignee(assignee)
.list();
if (!CollectionUtils.isEmpty(historicTasks)) {
historicTasks.forEach(historicTaskInstance -> {
System.out.println("Id:" + historicTaskInstance.getId());
System.out.println("ProcessInstanceId:" + historicTaskInstance.getProcessInstanceId());
System.out.println("Name:" + historicTaskInstance.getName());
});
}
}
/**
* 根据流程实例ID查询历史
*/
@Test
public void listHistoricTasksByProcessInstanceId() {
String processInstanceId = "0f8a9b00-479e-11ed-af85-e4a8dfd43d4a";
List<HistoricTaskInstance> historicTasks = this.historyService.createHistoricTaskInstanceQuery()
.orderByHistoricTaskInstanceEndTime()
.asc()
.processInstanceId(processInstanceId)
.list();
if (!CollectionUtils.isEmpty(historicTasks)) {
historicTasks.forEach(historicTaskInstance -> {
System.out.println("Id:" + historicTaskInstance.getId());
System.out.println("ProcessInstanceId:" + historicTaskInstance.getProcessInstanceId());
System.out.println("Name:" + historicTaskInstance.getName());
});
}
}
}
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
UEL表达式
- 表达式以
${
开始;}
结束,例如:${day > 100} - 支持逻辑运算:${username == “admin” and password == “123456”}
- 支持变量(变量名必须小写)与实体类赋值(salvio修改)
涉及数据表:act_hi_varinst、act_ru_variable
BPMN2.0网关
并行网关(Parallel gateway)
注意:并行网关有开始就要有结束
排他网关(Exclusive gateway)
包容网关(Inclusive gateway)
注意:包容网关是并行网关的一种衍生,所以也是成对出现