springboot集成flowable實踐
隨著企業(yè)對于業(yè)務流程管理需求的增加,流程引擎在企業(yè)信息化建設中的作用越來越重要。Flowable是一個開源的輕量級業(yè)務流程管理(BPM)和工作流引擎,它支持BPMN 2.0標準。
Flowable的一些特點:
- 安裝集成:Flowable可以與Spring Boot等現(xiàn)代開發(fā)框架集成,提供了一套完整的工具和API來支持工作流的建表、設計和實施。
- 流程設計:使用Flowable,開發(fā)者可以構建符合BPMN 2.0規(guī)范的流程定義,這些流程定義可以通過流程設計器進行可視化設計,也可以通過XML或JSON格式的文件直接編輯。
- 流程實例管理:Flowable允許用戶創(chuàng)建和管理流程實例,查詢實例狀態(tài),以及在運行時對流程實例進行干預,如啟動、暫?;蚪K止實例。
- 數(shù)據(jù)訪問:Flowable提供了訪問流程相關數(shù)據(jù)的API,允許應用程序在流程執(zhí)行過程中存取相關數(shù)據(jù)。
- 數(shù)據(jù)庫支持:Flowable支持多種數(shù)據(jù)庫,這使得它可以在不同的企業(yè)環(huán)境中靈活部署和使用。
- 社區(qū)文檔:作為一個開源項目,F(xiàn)lowable擁有一個活躍的社區(qū),提供豐富的文檔和案例,幫助開發(fā)者學習和解決問題。今天介紹的是springboot2.3版本集成flowable。
集成flowable
pom.xml中引入以下內容:
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-modeler</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-admin</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-idm</artifactId>
<version>${flowable.version}</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-ui-task</artifactId>
<version>${flowable.version}</version>
</dependency>
配置文件追加配置自動建表:
spring:
datasource:
url: jdbc:mysql://localhost:3306/tt?useSSL=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: root
password: pass
driverClassName: com.mysql.cj.jdbc.Driver
flowable:
async-executor-activate: false
database-schema-update: true
默認會啟動liquibase, 可關閉:
spring:
liquibase:
enabled: false
生成的表如下:

生成的表分別做什么用的
如果用過Activity的同學, 就會發(fā)現(xiàn)表名很多相同的, 但Flowable的表多了很多, 畢竟兩者早期是同一個產品。表的功能一般可以通過第二個詞語縮寫來進行區(qū)分。
Flowable 生成的數(shù)據(jù)表主要用于支撐工作流引擎的運行和管理。這些表中包括但不限于以下幾類:
- ACT_RE_: 以 “RE” 為前綴的表屬于倉庫服務(RepositoryService),它們主要存儲與流程定義相關的靜態(tài)信息,如流程定義、流程的資源(圖片、規(guī)則等)。
- ACT_RU_: 以 “RU” 為前綴的表屬于運行時服務(RuntimeService),存儲著流程實例在執(zhí)行期間的動態(tài)數(shù)據(jù),包括流程變量、用戶任務、變量、職責(job)等。
- ACT_ID_: 以 “ID” 為前綴的表與組織機構或用戶身份有關,涉及用戶、組和權限等信息。
- ACT_HI_: 是Flowable工作流引擎中使用的歷史數(shù)據(jù)表。
- ACT_GE_:主要用于存儲全局通用的數(shù)據(jù)和設置,這些數(shù)據(jù)在各種情況下都可能被使用
簡單使用flowable
- 用戶admin密碼test:



簡單繪制一個請假審批:
- 部門經理線的流條件:${day>1 && day<=3}
- 組長的流條件:${day<=1}
- 總經理的流條件:${day>3}

下載該文件命名為a.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: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" exporter="Flowable Open Source Modeler" exporterVersion="6.8.1">
<process id="a" name="a" isExecutable="true">
<startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
<userTask id="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7" name="組長" flowable:formFieldValidation="true"></userTask>
<exclusiveGateway id="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B"></exclusiveGateway>
<sequenceFlow id="sid-EA5097EB-7C3A-4295-86F5-8EFFC6F3E02A" sourceRef="startEvent1" targetRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B"></sequenceFlow>
<userTask id="sid-96F84EB8-DB79-4092-89A5-141313627020" name="部門經理" flowable:formFieldValidation="true"></userTask>
<userTask id="sid-09FB7A15-7582-4785-B635-05E745D56438" name="總經理" flowable:formFieldValidation="true"></userTask>
<endEvent id="sid-CF77490C-2721-4269-9A2C-301CD4DD6F15"></endEvent>
<sequenceFlow id="sid-38EF250C-CF01-414A-BDC3-A0E2CBAB9175" sourceRef="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7" targetRef="sid-CF77490C-2721-4269-9A2C-301CD4DD6F15"></sequenceFlow>
<endEvent id="sid-F9DE2B7F-F2B4-4D7A-872C-D620EC006C80"></endEvent>
<sequenceFlow id="sid-0537C179-756A-4F54-AF48-BCD3669EC88B" sourceRef="sid-96F84EB8-DB79-4092-89A5-141313627020" targetRef="sid-F9DE2B7F-F2B4-4D7A-872C-D620EC006C80"></sequenceFlow>
<endEvent id="sid-6224E896-E5DF-4AD8-9C5C-0BB6BBB8125C"></endEvent>
<sequenceFlow id="sid-B4C4F1DA-257B-4206-950A-133010C19C4A" sourceRef="sid-09FB7A15-7582-4785-B635-05E745D56438" targetRef="sid-6224E896-E5DF-4AD8-9C5C-0BB6BBB8125C"></sequenceFlow>
<sequenceFlow id="sid-669C693E-E02E-47A1-B8EF-0F6DC4AEF61C" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-96F84EB8-DB79-4092-89A5-141313627020">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${day>1 && day<=3}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="sid-2498854B-52F3-4FCE-9994-B5ED2DC350E4" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${day<=1}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="sid-9D85E323-099E-4F30-861E-0DED4491C1B8" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-09FB7A15-7582-4785-B635-05E745D56438">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${day}>3]]></conditionExpression>
</sequenceFlow>
</process>
</definitions>
部署這個a.bpmn20.xml文件:
@Test
void test01() {
// 配置數(shù)據(jù)庫相關信息 獲取 ProcessEngineConfiguration
ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
.setJdbcUrl("jdbc:mysql://localhost:3306/tt?serverTimezone=UTC&nullCatalogMeansCurrent=true")
.setJdbcUsername("root")
.setJdbcDriver("com.mysql.cj.jdbc.Driver")
.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
// 獲取流程引擎對象
ProcessEngine processEngine = cfg.buildProcessEngine();
// 部署流程 獲取RepositoryService對象
RepositoryService repositoryService = processEngine.getRepositoryService();
Deployment deployment = repositoryService.createDeployment()// 創(chuàng)建Deployment對象
.addClasspathResource("a.bpmn20.xml") // 添加流程部署文件
.name("請假") // 設置部署流程的名稱
.deploy(); // 執(zhí)行部署操作
}
測試執(zhí)行:
Map<String, Object> variables = new HashMap<>();
//days是流程圖條件
variables.put("day", 3);
ProcessInstance pi = runtimeService.startProcessInstanceByKey("a", variables);
System.out.println(pi.getName());
我們分別調整day為1,3,5時,此時ACT_RU_TASK表(ACT_RU_TASK表存儲了運行時的用戶任務數(shù)據(jù))中將有三條數(shù)據(jù):

總結
以上為個人經驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關文章
GateWay路由規(guī)則與動態(tài)路由詳細介紹
這篇文章主要介紹了GateWay路由規(guī)則與GateWay動態(tài)路由,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2022-09-09
java正則表達式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字的示例
這篇文章主要介紹了java正則表達式匹配網(wǎng)頁所有網(wǎng)址和鏈接文字java正則表達式匹配,需要的朋友可以參考下2014-03-03
Spring Cloud Gateway 獲取請求體(Request Body)的多種方法
這篇文章主要介紹了Spring Cloud Gateway 獲取請求體(Request Body)的多種方法,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-01-01
Java中隊列Queue和Deque的區(qū)別與代碼實例
學過數(shù)據(jù)結構的,一定對隊列不陌生,java也實現(xiàn)了隊列,下面這篇文章主要給大家介紹了關于Java中隊列Queue和Deque區(qū)別的相關資料,需要的朋友可以參考下2021-08-08

