1. Flowable概述
1.1 Flowable简介
Flowable是一个使用Java编写的轻量级业务流程引擎。Flowable流程引擎可用于部署BPMN 2.0流程定义(用于定义流程的行业XML标准), 创建这些流程定义的流程实例,进行查询,访问运行中或历史的流程实例与相关数据,等等。
TKJohn.github.io:https://tkjohn.github.io/#/
Flowable 6.3.0 BPMN用户手册中文版:https://tkjohn.github.io/flowable-userguide/
1.2 Flowable与Activiti的区别
Flowable是Activiti(Alfresco持有的注册商标)的fork。在下面的章节中,你会注意到包名,配置文件等等,都使用flowable。 Tijs Rademakers以及Salaboy原本都是Activiti团队,后来因为理念不一样Tijs Rademakers独立出来创建Flowable。Activiti5以及Activiti6、Flowable是Tijs Rademakers团队开发的。Activiti7是 Salaboy团队开发的。Flowable更注重其功能性、性能以及扩展性,Flowable在Activiti6的基础上修复了的部分的bug,且功能性能上得到了很大的优化。Activiti7则更关注云结合,性能功能还是沿用Activiti6的内核。
Flowable比较臃肿,它支持了太多的东西,以致于如果想做POC或者Demo,环境搭建这一步都够呛。但是如果你本身就想做一个扩展性强的,性能高的工作流平台(SaaS\PaaS),Flowable是不二的选择。
Activiti与Flowable框架情况,Activiti Issues 396,Flowable Issues 220 activiti的github地址:https://github.com/Activiti/Activiti flowable的github地址:https://github.com/flowable/flowable-engine
2. Flowable流程设计
Flowable流程设计就是指设计BPMN文件流的设计,BPMN本质上就是流程的行业标准的XML文件,这里提供四种方式实现BPMN工作流的绘制。第一种是手动编写bpmn文件,剩下三种采用工具形式,分别是Eclipse 插件Eclipse Designer, Idea 插件Idea actiBPM,以及Flowable官方提供的web页面Flowable UI。
2.1 手动编写BPMN文件
根据bpmn文件格式实现手工编写BPMN,比较繁琐,但是根据官方表示这种更易于理解工作流。^ _ ^
BPMN 2.0介绍:https://tkjohn.github.io/flowable-userguide/#bpmn20
2.2 Eclipse Designer
Flowable提供了名为Flowable Eclipse Designer的Eclipse插件,可以用于图形化地建模、测试与部署BPMN 2.0流程。
Eclipse Designer:https://tkjohn.github.io/flowable-userguide/#flowableDesigner
2.3 Idea actiBPM
1.安装idea插件 actiBPM插件。 2.在工程中新建一个文件夹保存工作流文件, 右键该文件夹New–>BPMN File
注意: 更改idea配置文件, 否则工作流中输入中文会乱码。 1.进入idea安装目录的bin文件夹下, 找到idea.exe.vmoptions与idea64.exe.vmoptions两个文件。 2.在两个文件的最后加入 -Dfile.encoding=UTF-8
因为activiti的默认流程图格式是bpmn, 但是idea必须xml格式才能生成图片, 所以改为这样, 后面部署流程的时候才可以部署上去, 否则是存不进数据库。
2.4 Flowable UI
Flowable UI是Flowable官方提供的可操作界面web应用。 Flowable UI:https://tkjohn.github.io/flowable-userguide/#flowableUIApps
Flowable UI主要包含四个模块
Flowable IDM: 身份管理应用。为所有Flowable UI应用提供单点登录认证功能,并且为拥有IDM管理员权限的用户提供了管理用户、组与权限的功能。
Flowable Modeler: 让具有建模权限的用户可以创建流程模型、表单、选择表与应用定义。
Flowable Task: 运行时任务应用。提供了启动流程实例、编辑任务表单、完成任务,以及查询流程实例与任务的功能。
Flowable Admin: 管理应用。让具有管理员权限的用户可以查询BPMN、DMN、Form及Content引擎,并提供了许多选项用于修改流程实例、任务、作业等。管理应用通过REST API连接至引擎,并与Flowable Task应用及Flowable REST应用一同部署。
Flowable UI部署安装 Flowable UI安装部署相对比较简单,只需要下载Apache Tomcat,Flowable 6。将Flowable下载压缩包中的war包放到tomcat中的webapps中,启动即可。
1.下载最新稳定版本的Apache Tomcat。
2.下载最新稳定版本的Flowable 6。
3.将Flowable发行包中,wars文件夹下的flowable-admin.war、flowable-idm.war、flowable-modeler.war与flowable-task.war文件,复制到Tomcat的webapps文件夹下。
4.运行bin/startup.sh(在Mac OS或Linux下),或bin/startup.bat(在Windows下)脚本,启动Tomcat服务器。
5.打开web浏览器,访问http://localhost:8080/flowable-modeler。
注意事项 1.修改数据库配置 应用默认是H2数据库,关闭数据清除,可以修改数据库配置。 路径:D:\flowable\apache-tomcat-8.5.77\webapps\flowable-ui\WEB-INF\classes\flowable-default.properties 修改内容:关闭H2数据库驱动,打开mysql驱动配置。 2.Windows环境下tomcat乱码问题 win环境下编码格式GBK,修改下tomcat下cofg下logging.properties文件设置的编码格式,将原来的utf-8全部替换为gbk即可。
3. Flowable表结构介绍
Flowable的所有数据库表都以ACT_开头。第二部分是说明表用途的两字符标示符。服务API的命名也大略符合这个规则。
ACT_RE_*: 'RE’代表repository。带有这个前缀的表包含“静态”信息,例如流程定义与流程资源(图片、规则等)。
ACT_RU_*: 'RU’代表runtime。这些表存储运行时信息,例如流程实例(process instance)、用户任务(user task)、变量(variable)、作业(job)等。Flowable只在流程实例运行中保存运行时数据,并在流程实例结束时删除记录。这样保证运行时表小和快。
ACT_HI_*: 'HI’代表history。这些表存储历史数据,例如已完成的流程实例、变量、任务等。
ACT_GE_*: 通用数据。在多处使用。
1)通用数据表(2个)
act_ge_bytearray:二进制数据表,如流程定义、流程模板、流程图的字节流文件;
act_ge_property:属性数据表(不常用);
2)历史表(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:流程历史变量表,存储流程历史节点的变量信息;
3)用户相关表(4个,IdentityService接口操作的表)
act_id_group:用户组信息表,对应节点选定候选组信息;
act_id_info:用户扩展信息表,存储用户扩展信息;
act_id_membership:用户与用户组关系表;
act_id_user:用户信息表,对应节点选定办理人或候选人信息;
4)流程定义、流程模板相关表(3个,RepositoryService接口操作的表)
act_re_deployment:部属信息表,存储流程定义、模板部署信息;
act_re_procdef:流程定义信息表,存储流程定义相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;
act_re_model:流程模板信息表,存储流程模板相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;
5)流程运行时表(6个,RuntimeService接口操作的表)
act_ru_task:运行时流程任务节点表,存储运行中流程的任务节点信息,重要,常用于查询人员或部门的待办任务时使用;
act_ru_event_subscr:监听信息表,不常用;
act_ru_execution:运行时流程执行实例表,记录运行中流程运行的各个分支信息(当没有子流程时,其数据与act_ru_task表数据是一一对应的);
act_ru_identitylink:运行时流程人员表,重要,常用于查询人员或部门的待办任务时使用;
act_ru_job:运行时定时任务数据表,存储流程的定时任务信息;
act_ru_variable:运行时流程变量数据表,存储运行中的流程各节点的变量信息;
4. Flowable基础操作
4.1 环境依赖
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-engine</artifactId>
<version>6.6.0</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.3.176</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
4.2 配置文件
spring:
datasource:
url: jdbc:mysql://localhost:3306/flowable?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8
driver-class-name: com.mysql.cj.jdbc.Driver
username: root
password: 123456
4.3 代码实现
holiday-request.bpmn20.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"/>
<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"/>
<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>
HolidayFlowTest
package com.zrj.flowable.util;
import org.flowable.engine.*;
import org.flowable.engine.history.HistoricActivityInstance;
import org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.task.api.Task;
import org.junit.Before;
import org.junit.Test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
public class HolidayFlowTest {
ProcessEngineConfiguration cfg = null;
@Before
public void initProcessEngineConfiguration() {
cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?serverTimezone=UTC&nullCatalogMeansCurrent=true")
.setJdbcUsername("root")
.setJdbcPassword("123456")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
System.out.println("初始化流程引擎数据库配置完成");
}
@Test
public void deploymentProcess() {
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()
.addClasspathResource("processes/holiday-request.bpmn20.xml")
.name("请求流程")
.deploy();
System.out.println("【部署流程】deployment.getId() = " + deployment.getId());
System.out.println("【部署流程】deployment.getName() = " + deployment.getName());
}
@Test
public void queryProcess() {
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.deploymentId("1")
.singleResult();
System.out.println("【查看流程定义】processDefinition.getId() = " + processDefinition.getId());
System.out.println("【查看流程定义】processDefinition.getName() = " + processDefinition.getName());
System.out.println("【查看流程定义】processDefinition.getDeploymentId() = " + processDefinition.getDeploymentId());
System.out.println("【查看流程定义】processDefinition.getDescription() = " + processDefinition.getDescription());
}
@Test
public void startProcess() {
ProcessEngine processEngine = cfg.buildProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();
Map<String, Object> variables = new HashMap<>();
variables.put("employee", "张三");
variables.put("nrOfHolidays", 3);
variables.put("description", "这是个请假原因");
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
System.out.println("【启动流程实例】流程定义的ID:" + processInstance.getProcessDefinitionId());
System.out.println("【启动流程实例】流程实例的ID:" + processInstance.getId());
System.out.println("【启动流程实例】当前活动的ID:" + processInstance.getActivityId());
}
@Test
public void queryTask() {
ProcessEngine processEngine = cfg.buildProcessEngine();
TaskService taskService = processEngine.getTaskService();
List<Task> list = taskService.createTaskQuery()
.processDefinitionKey("holidayRequest")
.taskAssignee("lisi")
.list();
for (Task task : list) {
System.out.println("【查看任务】task.getProcessDefinitionId() = " + task.getProcessDefinitionId());
System.out.println("【查看任务】task.getId() = " + task.getId());
System.out.println("【查看任务】task.getAssignee() = " + task.getAssignee());
System.out.println("【查看任务】task.getName() = " + task.getName());
}
}
@Test
public void completeTask() {
ProcessEngine processEngine = cfg.buildProcessEngine();
TaskService taskService = processEngine.getTaskService();
Task task = taskService.createTaskQuery()
.processDefinitionKey("holidayRequest")
.taskAssignee("lisi")
.singleResult();
Map<String, Object> variables = new HashMap<>();
variables.put("approved", false);
taskService.complete(task.getId(), variables);
}
@Test
public void deleteProcess() {
ProcessEngine processEngine = cfg.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
repositoryService.deleteDeployment("1", true);
}
@Test
public void testQueryHistory() {
ProcessEngine processEngine = cfg.buildProcessEngine();
HistoryService historyService = processEngine.getHistoryService();
List<HistoricActivityInstance> list = historyService.createHistoricActivityInstanceQuery()
.processDefinitionId("holidayRequest:1:10003")
.finished()
.orderByHistoricActivityInstanceEndTime().asc()
.list();
for (HistoricActivityInstance historicActivityInstance : list) {
System.out.println(historicActivityInstance.getActivityId() + " took "
+ historicActivityInstance.getDurationInMillis() + " milliseconds");
}
}
}
5. Flowable整合SpringBoot
Spring Boot提倡约定大于配置。要开始工作,只需在项目中添加flowable-spring-boot-starter或flowable-spring-boot-starter-rest依赖。如果不需要引入所有的引擎,可以查看其它的Flowable starter。 Flowable整合SpringBoot:https://tkjohn.github.io/flowable-userguide/#springSpringBoot
FinancialReportProcess.bpmn20.xml
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>com.h2database</groupId>
<artifactId>h2</artifactId>
<version>1.4.197</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.45</version>
</dependency>
<definitions id="definitions"
targetNamespace="http://flowable.org/bpmn20"
xmlns:flowable="http://flowable.org/bpmn"
xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL">
<process id="financialReport" name="Monthly financial report reminder process">
<startEvent id="theStart" />
<sequenceFlow id="flow1" sourceRef="theStart" targetRef="writeReportTask" />
<userTask id="writeReportTask" name="Write monthly financial report" >
<documentation>
Write monthly financial report for publication to shareholders.
</documentation>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>accountancy</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<sequenceFlow id="flow2" sourceRef="writeReportTask" targetRef="verifyReportTask" />
<userTask id="verifyReportTask" name="Verify monthly financial report" >
<documentation>
Verify monthly financial report composed by the accountancy department.
This financial report is going to be sent to all the company shareholders.
</documentation>
<potentialOwner>
<resourceAssignmentExpression>
<formalExpression>management</formalExpression>
</resourceAssignmentExpression>
</potentialOwner>
</userTask>
<sequenceFlow id="flow3" sourceRef="verifyReportTask" targetRef="theEnd" />
<endEvent id="theEnd" />
</process>
</definitions>
FlowableController
package com.zrj.flowable.controller;
import com.zrj.flowable.entity.TaskRepresentation;
import com.zrj.flowable.service.FinancialReportProcessService;
import com.zrj.flowable.service.OneTaskProcessService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import java.util.ArrayList;
import java.util.List;
@RestController
@RequestMapping("/flow")
@Api(tags = "FlowableController", description = "流程控制器")
public class FlowableController {
@Autowired
private OneTaskProcessService oneTaskProcessService;
@Autowired
private FinancialReportProcessService financialReportProcessService;
@GetMapping("/start")
@ApiOperation("启动金融报表")
public String financialReportProcessStart() {
financialReportProcessService.financialReportProcessStart();
return String.format("FinancialReportProcessStart %s!", "Success!");
}
@ApiOperation("启动流程")
@RequestMapping(value = "/process", method = RequestMethod.POST)
public void startProcessInstance() {
oneTaskProcessService.startProcess();
}
@ApiOperation("获取流程节点")
@RequestMapping(value = "/tasks", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public List<TaskRepresentation> getTasks(@RequestParam String assignee) {
List<Task> tasks = oneTaskProcessService.getTasks(assignee);
List<TaskRepresentation> dtos = new ArrayList<>();
for (Task task : tasks) {
dtos.add(new TaskRepresentation(task.getId(), task.getName()));
}
return dtos;
}
}
FinancialReportProcessService
package com.zrj.flowable.service;
import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.springframework.stereotype.Service;
@Service
public class FinancialReportProcessService {
public void financialReportProcessStart() {
ProcessEngine processEngine = ProcessEngineConfiguration
.createStandaloneProcessEngineConfiguration()
.buildProcessEngine();
RepositoryService repositoryService = processEngine.getRepositoryService();
RuntimeService runtimeService = processEngine.getRuntimeService();
repositoryService.createDeployment()
.addClasspathResource("FinancialReportProcess.bpmn20.xml")
.deploy();
runtimeService.startProcessInstanceByKey("financialReport");
System.out.println("FinancialReportProcess start success!");
}
}
|