<dependencies>
<dependency>
<groupId>org.flowablegroupId>
<artifactId>flowable-engineartifactId>
<version>6.6.0version>
dependency>
<dependency>
<groupId>mysqlgroupId>
<artifactId>mysql-connector-javaartifactId>
<version>8.0.20version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-apiartifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>org.slf4jgroupId>
<artifactId>slf4j-log4j12artifactId>
<version>1.7.30version>
dependency>
<dependency>
<groupId>junitgroupId>
<artifactId>junitartifactId>
<version>4.13.2version>
<scope>testscope>
dependency>
<dependency>
<groupId>cn.hutoolgroupId>
<artifactId>hutool-allartifactId>
<version>5.7.22version>
dependency>
dependencies>
resources 目录下 新建一个 holiday-request.bpmn20.xml 文件
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema"
xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"
xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"
xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"
xmlns:flowable="http://flowable.org/bpmn"
typeLanguage="http://www.w3.org/2001/XMLSchema"
expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.flowable.org/processdef">
<process id="holidayRequest" name="Holiday Request" isExecutable="true">
<startEvent id="startEvent"/>
<sequenceFlow sourceRef="startEvent" targetRef="approveTask"/>
<userTask id="approveTask" name="Approve or reject request"/>
<sequenceFlow sourceRef="approveTask" targetRef="decision"/>
<exclusiveGateway id="decision"/>
<sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression">
conditionExpression>
sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression">
conditionExpression>
sequenceFlow>
<serviceTask id="externalSystemCall" name="Enter holidays in external system"
flowable:class="org.flowable.CallExternalSystemDelegate"/>
<sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/>
<userTask id="holidayApprovedTask" name="Holiday approved"/>
<sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/>
<serviceTask id="sendRejectionMail" name="Send out rejection email"
flowable:class="org.flowable.SendRejectionMail"/>
<sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/>
<endEvent id="approveEnd"/>
<endEvent id="rejectEnd"/>
process>
definitions>
@Before
public void init() {
ProcessEngineConfiguration configuration = new StandaloneInMemProcessEngineConfiguration();
configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
configuration.setJdbcUsername("root");
configuration.setJdbcPassword("hwl123456");
configuration.setJdbcUrl("jdbc:mysql://127.0.0.1:3306/flowable?allowMultiQueries=true&useUnicode=true&useSSL=false&characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&autoReconnect=true&nullCatalogMeansCurrent=true");
configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
engine = configuration.buildProcessEngine();
}
@Test
public void testDeploy() {
RepositoryService repositoryService = engine.getRepositoryService();
// 部署流程,部署后会在 act_re_deployment 和 act_ge_bytearray 表中添加记录
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("holiday-request.bpmn20.xml")
// 指定流程名称
.name("请假流程")
.deploy();
System.out.println(JSONUtil.toJsonPrettyStr(deployment));
}
查看 act_re_deployment 表
查看 act_ge_bytearray,记录 xml 文本字节数组,通过 deployment_id 关联,
@Test
public void testQuery() {
RepositoryService repositoryService = engine.getRepositoryService();
Deployment deployment = repositoryService.createDeploymentQuery()
.deploymentId("1")
.singleResult();
System.out.println(deployment.getName());
}
查看相关的 api 有列表查询、分页查询等
@Test
public void testDelete() {
// 如果部署的流程已启动则不能删除
engine.getRepositoryService().deleteDeployment("1");
// 如果流程已启动则相关的任务一并删除
engine.getRepositoryService().deleteDeployment("1", true);
}
修改 holiday-request.bpmn20 文件,添加一个审批人
@Test
public void testStart() {
RuntimeService runtimeService = engine.getRuntimeService();
Map<String, Object> param = new HashMap<>(8);
param.put("employee", "张三");
param.put("days", 3);
param.put("description", "请假回家");
// 启动流程实例
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", param);
// 启动后 act_ru_variable 表中记录参数信息
System.out.println(processInstance.getName());
}
发起流程后流程实例表 act_hi_procinst
流程任务表 act_ru_task,通过 proc_inst_id 和流程实例关联
流程执行过程 act_ru_execution,通过 proc_inst_id 和流程实例关联
提交参数表 act_ru_variable,通过 proc_inst_id 和流程实例关联,通过 execution_id 和执行过程关联
@Test
public void testQueryTask() {
TaskService taskService = engine.getTaskService();
List<Task> list = taskService.createTaskQuery()
// 指定流程类型
.processDefinitionKey("holidayRequest")
// 指定办理人,通常用于查询我的待办
.taskAssignee("zhangsan")
.list();
System.out.println(JSONUtil.toJsonPrettyStr(list));
}
@Test
public void testHandleTask() {
TaskService taskService = engine.getTaskService();
Map<String, Object> param = new HashMap<>();
param.put("approved", false);
// 完成任务
taskService.complete("2509", param);
}
执行后看到控制台输出拒绝邮件
查看流程实例表 act_hi_procinst
有流程持续时间、开始节点、结束节点
查看流程任务表 act_ru_task 、流程执行过程 act_ru_execution 和提交参数表 act_ru_variable 已经被清空
流程审批后,相关的中间数据都已经被清理了。如果查看流程提交记录需要查看历史信息
@Test
public void testGetHistory() {
HistoryService historyService = engine.getHistoryService();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery().processDefinitionId("holidayRequest:1:3")
// 已结束的流程
.finished()
// 根据结束时间倒序排序
.orderByHistoricActivityInstanceEndTime().desc()
.list();
for (HistoricActivityInstance instance: list) {
System.out.println(JSONUtil.toJsonStr(instance));
}
}