介绍:工作流引擎目前比较热门的有Activiti、Flowable等,Flowable是Activiti(Alfresco持有的注册商标)的fork版本。下面就两种工作流引擎做一个比较和入门分析。
模块一 对比:
- Activiti现存文档比Flowable多,有大量与业务集成的文档及博客。相比之下,学习Flowable主要为官方的用户手册。
- Flowable是Activiti的fork版本,所以API使用类似,包括核心类、各种服务类和业务对象。
- Flowable目前最新版本为6.4.X,其插件设计器只支持Eclipse;Activiti目前最新版本为7.X,这个版本是基于6.X版本的Bug修改和API封装。
- 查看Flowable的发展史,发现Flowable的开发人员是原来Activiti的主要负责人员,所以可能Flowable为后起之秀。但目前与业务集成文档较少。
模块二 Flowable引擎使用:
- Flowable有几大引擎,分别用来管理不同部分
- ProcessEngine(重要):核心引擎,其下管理着各种Service
- RuntimeService:负责创建实例
- TaskService:负责操作任务,如查询任务
- RepositoryService: 负责操作流程,如加载bpmn文件部署流程、创建查询对象
- CmmnEngine
- DmnEngine:决策引擎
- FormEngine:表单引擎
- ContentEngine:内容引擎
- IdmEngine:身份识别引擎
- ProcessEngine(重要):核心引擎,其下管理着各种Service
2. 独立运行的Flowable引擎
2.1 引入引擎的依赖,这里数据库使用的是内存数据库
<!-- Standalone -->
<dependencies>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.3.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.21</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.21</version>
</dependency>
</dependencies>
2.2 创建引擎配置,并使用此配置生成引擎
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:h2:mem:flowable;DB_CLOSE_DELAY=-1")
.setJdbcUsername("sa")
.setJdbcPassword("")
.setJdbcDriver("org.h2.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
ProcessEngine processEngine = cfg.buildProcessEngine();
2.3 使用RepositoryService部署一个流程,需提供BPMN或BPMN 2.0 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: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" flowable:candidateGroups="managers" />
<sequenceFlow sourceRef="approveTask" targetRef="decision"/>
<exclusiveGateway id="decision"/>
<sequenceFlow sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${approved}
]]>
</conditionExpression>
</sequenceFlow>
<sequenceFlow sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression">
<![CDATA[
${!approved}
]]>
</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" flowable:assignee="${employee}"/>
<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>
部署流程:
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deploy = repositoryService.createDeployment().addClasspathResource("processes/one-task-process.bpmn20.xml").deploy();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
System.out.println("流程定义: " + processDefinition.getName());
2.4 使用RuntimeService开启一个流程实例任务
String employee = "dedu";
Integer holidayNumber = 5;
String reason = "tensorflow";
//根据process id 创建实例
String processId = "holidayRequest";
RuntimeService runtimeService = processEngine.getRuntimeService();
// 开启实例并传入参数
Map<String, Object> conditions = new HashMap<>();
conditions.put("employee", employee);
conditions.put("holidayNumber", holidayNumber);
conditions.put("reason", reason);
runtimeService.startProcessInstanceByKey(processId, conditions);
2.5 使用TaskService查询并完成任务
// 查询任务
TaskService taskService = processEngine.getTaskService();
List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("你有"+ taskList.size() + "个任务!");
Map<String, Object> variables = taskService.getVariables(taskList.get(0).getId());
System.out.println(variables);
// 完成任务
variables = new HashMap<>();
variables.put("approved", true);
taskService.complete(taskList.get(0).getId(), variables);
//查询 任务数量
List<Task> endTaskList = taskService.createTaskQuery().taskCandidateGroup("managers").list();
System.out.println("你有"+ endTaskList.size() + "个任务!");
2.6 可以配置任务完成后的回调,注意package名字需改成org.flowable并实现JavaDelegate接口:
package org.flowable;
import org.flowable.engine.delegate.DelegateExecution;
import org.flowable.engine.delegate.JavaDelegate;
public class CallExternalSystemDelegate implements JavaDelegate {
@Override
public void execute(DelegateExecution delegateExecution) {
System.out.println("任务完成后回调:");
System.out.println(delegateExecution.getVariables());
}
}
3 整合SpringBoot的Flowable引擎,即由Spring管理Flowable,然后在使用各种Service时直接依赖注入即可。
3.1 引入依赖,这里使用的Flowable引擎版本为6.4(引入的依赖为flowable-spring-boot-starter,即会加载所有引擎,如只需个别引擎,可单独引入)。这里使用MySQL数据库管理流程,所以引入了JPA。
<properties>
<java.version>1.8</java.version>
<flowable.version>6.4.0</flowable.version>
</properties>
<dependencies>
<dependency>
<groupId>org.flowable</groupId>