深入解析Flowable工作流引擎:從原理到實踐
1.概述
最近在研究工作流功能,所以趁此機會在這里深入了解下工作流相關知識點和流程引擎開源框架。工作流也可以叫審批流,在當下互聯(lián)網(wǎng)快速發(fā)展和自動化辦公的背景下,有很多業(yè)務場景都是需要走流程審批的,比如說日常的請假流程:提交申請→領導審批→通過或拒絕→抄送人事→結束。那到底什么是工作流呢?
工作流是什么?
工作流是對工作流程及其各操作步驟之間業(yè)務規(guī)則的抽象、概括描述。工作流建模,即將工作流程中的工作如何前后組織在一起的邏輯和規(guī)則,在計算機中以恰當?shù)哪P捅磉_并對其實施計算。工作流要解決的主要問題是:為實現(xiàn)某個業(yè)務目標,利用計算機在多個參與者之間按某種預定規(guī)則自動傳遞文檔、信息或者任務。簡單來說,工作流就是對業(yè)務的流程化抽象。
開源的工作流引擎很多,比如 activiti、Flowable、Camunda 等,后兩個都是基于activiti分叉出來開發(fā)的,所以這些框架都是同宗同源的,研究一個即可,其他的使用套路,實現(xiàn)原理都是差不多的。這里就來聊聊當下使用較多,比較主流框架:Flowable
2.Flowable核心概念和原理
Flowable是一個基于Apache 2.0許可證的開源工作流引擎,源自Activiti項目分支。它實現(xiàn)了BPMN 2.0規(guī)范,提供了完整的流程定義、執(zhí)行和監(jiān)控能力。
BPMN(Bussiness Process Model Notation):業(yè)務流程管理和符號, 簡單來說就是工作流模型,是一種流行的業(yè)務流程建模語言。它是為業(yè)務流程建模而設計的,用于支持業(yè)務流程管理,包括分析、設計、優(yōu)化和實施。通過提供易于理解的、視覺化的圖形表示法,使得非技術性的業(yè)務人員也能很好地理解業(yè)務過程。
下面是Flowable官網(wǎng)的核心api架構圖:
服務接口 | 職責說明 | 典型方法示例 |
RepositoryService | 流程定義和部署管理 | deploy(), createDeployment() |
RuntimeService | 流程運行時控制 | startProcessInstanceByKey() |
TaskService | 用戶任務操作 | complete(), claim() |
HistoryService | 歷史數(shù)據(jù)查詢 | createHistoricTaskInstanceQuery() |
ManagementService | 引擎管理和維護 | executeCustomSql() |
ProcessEngine:整個Flowable引擎的核心入口,通過它可以獲取所有Service
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();核心流程:
圖片
3.Spring Boot整合實戰(zhàn)
3.1 依賴配置
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.7.2</version>
</dependency>3.2 流程模型定義
設計一個請假流程如下圖所示:
圖片
在服務的資源文件resources下新建一個processes目錄,然后定義一個流程文件:leave-process.bpmn20.xml:
<?xml versinotallow="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-process" name="leave-process" isExecutable="true">
<startEvent id="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7"/>
<userTask id="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad" name="請假" flowable:assignee="#{leaveTask}">
<documentation>員工請假</documentation>
</userTask>
<sequenceFlow id="sid-ff0c2afe-1c1c-4e2f-b2eb-5083c5fadb41" sourceRef="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7" targetRef="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad"/>
<userTask id="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab" name="組長審批" flowable:assignee="#{teamTask}"/>
<sequenceFlow id="sid-32a2a643-9ce6-44f3-bb94-f9df3d97c49b" sourceRef="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad" targetRef="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab"/>
<exclusiveGateway id="sid-c03f8946-a6cd-4e38-881a-70d450e32748" name="組長審批網(wǎng)關"/>
<sequenceFlow id="sid-bbed6467-cc35-41d2-ad08-2c3b17b8ca83" sourceRef="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab" targetRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748"/>
<sequenceFlow id="sid-afe09a37-7634-4166-8617-68d5f35edef7" sourceRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748" targetRef="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" name="組長審批通過">
<conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"通過")}</conditionExpression>
</sequenceFlow>
<sequenceFlow id="sid-df2ad71d-6aa1-4d43-a9f9-bbdad8687e3a" sourceRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748" targetRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" name="組長審批拒絕">
<conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"拒絕")}</conditionExpression>
</sequenceFlow>
<userTask id="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" name="經(jīng)理審批" flowable:assignee="#{manageTask}"/>
<exclusiveGateway id="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" name="經(jīng)理審批網(wǎng)關"/>
<sequenceFlow id="sid-ffa6dd8b-ae8d-47a5-a2ff-45d25e58dcac" sourceRef="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" targetRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53"/>
<serviceTask id="sid-e720a425-9f57-4a42-9833-b5f4f0175840" flowable:exclusive="true" name="發(fā)送失敗提示" isForCompensation="true" flowable:class="com.xx.process.service.LeaveFailService"/>
<endEvent id="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb"/>
<sequenceFlow id="sid-6e2fc1e3-43be-4768-96f5-d9c7861cbce7" sourceRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" targetRef="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb"/>
<sequenceFlow id="sid-c90c5e3e-e5c2-40f0-8324-44d548de0ef2" sourceRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" targetRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" name="經(jīng)理審批拒絕">
<conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"拒絕")}</conditionExpression>
</sequenceFlow>
<endEvent id="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1"/>
<sequenceFlow id="sid-5cad8e7b-8ee3-4e34-b94f-920a362f578d" sourceRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" targetRef="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1" name="經(jīng)理審批通過">
<conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"通過")}</conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_ask_for_leave">
<bpmndi:BPMNPlane bpmnElement="ask_for_leave" id="BPMNPlane_ask_for_leave">
<bpmndi:BPMNShape id="shape-013aa23c-13e6-466e-942b-6a1a308eeda7" bpmnElement="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7">
<omgdc:Bounds x="-2235.0" y="-1200.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-10dbd0c8-a3b1-4a9e-b214-5c506e80fd1d" bpmnElement="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad">
<omgdc:Bounds x="-2145.0" y="-1224.9999" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-97bf72f7-edcb-4813-9e03-73bc597d1492" bpmnElement="sid-ff0c2afe-1c1c-4e2f-b2eb-5083c5fadb41">
<omgdi:waypoint x="-2205.0" y="-1185.0"/>
<omgdi:waypoint x="-2145.0" y="-1184.9999"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-df2f6c22-1d44-4180-94fb-87753f871822" bpmnElement="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab">
<omgdc:Bounds x="-1945.0" y="-1225.0001" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-e0d1746e-e6b5-481a-a492-83881d2a9c22" bpmnElement="sid-32a2a643-9ce6-44f3-bb94-f9df3d97c49b">
<omgdi:waypoint x="-2045.0" y="-1184.9999"/>
<omgdi:waypoint x="-1945.0" y="-1185.0001"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-6e0e9d80-5884-4ca5-847d-d3e555005c51" bpmnElement="sid-c03f8946-a6cd-4e38-881a-70d450e32748">
<omgdc:Bounds x="-1735.0" y="-1205.0" width="40.0" height="40.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-9dbfbe2b-c839-44eb-b74b-fceee48631b7" bpmnElement="sid-bbed6467-cc35-41d2-ad08-2c3b17b8ca83">
<omgdi:waypoint x="-1845.0" y="-1185.0001"/>
<omgdi:waypoint x="-1735.0" y="-1185.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-1ff96c95-d96d-4241-9d0f-85414bbb3859" bpmnElement="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687">
<omgdc:Bounds x="-1570.0" y="-1224.9999" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-f8f6488f-354a-490c-9d22-1f57e9a654d4" bpmnElement="sid-afe09a37-7634-4166-8617-68d5f35edef7">
<omgdi:waypoint x="-1695.0" y="-1185.0"/>
<omgdi:waypoint x="-1632.5" y="-1185.0"/>
<omgdi:waypoint x="-1570.0" y="-1184.9999"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-bb52e23c-ee53-4073-9b74-c5dca8459100" bpmnElement="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53">
<omgdc:Bounds x="-1395.0" y="-1205.0" width="40.0" height="40.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-6e5e32bc-5b44-41f5-97e6-458c1aa7b7f3" bpmnElement="sid-ffa6dd8b-ae8d-47a5-a2ff-45d25e58dcac">
<omgdi:waypoint x="-1470.0" y="-1184.9999"/>
<omgdi:waypoint x="-1395.0" y="-1185.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-26269b4b-a620-4e2f-a463-145fdfd8c0b1" bpmnElement="sid-e720a425-9f57-4a42-9833-b5f4f0175840">
<omgdc:Bounds x="-1764.9999" y="-1055.0" width="100.0" height="80.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape id="shape-47431d28-2f8b-4b2c-81e0-19dc832d1880" bpmnElement="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb">
<omgdc:Bounds x="-1910.0" y="-1030.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-cfc9460b-eaf6-44ee-9baa-300e89b18f4d" bpmnElement="sid-6e2fc1e3-43be-4768-96f5-d9c7861cbce7">
<omgdi:waypoint x="-1764.9999" y="-1015.0"/>
<omgdi:waypoint x="-1880.0" y="-1015.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-46d26639-0c9b-40da-b5ec-10678783920b" bpmnElement="sid-c90c5e3e-e5c2-40f0-8324-44d548de0ef2">
<omgdi:waypoint x="-1375.0" y="-1165.0"/>
<omgdi:waypoint x="-1375.0" y="-1015.0"/>
<omgdi:waypoint x="-1664.9998" y="-1015.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNShape id="shape-cfc8c0f0-f529-4023-83a6-e8635533be1b" bpmnElement="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1">
<omgdc:Bounds x="-1200.0" y="-1200.0" width="30.0" height="30.0"/>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge id="edge-5c22485e-bc92-4af7-b814-6f384c98ba5d" bpmnElement="sid-5cad8e7b-8ee3-4e34-b94f-920a362f578d">
<omgdi:waypoint x="-1355.0" y="-1185.0"/>
<omgdi:waypoint x="-1200.0" y="-1185.0"/>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge id="edge-eb5ecc69-91e8-4e50-81e2-14123d77ee2d" bpmnElement="sid-df2ad71d-6aa1-4d43-a9f9-bbdad8687e3a">
<omgdi:waypoint x="-1715.0" y="-1165.0"/>
<omgdi:waypoint x="-1714.9999" y="-1055.0"/>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>這樣項目啟動之后,F(xiàn)lowable會自動加載這個流程定義文件,寫入數(shù)據(jù)庫相關表。
3.3 流程模型使用
基于模型發(fā)起一次審批流程:
@Autowired
RuntimeService runtimeService;
@Autowired
TaskService taskService;
// 員工id
publicstaticfinal String userId = "user_001";
// 組長id
publicstaticfinal String teamId = "team_001";
// 部門經(jīng)理id
publicstaticfinal String depId = "dep_001";
@Test
public void leaveRequest() {
HashMap<String, Object> map = new HashMap<>();
map.put("leaveTask", userId);
// 開啟流程的key,就是流程定義文件里 process 標簽的id
ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave-process", map);
// 設置一些參數(shù)
runtimeService.setVariable(processInstance.getId(), "name", "java-boy");
runtimeService.setVariable(processInstance.getId(), "reason", "想休息個幾天,心累了");
runtimeService.setVariable(processInstance.getId(), "days", 3);
log.info("======>>>創(chuàng)建請假流程, 流程實例processInstanceId:{}", processInstance.getId());
}上面的實現(xiàn)邏輯其實就是員工user001發(fā)起了一次請假申請,并附帶了一些變量信息,比如說請假幾天之類的。
提交請假:
/**
* 員工提交請假
*/
@Test
public void submitToTeam() {
// 員工查找到自己的任務,然后提交給組長審批
List<Task> list = taskService.createTaskQuery().taskAssignee(userId).orderByTaskId().desc().list();
for (Task task: list) {
Map<String, Object> map = new HashMap<>();
// 提交給組長的時候,需要指定組長的 id
map.put("teamTask", teamId);
taskService.complete(task.getId(), map);
}
}審批:
/**
* 組長批準請假
*/
@Test
public void teamApprove() {
List<Task> list = taskService.createTaskQuery().taskAssignee(teamId).orderByTaskId().desc().list();
for (Task task: list) {
Map<String, Object> map = new HashMap<>();
//提交給組長的時候,需要指定組長的 id
map.put("manageTask", depId);
map.put("checkResult", "通過");
map.put("teamTask", teamId);
try {
taskService.complete(task.getId(), map);
} catch (Exception e) {
log.error("組長審批失敗{} {}", task.getId(), task.getAssignee(), e);
}
}
}
}4.總結
Flowable在功能豐富性和易用性之間取得了良好平衡,雖然表結構看似復雜,但通過合理配置可以大幅簡化。對于需要完整BPM功能又希望保持輕量集成的Java項目,它仍然是目前最好的選擇之一,非常適合Spring技術棧的項目快速整合工作流。新項目建議從6.7.x版本開始,并合理規(guī)劃歷史數(shù)據(jù)歸檔策略。
































