概念
核心組件
API介紹
Springboot集成
具體業(yè)務(wù)集成
API使用
前言
項目中需要用到工作流引擎來設(shè)計部分業(yè)務(wù)流程,框架選型最終選擇了 Camunda7,關(guān)于 Camunda以及 Activity 等其他工作流 引擎的介紹及對比不再介紹,這里只介紹與現(xiàn)有Springboot項目的集成以及具體使用及配置
基于 Spring Boot + MyBatis Plus + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
項目地址:https://github.com/YunaiV/ruoyi-vue-pro
視頻教程:https://doc.iocoder.cn/video/
概念
流程(PROCESS) : 通過工具建模最終生成的BPMN文件,里面有整個流程的定義
流程實例(Instance) :流程啟動后的實例
流程變量(Variables) :流程任務(wù)之間傳遞的參數(shù)
任務(wù)(TASK) :流程中定義的每一個節(jié)點
流程部署 :將之前流程定義的.bpmn文件部署到工作流平臺
基于 Spring Cloud Alibaba + Gateway + Nacos + RocketMQ + Vue & Element 實現(xiàn)的后臺管理系統(tǒng) + 用戶小程序,支持 RBAC 動態(tài)權(quán)限、多租戶、數(shù)據(jù)權(quán)限、工作流、三方登錄、支付、短信、商城等功能
項目地址:https://github.com/YunaiV/yudao-cloud
視頻教程:https://doc.iocoder.cn/video/
核心組件
Process Engine -流程引擎
Web Applicatons - 基于web的管理頁面
API介紹
官方文檔
https://docs.camunda.org/manual/7.18/user-guide/process-engine/process-engine-api/
下面是官網(wǎng)的一些文檔,有時間可以看看,下面說一些核心的東西。

ProcessEngine
為流程引擎,可以通過他獲取相關(guān)service,里面集成了很多相關(guān)service,默認(rèn)實現(xiàn)如下:

RepositoryService
此服務(wù)提供用于管理和操作部署和流程定義的操作,使用camunda的第一要務(wù)
RuntimeService
運行相關(guān),啟動流程實例、刪除、搜索等
TaskService
所有圍繞任務(wù)相關(guān)的操作,如完成、分發(fā)、認(rèn)領(lǐng)等
HistoryService
提供引擎搜集的歷史數(shù)據(jù)服務(wù)
IdentityService
用戶相關(guān),實際中用不太到
Springboot集成
依賴集成
maven
https://mvnrepository.com/search?q=org.camunda.bpm.springboot
可以根據(jù)需要引用版本,我這邊用的是 7.18
需要3個maven依賴,分別是對應(yīng) 流程引擎、Web管理平臺、提供rest api操作接口包
org.camunda.bpm.springboot camunda-bpm-spring-boot-starter 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-rest 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-webapp 7.18.0
數(shù)據(jù)庫
我這邊使用的是mysql,建了個新庫 camunda(可自定義),啟動后會自動生成所需表結(jié)構(gòu)
POM文件
4.0.0 org.springframework.boot spring-boot-starter-parent 2.7.3 com.example camunda-demo 0.0.1-SNAPSHOT camunda-demo camunda-demo 17 org.springframework.boot spring-boot-starter org.camunda.bpm.springboot camunda-bpm-spring-boot-starter 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-rest 7.18.0 org.camunda.bpm.springboot camunda-bpm-spring-boot-starter-webapp 7.18.0 mysql mysql-connector-java 8.0.32 org.springframework.boot spring-boot-starter-test test org.springframework.boot spring-boot-maven-plugin
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//localhost:8101/camunda username:root password:123456 type:com.mysql.cj.jdbc.MysqlDataSource
啟動效果
準(zhǔn)備好前置工作,啟動后效果如下:

數(shù)據(jù)庫表結(jié)構(gòu)
啟動后自動生成的表結(jié)構(gòu)如下

大概有這么幾個表模塊,重要的詳細(xì)介紹下:
ACT_ID_
這部分表示用戶模塊,配置文件里面的用戶,信息就在此模塊

ACT_HI_
表示流程歷史記錄
act_hi_actinst: 執(zhí)行的活動歷史
act_hi_taskinst:執(zhí)行任務(wù)歷史
act_hi_procinst:執(zhí)行流程實例歷史
act_hi_varinst:流程變量歷史表
ACT_RE_
表示流程資源存儲
act_re_procdef:流程定義存儲
act_re_deployment: 自動部署,springboot每次啟動都會重新部署,生成記錄
ACT_RU_
表示流程運行時表數(shù)據(jù),流程結(jié)束后會刪除
act_ru_execution:運行時流程實例
act_ru_task:運行時的任務(wù)
act_ru_variable:運行時的流程變量
ACT_GE_
流程通用數(shù)據(jù)
act_ge_bytearray:每次部署的文件2進(jìn)制數(shù)據(jù),所以如果文件修改后,重啟也沒用,因為重新生成了記錄,需要清掉數(shù)據(jù)庫,或者這個表記錄
登錄界面
登錄地址為 http://localhost:8081/,輸入用戶名密碼即為配置文件里面的 admin,123456

主控制臺
登陸成功后,如下所示,具體的使用在下面介紹

具體業(yè)務(wù)集成
繪制流程圖
下載
首先需要一個工具 Camunda Modeler 來畫,下載地址:
https://camunda.com/download/modeler/

解壓縮后打開如下:

繪制
新建一個

我這邊稍微畫了一個,具體怎么畫,就不在細(xì)說了,最后效果如下,模擬了個OA的流程

任務(wù)分類
只介紹最常用的兩種
用戶任務(wù) (User Task)

具體來說就是需要手動執(zhí)行的任務(wù),即需要我們這變寫完業(yè)務(wù)代碼后,調(diào)用代碼
taskService.complete(taskId,variables);
才會完成的任務(wù)
系統(tǒng)任務(wù)(Service Task)

系統(tǒng)會自動幫我們完成的任務(wù)
網(wǎng)關(guān)
分為這么幾類,會根據(jù)我們傳入的流程變量及設(shè)定的條件走

排他網(wǎng)關(guān)(exclusive gateway)
這個網(wǎng)關(guān)只會走一個,我們走到這個網(wǎng)關(guān)時,會從上到下找第一個符合條件的任務(wù)往下走
并行網(wǎng)關(guān)(Parallel Gateway)
這個網(wǎng)關(guān)不需要設(shè)置條件,會走所有的任務(wù)
包含網(wǎng)關(guān)(Inclusive Gateway)
這個網(wǎng)關(guān)會走一個或者多個符合條件的任務(wù)
示例

如上圖包含網(wǎng)關(guān),需要在網(wǎng)關(guān)的連線初設(shè)置表達(dá)式 condition,參數(shù)來自于流程變量
兩個參數(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é)束
引入項目
將畫好的流程圖保存文件為 test_1.bpmn,在剛才的springboot項目中resources新建一個bpmn文件夾,放進(jìn)去,

重啟項目,發(fā)現(xiàn)web界面中已經(jīng)被集成進(jìn)來了

具體開發(fā)
寫幾個測試controller和service
controller

service
publicvoidstartProcess(){
ProcessInstanceinstance=runtimeService.startProcessInstanceByKey("key");
System.out.println(instance.toString());
}
publicListfindProcesses(){
returnrepositoryService.createProcessDefinitionQuery().list();
}
publicListfindTasks(){
returntaskService.createTaskQuery().list();
}
啟動流程成功,說明問題不大,接下來詳細(xì)業(yè)務(wù)改進(jìn)。
下一篇介紹詳細(xì)的業(yè)務(wù)集成及各種API(變量傳遞、自動任務(wù))的使用
API使用
流程相關(guān)API
創(chuàng)建流程:
會同時創(chuàng)建第一個任務(wù)
ProcessInstanceinstance=runtimeService.startProcessInstanceByKey(processKey,params);
暫停流程
流程暫停后,再執(zhí)行相關(guān)任務(wù)會報錯,需要先重新激活任務(wù)
runtimeService.suspendProcessInstanceById(instance.getId());
重新激活流程
runtimeService.activateProcessInstanceById(instance.getId());
刪除流程
會同時刪除任務(wù)
runtimeService.deleteProcessInstance(instance.getId(),"手動刪除");

以上都可以在流程歷史表 act_hi_procinst 里查詢
任務(wù)相關(guān)API
基于service的查詢類,都可先構(gòu)建一個 query,然后在附上查詢條件,實例幾個
Listlist=repositoryService.createProcessDefinitionQuery().list(); List list=taskService.createTaskQuery().taskAssignee("zhangsan").list(); List instances=runtimeService.createProcessInstanceQuery().listPage(1,10);
查詢歷史任務(wù)
Listlist=historyService.createHistoricProcessInstanceQuery().list();
查詢當(dāng)前任務(wù)/分頁
Listlist=taskService.createTaskQuery().orderByTaskCreateTime().desc().list();
任務(wù)回退
大體思路是拿到當(dāng)前的任務(wù),及當(dāng)前任務(wù)的上一個歷史任務(wù),然后重啟
代碼示例
TaskactiveTask=taskService.createTaskQuery() .taskId(taskId) .active() .singleResult(); ListhistoricTaskInstance=historyService.createHistoricTaskInstanceQuery() .processInstanceId(instanceId) .orderByHistoricActivityInstanceStartTime() .desc() .list(); List historicTaskInstances=historicTaskInstance.stream().filter(v->!v.getTaskDefinitionKey().equals(activeTask.getTaskDefinitionKey())).toList(); Assert.notEmpty(historicTaskInstances,"當(dāng)前已是初始任務(wù)!"); HistoricTaskInstancecurr=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ù)表單中填寫的流程需要用到的變量等。很多地方都要用到
流程變量變量傳遞
變量最終會存在 act_ru_variable 這個表里面
在繪制流程圖的時候,如果是用戶任務(wù)(userService) 可以設(shè)置變量,比如執(zhí)行人,

寫法有這么幾種方式
寫死,就比如 zhangsan
表達(dá)式,比如上面寫的 ${user},這種需要傳入?yún)?shù),其實就是啟動參數(shù)的時候傳入,傳入?yún)?shù),可選值為一個Map
關(guān)于擴(kuò)展變量,可在流程圖繪制這么設(shè)定,傳遞方式還是一樣,流程圖里面在下面寫:

代碼:
ProcessInstanceinstance=runtimeService.startProcessInstanceByKey(key,newHashMap<>());
變量設(shè)置
runtimeService.setVariable(instance.getId(),Constants.PATIENT_ID,relatedId);
變量查詢
Objectvariable=runtimeService.getVariable(instance.getId(),Constants.GENERAL_ID);
歷史變量查詢
HistoricVariableInstancevariableInstance=historyService.createHistoricVariableInstanceQuery().processInstanceId(bo.getId().toString()). variableName(Constants.PATIENT_ID).singleResult(); //變量值 variableInstance.getValue(); //變量名稱 variableInstance.getName();
針對后端來說任務(wù)類型主要有兩種。
用戶任務(wù)-userTask
即需要用戶參與的任務(wù),因為工作流執(zhí)行過程中需要涉及到審批、過審之類的需要用戶參與的任務(wù),這個時候需要用戶參與,然后調(diào)用接口完成任務(wù)。
服務(wù)任務(wù)-serviceTask
即自動執(zhí)行的任務(wù),比如用戶提交后,系統(tǒng)自動存儲、修改狀態(tài)等自動完成的任務(wù)。
Type
任務(wù)類型是關(guān)鍵,可根據(jù)配型配置實現(xiàn)調(diào)用 java的方法,spring 的bean方法,等等有這么幾種類型

推薦使用 -- Delegate Expression !!!
在系統(tǒng)任務(wù)中,因為是自動執(zhí)行,所以實際應(yīng)用中需要嵌入各種業(yè)務(wù)邏輯,可以在流程圖設(shè)計中,按照下面方式調(diào)用java代碼執(zhí)行,在spring中配置同名的bean

配置表達(dá)式,可以實現(xiàn)JavaDelegate接口使用類名配置,快捷寫法如下,比較推薦下面這種,此種可靈活配置bean和spring結(jié)合使用,注入service等業(yè)務(wù)方法
@Bean("t17")
JavaDelegatet17(){
returnexecution->{
Mapvariables=execution.getVariables();
Tasktask=taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
//業(yè)務(wù)邏輯
task.setOwner(String.valueOf(dentistId));
};
}
Java Class :
配置java類名,需要實現(xiàn)JavaDelegate接口,注意是全路徑名,不可以使用Spring的bean配置?。?!
@Component
publicclassT17DelegateimplementsJavaDelegate{
@Override
publicvoidexecute(DelegateExecutionexecution)throwsException{
StringtaskId=execution.getId();
StringinstanceId=execution.getProcessInstanceId();
Mapvariables=execution.getVariables();
}
}
下面兩種可使用spring的配置
Expression:
EL表達(dá)式,調(diào)用java類的方法 ,規(guī)范:
expression=“#{monitorExecution.execution(execution)}”
@Component("monitorExecution")
publicclassMonitorExecution{
publicvoidexecution(DelegateExecutionexecution){
StringprocessInstanceId=execution.getProcessInstanceId();
}
}
任務(wù)監(jiān)聽器 - Task Listener
任務(wù)監(jiān)聽器用于在某個與任務(wù)相關(guān)的事件發(fā)生時執(zhí)行自定義Java邏輯或表達(dá)式。它只能作為用戶任務(wù)的子元素添加到流程定義中。
請注意,這也必須作為BPMN 2.0擴(kuò)展元素的子級和Camunda命名空間中發(fā)生,因為任務(wù)偵聽器是專門為Camunda引擎構(gòu)建的。
適用場景:
@Bean
TaskListenert21(){
returndelegateTask->{
StringtaskId=delegateTask.getId();
StringinstanceId=delegateTask.getProcessInstanceId();
Mapvariables=delegateTask.getVariables();
//TODO:20log/3/22
delegateTask.setVariable("","");
};
}
執(zhí)行監(jiān)聽器 - Execution Listener
執(zhí)行偵聽器在流程執(zhí)行過程中發(fā)生某些事件時執(zhí)行外部Java代碼或計算表達(dá)式??梢杂迷谌魏稳蝿?wù)中,可以捕獲的事件有:
流程實例的開始和結(jié)束。
進(jìn)行過渡。
活動的開始和結(jié)束。
網(wǎng)關(guān)的開始和結(jié)束。
中間事件的開始和結(jié)束。
結(jié)束開始事件或開始結(jié)束事件
適用場景:每個任務(wù)結(jié)束時設(shè)置任務(wù)進(jìn)度
publicclassExampleExecutionListenerOneimplementsExecutionListener{
publicvoidnotify(DelegateExecutionexecution)throwsException{
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ù),但是我們實際開發(fā)中,一般會用到我們自己的鑒權(quán)系統(tǒng),所以可以使用camunda提供的api來設(shè)置,具體可以看IdentityServiceImpl這個類,其中也是使用了ThreadLocal來保存鑒權(quán)信息 ,代碼在下面
privateThreadLocalcurrentAuthentication=newThreadLocal ();
用戶信息設(shè)置:
//Userutil是我們自己封裝的用戶工具類 identityService.setAuthenticatedUserId(UserUtil.getUserId().toString()); //獲取 Authenticationauthentication=identityService.getCurrentAuthentication();
他內(nèi)置很多比如開啟流程時候,會默認(rèn)找當(dāng)前登錄的人,這個類DefaultHistoryEventProducer
//setsuperprocessinstanceid
ExecutionEntitysuperExecution=executionEntity.getSuperExecution();
if(superExecution!=null){
evt.setSuperProcessInstanceId(superExecution.getProcessInstanceId());
}
//state
evt.setState(HistoricProcessInstance.STATE_ACTIVE);
//setstartuserId
evt.setStartUserId(Context.getCommandContext().getAuthenticatedUserId());
任務(wù)執(zhí)行人及發(fā)起人設(shè)置
//根據(jù)任務(wù)id設(shè)置執(zhí)行人 taskService.setAssignee(task.getId(),UserUtil.getUserId().toString());
-
數(shù)據(jù)
+關(guān)注
關(guān)注
8文章
7347瀏覽量
94996 -
管理系統(tǒng)
+關(guān)注
關(guān)注
1文章
2936瀏覽量
38682 -
小程序
+關(guān)注
關(guān)注
1文章
243瀏覽量
13445 -
SpringBoot
+關(guān)注
關(guān)注
0文章
178瀏覽量
709
原文標(biāo)題:SpringBoot 集成 Camunda 流程引擎,實現(xiàn)一套完整的業(yè)務(wù)流程
文章出處:【微信號:芋道源碼,微信公眾號:芋道源碼】歡迎添加關(guān)注!文章轉(zhuǎn)載請注明出處。
發(fā)布評論請先 登錄
springboot集成mqtt
SpringBoot配置嵌入式Servlet
cmake管理配置ROOT項目的方法
推薦兩個工作流的springboot項目
如何在SpringBoot項目中實現(xiàn)動態(tài)定時任務(wù)
基于SpringBoot實現(xiàn)郵件發(fā)送
深入了解SpringBoot的自動配置原理
什么是 SpringBoot?
SpringBoot的核心注解1
SpringBoot的核心注解2
有哪些方法可以讀取Springboot的配置呢?
Springboot項目的集成以及具體使用及配置
評論