SpringBoot 集成 Camunda 流程引擎,實(shí)現(xiàn)一套完整的業(yè)務(wù)流程
項(xiàng)目中需要用到工作流引擎來(lái)設(shè)計(jì)部分業(yè)務(wù)流程,框架選型最終選擇了 Camunda7,關(guān)于 Camunda以及 Activity 等其他工作流 引擎的介紹及對(duì)比不再介紹,這里只介紹與現(xiàn)有Springboot項(xiàng)目的集成以及具體使用及配置
概念
- 流程(PROCESS): 通過(guò)工具建模最終生成的BPMN文件,里面有整個(gè)流程的定義
- 流程實(shí)例(Instance):流程啟動(dòng)后的實(shí)例
- 流程變量(Variables):流程任務(wù)之間傳遞的參數(shù)
- 任務(wù)(TASK):流程中定義的每一個(gè)節(jié)點(diǎn)
- 流程部署:將之前流程定義的.bpmn文件部署到工作流平臺(tái)
核心組件
- Process Engine-流程引擎
- Web Applicatons- 基于web的管理頁(yè)面
API介紹
官方文檔
下面是官網(wǎng)的一些文檔,有時(shí)間可以看看,下面說(shuō)一些核心的東西。
圖片
ProcessEngine
為流程引擎,可以通過(guò)他獲取相關(guān)service,里面集成了很多相關(guān)service,默認(rèn)實(shí)現(xiàn)如下:
圖片
RepositoryService
此服務(wù)提供用于管理和操作部署和流程定義的操作,使用camunda的第一要?jiǎng)?wù)
RuntimeService
運(yùn)行相關(guān),啟動(dòng)流程實(shí)例、刪除、搜索等
TaskService
所有圍繞任務(wù)相關(guān)的操作,如完成、分發(fā)、認(rèn)領(lǐng)等
HistoryService
提供引擎搜集的歷史數(shù)據(jù)服務(wù)
IdentityService
用戶相關(guān),實(shí)際中用不太到
Springboot集成
依賴集成
maven
https://mvnrepository.com/search?q=org.camunda.bpm.springboot
可以根據(jù)需要引用版本,我這邊用的是 7.18
需要3個(gè)maven依賴,分別是對(duì)應(yīng) 流程引擎、Web管理平臺(tái)、提供rest api操作接口包
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter</artifactId>
<version>7.18.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
<version>7.18.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>7.18.0</version>
</dependency>
數(shù)據(jù)庫(kù)
我這邊使用的是mysql,建了個(gè)新庫(kù) camunda(可自定義),啟動(dòng)后會(huì)自動(dòng)生成所需表結(jié)構(gòu)
POM文件
<?xml versinotallow="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.3</version>
<relativePath/><!-- lookup parent from repository -->
</parent>
<groupId>com.example</groupId>
<artifactId>camunda-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>camunda-demo</name>
<description>camunda-demo</description>
<properties>
<java.version>17</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter</artifactId>
<version>7.18.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-rest</artifactId>
<version>7.18.0</version>
</dependency>
<dependency>
<groupId>org.camunda.bpm.springboot</groupId>
<artifactId>camunda-bpm-spring-boot-starter-webapp</artifactId>
<version>7.18.0</version>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.32</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
application.yml
server:
port:8081
## camunda登錄信息配置
camunda.bpm:
admin-user:
id:admin##用戶名
password:123456##密碼
firstName:yu
filter:
create:Alltasks
## mysql連接信息
spring:
datasource:
driver-class-name:com.mysql.cj.jdbc.Driver
url:jdbc:mysql://localhost:8101/camunda
username:root
password:123456
type:com.mysql.cj.jdbc.MysqlDataSource
啟動(dòng)效果
準(zhǔn)備好前置工作,啟動(dòng)后效果如下:
圖片
數(shù)據(jù)庫(kù)表結(jié)構(gòu)
啟動(dòng)后自動(dòng)生成的表結(jié)構(gòu)如下:
大概有這么幾個(gè)表模塊,重要的詳細(xì)介紹下:
- ACT_ID_
這部分表示用戶模塊,配置文件里面的用戶,信息就在此模塊
關(guān)注公眾號(hào):碼猿技術(shù)專欄,回復(fù)關(guān)鍵詞:1111 獲取阿里內(nèi)部Java性能調(diào)優(yōu)手冊(cè)!
圖片
- ACT_HI_
表示流程歷史記錄
act_hi_actinst
:執(zhí)行的活動(dòng)歷史act_hi_taskinst
:執(zhí)行任務(wù)歷史act_hi_procinst
:執(zhí)行流程實(shí)例歷史act_hi_varinst
:流程變量歷史表- ACT_RE_
表示流程資源存儲(chǔ)
act_re_procdef
:流程定義存儲(chǔ)act_re_deployment
: 自動(dòng)部署,springboot每次啟動(dòng)都會(huì)重新部署,生成記錄- ACT_RU_
表示流程運(yùn)行時(shí)表數(shù)據(jù),流程結(jié)束后會(huì)刪除
act_ru_execution
:運(yùn)行時(shí)流程實(shí)例act_ru_task
:運(yùn)行時(shí)的任務(wù)act_ru_variable
:運(yùn)行時(shí)的流程變量- ACT_GE_
流程通用數(shù)據(jù)
act_ge_bytearray
:每次部署的文件2進(jìn)制數(shù)據(jù),所以如果文件修改后,重啟也沒(méi)用,因?yàn)橹匦律闪擞涗洠枰宓魯?shù)據(jù)庫(kù),或者這個(gè)表記錄
登錄界面
登錄地址為 http://localhost:8081/
,輸入用戶名密碼即為配置文件里面的 admin,123456
圖片
主控制臺(tái)
登陸成功后,如下所示,具體的使用在下面介紹
具體業(yè)務(wù)集成
繪制流程圖
下載
首先需要一個(gè)工具 Camunda Modeler 來(lái)畫(huà),下載地址:https://camunda.com/download/modeler/
解壓縮后打開(kāi)如下:
圖片
繪制
新建一個(gè):
圖片
我這邊稍微畫(huà)了一個(gè),具體怎么畫(huà),就不在細(xì)說(shuō)了,最后效果如下,模擬了個(gè)OA的流程
圖片
任務(wù)分類
只介紹最常用的兩種
- 用戶任務(wù) (User Task)
圖片
具體來(lái)說(shuō)就是需要手動(dòng)執(zhí)行的任務(wù),即需要我們這變寫(xiě)完業(yè)務(wù)代碼后,調(diào)用代碼
taskService.complete(taskId, variables);
才會(huì)完成的任務(wù)
- 系統(tǒng)任務(wù)(Service Task)
圖片
系統(tǒng)會(huì)自動(dòng)幫我們完成的任務(wù)
網(wǎng)關(guān)
分為這么幾類,會(huì)根據(jù)我們傳入的流程變量及設(shè)定的條件走
圖片
- 排他網(wǎng)關(guān)(exclusive gateway)
這個(gè)網(wǎng)關(guān)只會(huì)走一個(gè),我們走到這個(gè)網(wǎng)關(guān)時(shí),會(huì)從上到下找第一個(gè)符合條件的任務(wù)往下走
- 并行網(wǎng)關(guān)(Parallel Gateway)
這個(gè)網(wǎng)關(guān)不需要設(shè)置條件,會(huì)走所有的任務(wù)
- 包含網(wǎng)關(guān)(Inclusive Gateway)
這個(gè)網(wǎng)關(guān)會(huì)走一個(gè)或者多個(gè)符合條件的任務(wù)
示例
圖片
如上圖包含網(wǎng)關(guān),需要在網(wǎng)關(guān)的連線初設(shè)置表達(dá)式 condition,參數(shù)來(lái)自于流程變量
兩個(gè)參數(shù):
switch2d 、 switch3d
- 如果 都為true,則走任務(wù)1,3
- 如果 switch2d 為true switch3d為false,則只走任務(wù)1
- 如果 switch3d 為true switch2d為false,則只走任務(wù)3
- 如果都為false,則直接走網(wǎng)關(guān),然后結(jié)束
引入項(xiàng)目
將畫(huà)好的流程圖保存文件為 test_1.bpmn,在剛才的springboot項(xiàng)目中resources新建一個(gè)bpmn文件夾,放進(jìn)去,
圖片
重啟項(xiàng)目,發(fā)現(xiàn)web界面中已經(jīng)被集成進(jìn)來(lái)了
圖片
具體開(kāi)發(fā)
寫(xiě)幾個(gè)測(cè)試controller和service
controller
圖片
service
public void startProcess() {
ProcessInstance instance = runtimeService.startProcessInstanceByKey("key");
System.out.println(instance.toString());
}
public List<ProcessDefinition> findProcesses() {
return repositoryService.createProcessDefinitionQuery().list();
}
public List<Task> findTasks() {
return taskService.createTaskQuery().list();
}
啟動(dòng)流程成功,說(shuō)明問(wèn)題不大,接下來(lái)詳細(xì)業(yè)務(wù)改進(jìn)。
下一篇介紹詳細(xì)的業(yè)務(wù)集成及各種API(變量傳遞、自動(dòng)任務(wù))的使用
API使用
流程相關(guān)API
創(chuàng)建流程:
會(huì)同時(shí)創(chuàng)建第一個(gè)任務(wù)
ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, params);
暫停流程:
流程暫停后,再執(zhí)行相關(guān)任務(wù)會(huì)報(bào)錯(cuò),需要先重新激活任務(wù)
runtimeService.suspendProcessInstanceById(instance.getId());
重新激活流程:
runtimeService.activateProcessInstanceById(instance.getId());
刪除流程:
會(huì)同時(shí)刪除任務(wù)
runtimeService.deleteProcessInstance(instance.getId(), "手動(dòng)刪除");
圖片
以上都可以在流程歷史表 act_hi_procinst
里查詢
任務(wù)相關(guān)API
基于service的查詢類,都可先構(gòu)建一個(gè) query,然后在附上查詢條件,實(shí)例幾個(gè)。
List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan").list();
List<ProcessInstance> instances = runtimeService.createProcessInstanceQuery().listPage(1, 10);
查詢歷史任務(wù)
List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().list();
查詢當(dāng)前任務(wù)/分頁(yè)
List<Task> list = taskService.createTaskQuery().orderByTaskCreateTime().desc().list();
任務(wù)回退
大體思路是拿到當(dāng)前的任務(wù),及當(dāng)前任務(wù)的上一個(gè)歷史任務(wù),然后重啟
代碼示例:
Task activeTask = taskService.createTaskQuery()
.taskId(taskId)
.active()
.singleResult();
List<HistoricTaskInstance> historicTaskInstance = historyService.createHistoricTaskInstanceQuery()
.processInstanceId(instanceId)
.orderByHistoricActivityInstanceStartTime()
.desc()
.list();
List<HistoricTaskInstance> historicTaskInstances = historicTaskInstance.stream().filter(v -> !v.getTaskDefinitionKey().equals(activeTask.getTaskDefinitionKey())).toList();
Assert.notEmpty(historicTaskInstances, "當(dāng)前已是初始任務(wù)!");
HistoricTaskInstance curr = historicTaskInstances.get(0);
runtimeService.createProcessInstanceModification(instanceId)
.cancelAllForActivity(activeTask.getTaskDefinitionKey())
.setAnnotation("重新執(zhí)行")
.startBeforeActivity(curr.getTaskDefinitionKey())
.execute();
流程變量
包括流程中產(chǎn)生的變量信息,包括控制流程流轉(zhuǎn)的變量,網(wǎng)關(guān)、業(yè)務(wù)表單中填寫(xiě)的流程需要用到的變量等。很多地方都要用到
流程變量變量傳遞
變量最終會(huì)存在 act_ru_variable
這個(gè)表里面
在繪制流程圖的時(shí)候,如果是用戶任務(wù)(userService) 可以設(shè)置變量,比如執(zhí)行人,
圖片
寫(xiě)法有這么幾種方式
- 寫(xiě)死,就比如 zhangsan
- 表達(dá)式,比如上面寫(xiě)的
${user}
,這種需要傳入?yún)?shù),其實(shí)就是啟動(dòng)參數(shù)的時(shí)候傳入,傳入?yún)?shù),可選值為一個(gè)Map<String, Object>
,之后的流程可查看次參數(shù),上面寫(xiě)的是 user, 所以map里面的key需要帶著user,不然會(huì)報(bào)錯(cuò)。
關(guān)于擴(kuò)展變量,可在流程圖繪制這么設(shè)定,傳遞方式還是一樣,流程圖里面在下面寫(xiě):
圖片
代碼:
ProcessInstance instance = runtimeService.startProcessInstanceByKey(key, new HashMap<>());
變量設(shè)置:
runtimeService.setVariable(instance.getId(), Constants.PATIENT_ID, relatedId);
變量查詢:
Object variable = runtimeService.getVariable(instance.getId(), Constants.GENERAL_ID);
歷史變量查詢:
HistoricVariableInstance variableInstance = historyService.createHistoricVariableInstanceQuery().processInstanceId(bo.getId().toString()).
variableName(Constants.PATIENT_ID).singleResult();
//變量值
variableInstance.getValue();
//變量名稱
variableInstance.getName();
針對(duì)后端來(lái)說(shuō)任務(wù)類型主要有兩種。
用戶任務(wù)-userTask
即需要用戶參與的任務(wù),因?yàn)楣ぷ髁鲌?zhí)行過(guò)程中需要涉及到審批、過(guò)審之類的需要用戶參與的任務(wù),這個(gè)時(shí)候需要用戶參與,然后調(diào)用接口完成任務(wù)。
服務(wù)任務(wù)-serviceTask
即自動(dòng)執(zhí)行的任務(wù),比如用戶提交后,系統(tǒng)自動(dòng)存儲(chǔ)、修改狀態(tài)等自動(dòng)完成的任務(wù)。
Type
任務(wù)類型是關(guān)鍵,可根據(jù)配型配置實(shí)現(xiàn)調(diào)用 java的方法,spring 的bean方法,等等有這么幾種類型
圖片
推薦使用 -- Delegate Expression !!!
在系統(tǒng)任務(wù)中,因?yàn)槭亲詣?dòng)執(zhí)行,所以實(shí)際應(yīng)用中需要嵌入各種業(yè)務(wù)邏輯,可以在流程圖設(shè)計(jì)中,按照下面方式調(diào)用java代碼執(zhí)行,在spring中配置同名的bean
圖片
配置表達(dá)式,可以實(shí)現(xiàn)JavaDelegate接口使用類名配置,快捷寫(xiě)法如下,比較推薦下面這種,此種可靈活配置bean和spring結(jié)合使用,注入service等業(yè)務(wù)方法
@Bean("t17")
JavaDelegate t17() {
return execution -> {
Map<String, Object> variables = execution.getVariables();
Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
//業(yè)務(wù)邏輯
task.setOwner(String.valueOf(dentistId));
};
}
Java Class :
配置java類名,需要實(shí)現(xiàn)JavaDelegate接口,注意是全路徑名,不可以使用Spring的bean配置?。?!
@Component
public class T17Delegate implements JavaDelegate {
@Override
public void execute(DelegateExecution execution) throws Exception {
String taskId = execution.getId();
String instanceId = execution.getProcessInstanceId();
Map<String, Object> variables = execution.getVariables();
}
}
下面兩種可使用spring的配置
Expression:
EL表達(dá)式,調(diào)用java類的方法 ,規(guī)范:
expressinotallow=“##{monitorExecution.execution(execution)}”
@Component("monitorExecution")
public class MonitorExecution {
public void execution(DelegateExecution execution){
String processInstanceId = execution.getProcessInstanceId();
}
}
任務(wù)監(jiān)聽(tīng)器 - Task Listener
任務(wù)監(jiān)聽(tīng)器用于在某個(gè)與任務(wù)相關(guān)的事件發(fā)生時(shí)執(zhí)行自定義Java邏輯或表達(dá)式。它只能作為用戶任務(wù)的子元素添加到流程定義中。
請(qǐng)注意,這也必須作為BPMN 2.0擴(kuò)展元素的子級(jí)和Camunda命名空間中發(fā)生,因?yàn)槿蝿?wù)偵聽(tīng)器是專門為Camunda引擎構(gòu)建的。
適用場(chǎng)景:
@Bean
TaskListener t21() {
return delegateTask -> {
String taskId = delegateTask.getId();
String instanceId = delegateTask.getProcessInstanceId();
Map<String, Object> variables = delegateTask.getVariables();
// TODO: 20log/3/22
delegateTask.setVariable("", "");
};
}
執(zhí)行監(jiān)聽(tīng)器 - Execution Listener
執(zhí)行偵聽(tīng)器在流程執(zhí)行過(guò)程中發(fā)生某些事件時(shí)執(zhí)行外部Java代碼或計(jì)算表達(dá)式。可以用在任何任務(wù)中,可以捕獲的事件有:
- 流程實(shí)例的開(kāi)始和結(jié)束。
- 進(jìn)行過(guò)渡。
- 活動(dòng)的開(kāi)始和結(jié)束。
- 網(wǎng)關(guān)的開(kāi)始和結(jié)束。
- 中間事件的開(kāi)始和結(jié)束。
- 結(jié)束開(kāi)始事件或開(kāi)始結(jié)束事件
適用場(chǎng)景:每個(gè)任務(wù)結(jié)束時(shí)設(shè)置任務(wù)進(jìn)度
public class ExampleExecutionListenerOne implements ExecutionListener {
public void notify(DelegateExecution execution) throws Exception {
execution.setVariable("variableSetInExecutionListener", "firstValue");
execution.setVariable("eventReceived", execution.getEventName());
}
}
擴(kuò)展屬性- Extension properties
擴(kuò)展屬性適用于很多自定義的業(yè)務(wù)屬性,比如設(shè)置業(yè)務(wù)流程進(jìn)度
圖片
流程權(quán)限及創(chuàng)建人設(shè)置
IdentityService為鑒權(quán)相關(guān)服務(wù),但是我們實(shí)際開(kāi)發(fā)中,一般會(huì)用到我們自己的鑒權(quán)系統(tǒng),所以可以使用camunda提供的api來(lái)設(shè)置,具體可以看IdentityServiceImpl這個(gè)類,其中也是使用了ThreadLocal來(lái)保存鑒權(quán)信息 ,代碼在下面
private ThreadLocal<Authentication> currentAuthentication = new ThreadLocal<Authentication>();
用戶信息設(shè)置:
// Userutil是我們自己封裝的用戶工具類
identityService.setAuthenticatedUserId(UserUtil.getUserId().toString());
//獲取
Authentication authentication = identityService.getCurrentAuthentication();
他內(nèi)置很多比如開(kāi)啟流程時(shí)候,會(huì)默認(rèn)找當(dāng)前登錄的人,這個(gè)類DefaultHistoryEventProducer
// set super process instance id
ExecutionEntity superExecution = executionEntity.getSuperExecution();
if (superExecution != null) {
evt.setSuperProcessInstanceId(superExecution.getProcessInstanceId());
}
//state
evt.setState(HistoricProcessInstance.STATE_ACTIVE);
// set start user Id
evt.setStartUserId(Context.getCommandContext().getAuthenticatedUserId());
任務(wù)執(zhí)行人及發(fā)起人設(shè)置
//根據(jù)任務(wù)id設(shè)置執(zhí)行人
taskService.setAssignee(task.getId(), UserUtil.getUserId().toString());