1. 創建 Spring Boot 項目
通過 Spring Initializr(https://start.spring.io/ )創建一個基礎的 Spring Boot 項目,添加以下依賴:
- Spring Web
- Spring Data JPA
- MySQL Driver
- Lombok(可選,用于簡化代碼)
2. 添加 Flowable 依賴
在 pom.xml
中添加 Flowable 相關依賴:
<dependencies><!-- Spring Boot Starter for Flowable --><dependency><groupId>org.flowable</groupId><!--引入flowable基礎功能 自動創建46張表--><artifactId>flowable-spring-boot-starter-basic</artifactId><!--引入flowable所有功能 自動創建79張表-->
<!-- <artifactId>flowable-spring-boot-starter</artifactId>--><!-- 根據需要選擇版本 --><version>6.8.0</version></dependency><!-- MySQL Driver --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!-- Spring Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>
3. 配置 MySQL 數據庫連接
在 application.properties
或 application.yml
中配置 MySQL 數據庫連接信息。以下是 application.yml
的示例:
server:port: 8080
spring:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=trueusername: rootpassword: xxxx
4. 完整配置 Flowable
在 application.yml
中添加完整的 Flowable 相關配置:
flowable:# 數據庫模式更新策略,可選值:false, true, create-drop, drop-create,生產環境建議falsedatabase-schema-update: trueactivity-font-name: 宋體label-font-name: 宋體annotation-font-name: 宋體process:# 流程定義緩存中保存流程定義的最大數量。默認值為-1(緩存所有流程定義)。definition-cache-limit: -1# 禁用異步執行器,開發和測試階段可這樣配置async-executor-activate: false# 歷史數據級別,可選值:none, activity, audit, fullhistory-level: full# 是否自動檢查并部署流程文件,設置為false需要手動部署流程文件check-process-definitions: true
flowable.history-level
配置項用于指定 Flowable 工作流引擎的歷史數據記錄級別。不同的歷史數據級別決定了 Flowable 在流程執行過程中記錄哪些歷史信息,這對于流程監控、審計和分析等操作非常重要。
-
none
當flowable.history-level=none
時,Flowable 工作流引擎不會記錄任何歷史數據。也就是說,在流程執行過程中,不會保存流程實例、任務、活動等相關的歷史信息。這種配置適用于對歷史數據沒有需求,只關注流程的實時執行,并且希望減少數據庫存儲壓力和提高性能的場景。例如,一些臨時性的、簡單的流程,不需要對執行過程進行追溯和分析。 -
activity
若設置flowable.history-level=activity
,Flowable 會記錄流程活動的基本歷史信息。具體來說,會記錄每個流程實例中活動(如任務、網關等)的開始和結束時間,以及活動的狀態信息。但不會記錄流程變量、任務的詳細信息(如任務的分配、完成時間等)。這種配置適用于只需要了解流程活動的大致執行情況,而不需要詳細的任務和變量信息的場景。例如,用于監控流程的整體執行進度,查看哪些活動已經完成,哪些還在進行中。 -
audit
當配置為flowable.history-level=audit
時,Flowable 會記錄更詳細的歷史信息,用于審計目的。除了記錄活動的開始和結束時間外,還會記錄任務的分配信息、任務的完成時間、流程變量的更新情況等。這些信息可以幫助管理員或審計人員了解流程的執行過程,追蹤任務的處理情況和變量的變化。例如,在一個請假流程中,可以查看每個審批任務是由誰處理的,處理時間是什么時候,以及請假天數等變量在流程執行過程中是否有修改。 -
full
設置flowable.history-level=full
會記錄最完整的歷史數據。除了包含audit
級別的信息外,還會記錄更多的細節,如活動的所有事件(如活動的創建、取消等)、任務的注釋、流程實例的啟動和結束原因等。這種配置適用于需要對流程進行全面追溯和分析的場景,例如進行流程優化、合規性檢查等。通過完整的歷史數據,可以深入了解流程的執行細節,發現潛在的問題和瓶頸。。
5. 創建 Flowable 流程定義文件
在 src/main/resources/processes
目錄下創建 BPMN 流程定義文件,例如 leave-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: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"id="Definitions_1"targetNamespace="http://www.flowable.org/processdef"><process id="leaveRequestProcess" name="Leave Request Process" isExecutable="true"><startEvent id="startEvent1"></startEvent><userTask id="approveTask" name="Approve Leave Request" flowable:assignee="manager"></userTask><endEvent id="endEvent1"></endEvent><sequenceFlow id="flow1" sourceRef="startEvent1" targetRef="approveTask"></sequenceFlow><sequenceFlow id="flow2" sourceRef="approveTask" targetRef="endEvent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_1"><bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_1"><bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1"><omgdc:Bounds height="36.0" width="36.0" x="173.0" y="102.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="approveTask" id="BPMNShape_approveTask"><omgdc:Bounds height="80.0" width="100.0" x="325.0" y="78.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endEvent1" id="BPMNShape_endEvent1"><omgdc:Bounds height="36.0" width="36.0" x="501.0" y="102.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="209.0" y="120.0"></omgdi:waypoint><omgdi:waypoint x="325.0" y="118.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="425.0" y="118.0"></omgdi:waypoint><omgdi:waypoint x="501.0" y="120.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>
6. 創建服務類啟動流程實例
創建一個服務類來啟動流程實例:
import org.flowable.engine.RuntimeService;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;@Service
public class LeaveRequestService {private final RuntimeService runtimeService;public LeaveRequestService(RuntimeService runtimeService) {this.runtimeService = runtimeService;}public String startLeaveRequestProcess() {Map<String, Object> variables = new HashMap<>();variables.put("employee", "John Doe");variables.put("leaveDays", 5);return runtimeService.startProcessInstanceByKey("leaveRequestProcess", variables).getId();}
}
7. 創建控制器測試流程啟動
創建一個控制器來測試流程啟動:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class LeaveRequestController {@Autowiredprivate LeaveRequestService leaveRequestService;@GetMapping("/start-leave-request")public String startLeaveRequest() {return "Process instance started with ID: " + leaveRequestService.startLeaveRequestProcess();}
}
8. 啟動項目
啟動 Spring Boot 項目后, leave-request.bpmn20.xml
文件會自動部署,可以在act_re_procdef
,act_re_deployment
表中查看流程定義的相關信息。
訪問 http://localhost:8080/startLeaveRequest
來啟動請假流程實例。可以在act_ru_task
表中查看正在運行的流程實例