<kbd id="afajh"><form id="afajh"></form></kbd>
<strong id="afajh"><dl id="afajh"></dl></strong>
    <del id="afajh"><form id="afajh"></form></del>
        1. <th id="afajh"><progress id="afajh"></progress></th>
          <b id="afajh"><abbr id="afajh"></abbr></b>
          <th id="afajh"><progress id="afajh"></progress></th>

          SpringBoot 集成 Camunda 流程引擎,實現(xiàn)一套完整的業(yè)務(wù)流程

          共 21412字,需瀏覽 43分鐘

           ·

          2023-08-01 12:19

          前言

          項目中需要用到工作流引擎來設(shè)計部分業(yè)務(wù)流程,框架選型最終選擇了 Camunda7,關(guān)于 Camunda以及 Activity 等其他工作流 引擎的介紹及對比不再介紹,這里只介紹與現(xiàn)有Springboot項目的集成以及具體使用及配置

          概念

          • 流程(PROCESS): 通過工具建模最終生成的BPMN文件,里面有整個流程的定義

          • 流程實例(Instance):流程啟動后的實例

          • 流程變量(Variables):流程任務(wù)之間傳遞的參數(shù)

          • 任務(wù)(TASK):流程中定義的每一個節(jié)點

          • 流程部署:將之前流程定義的.bpmn文件部署到工作流平臺

          核心組件

          • 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,默認實現(xiàn)如下:

          RepositoryService

          此服務(wù)提供用于管理和操作部署和流程定義的操作,使用camunda的第一要務(wù)

          RuntimeService

          運行相關(guān),啟動流程實例、刪除、搜索等

          TaskService

          所有圍繞任務(wù)相關(guān)的操作,如完成、分發(fā)、認領(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操作接口包

          <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ù)庫

          我這邊使用的是mysql,建了個新庫 camunda(可自定義),啟動后會自動生成所需表結(jié)構(gòu)

          POM文件

          <?xml version="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: All tasks
           
          # 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

          啟動效果

          準備好前置工作,啟動后效果如下:

          數(shù)據(jù)庫表結(jié)構(gòu)

          啟動后自動生成的表結(jié)構(gòu)如下

          大概有這么幾個表模塊,重要的詳細介紹下:

          • 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進制數(shù)據(jù),所以如果文件修改后,重啟也沒用,因為重新生成了記錄,需要清掉數(shù)據(jù)庫,或者這個表記錄

          登錄界面

          登錄地址為 http://localhost:8081/,輸入用戶名密碼即為配置文件里面的 admin,123456

          主控制臺

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

          具體業(yè)務(wù)集成

          繪制流程圖

          下載

          首先需要一個工具 Camunda Modeler 來畫,下載地址:

          • https://camunda.com/download/modeler/

          解壓縮后打開如下:

          繪制

          新建一個

          我這邊稍微畫了一個,具體怎么畫,就不在細說了,最后效果如下,模擬了個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è)置表達式 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文件夾,放進去,

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

          具體開發(fā)

          寫幾個測試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();
          }

          啟動流程成功,說明問題不大,接下來詳細業(yè)務(wù)改進。

          下一篇介紹詳細的業(yè)務(wù)集成及各種API(變量傳遞、自動任務(wù))的使用

          API使用

          流程相關(guān)API

          創(chuàng)建流程:

          會同時創(chuàng)建第一個任務(wù)

          ProcessInstance instance = 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,然后在附上查詢條件,實例幾個

          List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
          List<Task> list = taskService.createTaskQuery().taskAssignee("zhangsan").list();
          List<ProcessInstance> instances = runtimeService.createProcessInstanceQuery().listPage(110);

          查詢歷史任務(wù)

          List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().list();

          查詢當前任務(wù)/分頁

          List<Task> list = taskService.createTaskQuery().orderByTaskCreateTime().desc().list();

          任務(wù)回退

          大體思路是拿到當前的任務(wù),及當前任務(wù)的上一個歷史任務(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, "當前已是初始任務(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ù)表單中填寫的流程需要用到的變量等。很多地方都要用到

          流程變量變量傳遞

          變量最終會存在 act_ru_variable 這個表里面

          在繪制流程圖的時候,如果是用戶任務(wù)(userService) 可以設(shè)置變量,比如執(zhí)行人,

          寫法有這么幾種方式

          • 寫死,就比如 zhangsan

          • 表達式,比如上面寫的 ${user},這種需要傳入?yún)?shù),其實就是啟動參數(shù)的時候傳入,傳入?yún)?shù),可選值為一個Map<String, Object>,之后的流程可查看次參數(shù),上面寫的是 user, 所以map里面的key需要帶著user,不然會報錯。

          關(guān)于擴展變量,可在流程圖繪制這么設(shè)定,傳遞方式還是一樣,流程圖里面在下面寫:

          代碼:

          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();

          針對后端來說任務(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

          配置表達式,可以實現(xiàn)JavaDelegate接口使用類名配置,快捷寫法如下,比較推薦下面這種,此種可靈活配置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類名,需要實現(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表達式,調(diào)用java類的方法 ,規(guī)范:

          expression=“#{monitorExecution.execution(execution)}”

          @Component("monitorExecution")
          public class MonitorExecution {
              public void execution(DelegateExecution execution){
                  String processInstanceId = execution.getProcessInstanceId();
              }
          }

          任務(wù)監(jiān)聽器 - Task Listener

          任務(wù)監(jiān)聽器用于在某個與任務(wù)相關(guān)的事件發(fā)生時執(zhí)行自定義Java邏輯或表達式。它只能作為用戶任務(wù)的子元素添加到流程定義中。

          請注意,這也必須作為BPMN 2.0擴展元素的子級和Camunda命名空間中發(fā)生,因為任務(wù)偵聽器是專門為Camunda引擎構(gòu)建的。

          適用場景:

          @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)聽器 - Execution Listener

          執(zhí)行偵聽器在流程執(zhí)行過程中發(fā)生某些事件時執(zhí)行外部Java代碼或計算表達式。可以用在任何任務(wù)中,可以捕獲的事件有:

          • 流程實例的開始和結(jié)束。
          • 進行過渡。
          • 活動的開始和結(jié)束。
          • 網(wǎng)關(guān)的開始和結(jié)束。
          • 中間事件的開始和結(jié)束。
          • 結(jié)束開始事件或開始結(jié)束事件

          適用場景:每個任務(wù)結(jié)束時設(shè)置任務(wù)進度

           public class ExampleExecutionListenerOne implements ExecutionListener {
           
              public void notify(DelegateExecution execution) throws Exception {
                execution.setVariable("variableSetInExecutionListener""firstValue");
                execution.setVariable("eventReceived", execution.getEventName());
              }
            }

          擴展屬性- Extension properties

          擴展屬性適用于很多自定義的業(yè)務(wù)屬性,比如設(shè)置業(yè)務(wù)流程進度

          流程權(quán)限及創(chuàng)建人設(shè)置

          IdentityService為鑒權(quán)相關(guān)服務(wù),但是我們實際開發(fā)中,一般會用到我們自己的鑒權(quán)系統(tǒng),所以可以使用camunda提供的api來設(shè)置,具體可以看IdentityServiceImpl這個類,其中也是使用了ThreadLocal來保存鑒權(quán)信息 ,代碼在下面

          private ThreadLocal<Authentication> currentAuthentication = new ThreadLocal<Authentication>();

          用戶信息設(shè)置:

          // Userutil是我們自己封裝的用戶工具類
          identityService.setAuthenticatedUserId(UserUtil.getUserId().toString());
           
          //獲取
          Authentication authentication = identityService.getCurrentAuthentication();

          他內(nèi)置很多比如開啟流程時候,會默認找當前登錄的人,這個類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());

          來源:blog.csdn.net/yu619251940/

          article/details/129670382


               

          End

                  
                      
                         

          資料鏈接


                        

          清華學姐自學的Linux筆記,天花板級別!

          新版鳥哥Linux私房菜資料

          阿里大佬總結(jié)的《圖解Java》火了,完整版PDF開放下載!

          Alibaba官方上線!SpringBoot+SpringCloud全彩指南

          國內(nèi)最強的SpringBoot+Vue全棧項目天花板,不接受反駁!


          瀏覽 1132
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          評論
          圖片
          表情
          推薦
          點贊
          評論
          收藏
          分享

          手機掃一掃分享

          分享
          舉報
          <kbd id="afajh"><form id="afajh"></form></kbd>
          <strong id="afajh"><dl id="afajh"></dl></strong>
            <del id="afajh"><form id="afajh"></form></del>
                1. <th id="afajh"><progress id="afajh"></progress></th>
                  <b id="afajh"><abbr id="afajh"></abbr></b>
                  <th id="afajh"><progress id="afajh"></progress></th>
                  小早川怜子 无码 在线 | 变态另类成人AV一区二区 | 日本少妇 ╳乄 黑人 | 免费成年人在线 | 黑人大屌操 |