本文参考博客:
https://zhuanlan.zhihu.com/p/457996810
https://www.51cto.com/article/708150.html
简介
Flowable 是一个使用 Java 编写的轻量级业务流程引擎。Flowable 流程引擎可用于部署 BPMN2.0 流程定义(用于定义流程的行业 XML 标准),创建这些流程定义的流程实例,进行查询,访问运行中或历史的流程实例与相关数据等等。
SpringBoot 搭建
引入依赖
导入boot支撑的flowable依赖
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.7.2</version>
</dependency>
安装插件
安装流程可视化的工具
新建BPMN文件
- 在 resources 目录下新建 processes 目录,这个目录下的流程文件将来会被自动部署
- 在 processes 目录下,新建一个 BPMN 文件(插件装好了就有这个选项了)
文件解析
流程图如下
根据流程图解析的xml文件:
- :表示一个完整的工作流程。
- :工作流中起点位置(圆形)。
- :工作流中结束位置(圆形)。
- :代表一个任务节点(矩形)。
- :逻辑判断节点(菱形)。
- :链接各个节点的线条。
- :这是服务任务,在具体的实现中,这个任务可以做任何事情。
<?xml version="1.0" encoding="UTF-8"?>
<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:flowable="http://flowable.org/bpmn"
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"
typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath"
targetNamespace="http://www.flowable.org/processdef">
<process id="leave_approval" name="请假审批" isExecutable="true">
<!--startEvent endEvent开始和结束节点(圆形)-->
<!--userTask 工作节点(矩形)-->
<!--exclusiveGateway 判断节点(菱形)-->
<!--flowable:candidateGroups候选操作组;flowable:assignee候选操作人-->
<startEvent id="start" name="开始" flowable:initiator="startuser" flowable:formFieldValidation="true"></startEvent>
<userTask id="stu_task" name="学生" flowable:candidateGroups="stu_group" flowable:formFieldValidation="true"></userTask>
<userTask id="te_task" name="老师" flowable:candidateGroups="te_group" flowable:formFieldValidation="true"></userTask>
<exclusiveGateway id="getway1" name="网关1"></exclusiveGateway>
<userTask id="mte_task" name="校长" flowable:candidateGroups="mte_group" flowable:formFieldValidation="true"></userTask>
<exclusiveGateway id="getway2" name="网关2"></exclusiveGateway>
<endEvent id="end" name="结束"></endEvent>
<!--流线:sourceRef起始位置 targetRef结束位置-->
<sequenceFlow id="flow1" sourceRef="start" targetRef="stu_task"></sequenceFlow>
<sequenceFlow id="flow2" name="请假" sourceRef="stu_task" targetRef="te_task"></sequenceFlow>
<sequenceFlow id="flow3" name="审批" sourceRef="te_task" targetRef="getway1"></sequenceFlow>
<sequenceFlow id="flow3_1" name="同意" sourceRef="getway1" targetRef="mte_task">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow3_2" name="拒绝" sourceRef="getway1" targetRef="stu_task">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" name="审批" sourceRef="mte_task" targetRef="getway2"></sequenceFlow>
<sequenceFlow id="flow4_1" name="同意" sourceRef="getway2" targetRef="end">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4_2" name="拒绝" sourceRef="getway2" targetRef="stu_task">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
</sequenceFlow>
</process>
</definitions>
流程引擎API
相关概念解读:
- RepositoryService很可能是使用Flowable引擎要用的第一个服务。这个服务提供了管理与控制部署(deployments)与流程定义(process definitions)的操作。管理静态信息,
- RuntimeService用于启动流程定义的新流程实例。
- IdentityService很简单。它用于管理(创建,更新,删除,查询……)组与用户。
- FormService是可选服务。也就是说Flowable没有它也能很好地运行,而不必牺牲任何功能。
- HistoryService暴露Flowable引擎收集的所有历史数据。要提供查询历史数据的能力。
- ManagementService通常在用Flowable编写用户应用时不需要使用。它可以读取数据库表与表原始数据的信息,也提供了对作业(job)的查询与管理操作。
- DynamicBpmnService可用于修改流程定义中的部分内容,而不需要重新部署它。例如可以修改流程定义中一个用户任务的办理人设置,或者修改一个服务任务中的类名。
测试类
package com.njcn.harmonic.controller;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
/**
* @author qijian
* @version 1.0.0
* @createTime 2022/11/7 - 14:33
*/
@Slf4j
@RestController
@RequestMapping("/flow")
@Api(tags = "流程图测试")
@AllArgsConstructor
public class FlowTestController {
@Autowired
private RepositoryService repositoryService;
@Autowired
private RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@Autowired
private HistoryService historyService;
@Autowired
private IdentityService identityService;
@PostMapping("/leaveApprovalFlow")
@ApiOperation("跑流程")
public void leaveApprovalFlow() {
/*
* Step 1: 查询部署的流程定义
*/
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").list();
List<ProcessDefinition> pages = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").listPage(1, 30);
/*
* Step 2: 启动流程,创建实例
*/
//流程定义的key,对应请假的流程图
String processDefinitionKey = "leave_approval";
//业务代码,根据自己的业务用
String businessKey = "schoolleave";
//流程变量,可以自定义扩充
Map<String, Object> variablesDefinition = new HashMap<>();
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variablesDefinition);
log.info("启动成功:{}", processInstance.getId());
/*
* Step 3: 查询指定流程所有启动的实例列表
* 列表,或 分页 删除
*/
List<Execution> executions = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").list();
List<Execution> executionPages = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").listPage(1, 30);
// runtimeService.deleteProcessInstance(processInstanceId, deleteReason); //删除实例
/*
* Step 4: 学生查询可以操作的任务,并完成任务
*/
//候选人 xml文件里面的 flowable:assignee
//String stu = "stu";
//List<Task> taskList = taskService.createTaskQuery().taskAssignee(stu).orderByTaskId().desc().list();
//候选组 xml文件里面的 flowable:candidateGroups
String candidateGroup = "stu_group";
List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).orderByTaskId().desc().list();
for (Task task : taskList) {
// 申领任务,并指定一个用户
taskService.claim(task.getId(), "my");
// 完成
taskService.complete(task.getId());
}
/*
* Step 5: 老师查询可以操作的任务,并完成任务
*/
//候选组 xml文件里面的 flowable:candidateGroups="te_group"
String candidateGroupTe = "te_group";
List<Task> taskListTe = taskService.createTaskQuery().taskCandidateGroup(candidateGroupTe).orderByTaskCreateTime().desc().list();
for (Task task : taskListTe) {
// 申领任务,并指定一个用户
taskService.claim(task.getId(), "myte");
// 完成
Map<String, Object> variables = new HashMap<>();
//携带变量,用于网关流程的条件判定,这里的条件是同意(xml配置)
variables.put("command","agree");
// variables.put("command","refuse");
taskService.complete(task.getId(), variables);
}
/*
* Step 6: 历史查询,因为一旦流程执行完毕,活动的数据都会被清空,上面查询的接口都查不到数据,但是提供历史查询接口
*/
// 历史流程实例
List<HistoricProcessInstance> historicProcessList = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave_approval").list();
// 历史任务
List<HistoricTaskInstance> historicTaskList = historyService.createHistoricTaskInstanceQuery().processDefinitionKey("leave_approval").list();
// 实例历史变量 , 任务历史变量
// historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId);
// historyService.createHistoricVariableInstanceQuery().taskId(taskId);
// *****************************************************分隔符********************************************************************
// *****************************************************分隔符********************************************************************
// 可能还需要的API
// 移动任务,人为跳转任务
// runtimeService.createChangeActivityStateBuilder().processInstanceId(processInstanceId)
// .moveActivityIdTo(currentActivityTaskId, newActivityTaskId).changeState();
// 如2果在数据库配置了分组和用户,还会用到
// List<User> users = identityService.createUserQuery().list(); //用户查询,用户id对应xml 里面配置的用户
// List<Group> groups = identityService.createGroupQuery().list(); //分组查询,分组id对应xml 里面配置的分组 如 stu_group,te_group 在表里是id的值
// 另外,每个查询后面都可以拼条件,内置恁多查询,包括模糊查询,大小比较都有
}
}
数据库
项目启动之后,会在配置的数据库中建立大约50张默认表:Flowable的所有数据库表都以ACT_开头。
整体结构
ACT_RE_
: 'RE’代表repository。带有这个前缀的表包含“静态”信息,例如流程定义与流程资源(图片、规则等)。ACT_RU_
: 'RU’代表runtime。这些表存储运行时信息,例如流程实例(process instance)、用户任务(user task)、变量(variable)、作业(job)等。Flowable只在流程实例运行中保存运行时数据,并在流程实例结束时删除记录。这样保证运行时表小和快。ACT_HI_
: 'HI’代表history。这些表存储历史数据,例如已完成的流程实例、变量、任务等。ACT_GE_
: 通用数据。在多处使用。
常用表
-
通用数据表(2个)
- act_ge_bytearray:二进制数据表,如流程定义、流程模板、流程图的字节流文件;
- act_ge_property:属性数据表(不常用);
-
历史表(8个,HistoryService接口操作)
- act_hi_actinst:历史节点表,存放流程实例运转的各个节点信息(包含开始、结束等非任务节点);
- act_hi_attachment:历史附件表,存放历史节点上传的附件信息(不常用);
- act_hi_comment:历史意见表;
- act_hi_detail:历史详情表,存储节点运转的一些信息(不常用);
- act_hi_identitylink:历史流程人员表,存储流程各节点候选、办理人员信息,常用于查询某人或部门的已办任务;
- act_hi_procinst:历史流程实例表,存储流程实例历史数据(包含正在运行的流程实例);
- act_hi_taskinst:历史流程任务表,存储历史任务节点;
- act_hi_varinst:流程历史变量表,存储流程历史节点的变量信息;
-
用户相关表(4个,IdentityService接口操作)
- act_id_group:用户组信息表,对应节点选定候选组信息;
- act_id_info:用户扩展信息表,存储用户扩展信息;
- act_id_membership:用户与用户组关系表;
- act_id_user:用户信息表,对应节点选定办理人或候选人信息;
-
流程定义、流程模板相关表(3个,RepositoryService接口操作)
- act_re_deployment:部属信息表,存储流程定义、模板部署信息;
- act_re_procdef:流程定义信息表,存储流程定义相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;
- act_re_model:流程模板信息表,存储流程模板相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;
-
流程运行时表(6个,RuntimeService接口操作)
- act_ru_task:运行时流程任务节点表,存储运行中流程的任务节点信息,重要,常用于查询人员或部门的待办任务时使用;
- act_ru_event_subscr:监听信息表,不常用;
- act_ru_execution:运行时流程执行实例表,记录运行中流程运行的各个分支信息(当没有子流程时,其数据与act_ru_task表数据是一一对应的);
- act_ru_identitylink:运行时流程人员表,重要,常用于查询人员或部门的待办任务时使用;
- act_ru_job:运行时定时任务数据表,存储流程的定时任务信息;
- act_ru_variable:运行时流程变量数据表,存储运行中的流程各节点的变量信息;