自拍偷在线精品自拍偷,亚洲欧美中文日韩v在线观看不卡

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

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

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

概念

  • 流程(PROCESS): 通過工具建模最終生成的BPMN文件,里面有整個流程的定義
  • 流程實例(Instance):流程啟動后的實例
  • 流程變量(Variables):流程任務之間傳遞的參數(shù)
  • 任務(TASK):流程中定義的每一個節(jié)點
  • 流程部署:將之前流程定義的.bpmn文件部署到工作流平臺

核心組件

  • Process Engine-流程引擎
  • Web Applicatons- 基于web的管理頁面

API介紹

官方文檔

下面是官網(wǎng)的一些文檔,有時間可以看看,下面說一些核心的東西。

圖片圖片

ProcessEngine

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

圖片圖片

RepositoryService

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

RuntimeService

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

TaskService

所有圍繞任務相關(guān)的操作,如完成、分發(fā)、認領(lǐng)等

HistoryService

提供引擎搜集的歷史數(shù)據(jù)服務

IdentityService

用戶相關(guān),實際中用不太到

Springboot集成

依賴集成

maven

https://mvnrepository.com/search?q=org.camunda.bpm.springboot

可以根據(jù)需要引用版本,我這邊用的是 7.18

需要3個maven依賴,分別是對應 流程引擎、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 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

啟動效果

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

圖片圖片

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

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

圖片

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

  • ACT_ID_

這部分表示用戶模塊,配置文件里面的用戶,信息就在此模塊

關(guān)注公眾號:碼猿技術(shù)專欄,回復關(guān)鍵詞:1111 獲取阿里內(nèi)部Java性能調(diào)優(yōu)手冊!

圖片圖片

  • ACT_HI_

表示流程歷史記錄

  • act_hi_actinst:執(zhí)行的活動歷史
  • act_hi_taskinst:執(zhí)行任務歷史
  • 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:運行時的任務
  • act_ru_variable:運行時的流程變量
  • ACT_GE_

流程通用數(shù)據(jù)

  • act_ge_bytearray:每次部署的文件2進制數(shù)據(jù),所以如果文件修改后,重啟也沒用,因為重新生成了記錄,需要清掉數(shù)據(jù)庫,或者這個表記錄

登錄界面

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

圖片圖片

主控制臺

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

具體業(yè)務集成

繪制流程圖

下載

首先需要一個工具 Camunda Modeler 來畫,下載地址:https://camunda.com/download/modeler/

解壓縮后打開如下:

圖片圖片

繪制

新建一個:

圖片圖片

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

圖片圖片

任務分類

只介紹最常用的兩種

  • 用戶任務 (User Task)

圖片圖片

具體來說就是需要手動執(zhí)行的任務,即需要我們這變寫完業(yè)務代碼后,調(diào)用代碼

taskService.complete(taskId, variables);

才會完成的任務

  • 系統(tǒng)任務(Service Task)

圖片圖片

系統(tǒng)會自動幫我們完成的任務

網(wǎng)關(guān)

分為這么幾類,會根據(jù)我們傳入的流程變量及設定的條件走

圖片圖片

  • 排他網(wǎng)關(guān)(exclusive gateway)

這個網(wǎng)關(guān)只會走一個,我們走到這個網(wǎng)關(guān)時,會從上到下找第一個符合條件的任務往下走

  • 并行網(wǎng)關(guān)(Parallel Gateway)

這個網(wǎng)關(guān)不需要設置條件,會走所有的任務

  • 包含網(wǎng)關(guān)(Inclusive Gateway)

這個網(wǎng)關(guān)會走一個或者多個符合條件的任務

示例

圖片圖片

如上圖包含網(wǎng)關(guān),需要在網(wǎng)關(guān)的連線初設置表達式 condition,參數(shù)來自于流程變量

兩個參數(shù):

switch2d 、 switch3d
  • 如果 都為true,則走任務1,3
  • 如果 switch2d 為true switch3d為false,則只走任務1
  • 如果 switch3d 為true switch2d為false,則只走任務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è)務改進。

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

API使用

流程相關(guān)API

創(chuàng)建流程:

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

ProcessInstance instance = runtimeService.startProcessInstanceByKey(processKey, params);

暫停流程:

流程暫停后,再執(zhí)行相關(guān)任務會報錯,需要先重新激活任務

runtimeService.suspendProcessInstanceById(instance.getId());

重新激活流程:

runtimeService.activateProcessInstanceById(instance.getId());

刪除流程:

會同時刪除任務

runtimeService.deleteProcessInstance(instance.getId(), "手動刪除");

圖片圖片

以上都可以在流程歷史表 act_hi_procinst 里查詢

任務相關(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(1, 10);

查詢歷史任務

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

查詢當前任務/分頁

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

任務回退

大體思路是拿到當前的任務,及當前任務的上一個歷史任務,然后重啟

代碼示例:

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, "當前已是初始任務!");
        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è)務表單中填寫的流程需要用到的變量等。很多地方都要用到

流程變量變量傳遞

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

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

圖片圖片

寫法有這么幾種方式

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

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

圖片圖片

代碼:

ProcessInstance instance = runtimeService.startProcessInstanceByKey(key, new HashMap<>());

變量設置:

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

針對后端來說任務類型主要有兩種。

用戶任務-userTask

即需要用戶參與的任務,因為工作流執(zhí)行過程中需要涉及到審批、過審之類的需要用戶參與的任務,這個時候需要用戶參與,然后調(diào)用接口完成任務。

服務任務-serviceTask

即自動執(zhí)行的任務,比如用戶提交后,系統(tǒng)自動存儲、修改狀態(tài)等自動完成的任務。

Type

任務類型是關(guān)鍵,可根據(jù)配型配置實現(xiàn)調(diào)用 java的方法,spring 的bean方法,等等有這么幾種類型

圖片圖片

推薦使用 -- Delegate Expression !!!

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

圖片圖片

配置表達式,可以實現(xiàn)JavaDelegate接口使用類名配置,快捷寫法如下,比較推薦下面這種,此種可靈活配置bean和spring結(jié)合使用,注入service等業(yè)務方法

@Bean("t17")
JavaDelegate t17() {
    return execution -> {
        Map<String, Object> variables = execution.getVariables();
        Task task = taskService.createTaskQuery().processInstanceId(execution.getProcessInstanceId()).singleResult();
        //業(yè)務邏輯
        task.setOwner(String.valueOf(dentistId));
    };
}

Java Class :

配置java類名,需要實現(xiàn)JavaDelegate接口,注意是全路徑名,不可以使用Spring的bean配置!?。?/span>

@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ī)范:

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

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

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

請注意,這也必須作為BPMN 2.0擴展元素的子級和Camunda命名空間中發(fā)生,因為任務偵聽器是專門為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代碼或計算表達式。可以用在任何任務中,可以捕獲的事件有:

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

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

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

擴展屬性- Extension properties

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

圖片圖片

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

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

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

用戶信息設置:

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

任務執(zhí)行人及發(fā)起人設置

//根據(jù)任務id設置執(zhí)行人
taskService.setAssignee(task.getId(), UserUtil.getUserId().toString());


責任編輯:武曉燕 來源: 碼猿技術(shù)專欄
相關(guān)推薦

2023-06-12 08:01:57

Camunda工作流引擎

2010-02-04 13:57:19

ibmdwWebsphere

2020-06-19 12:23:05

CIO數(shù)據(jù)分析技術(shù)

2024-10-11 11:19:05

LiteFlow框架流程

2023-03-26 00:53:04

camunda7camunda8流程引擎

2017-05-16 10:46:06

博陽咨詢流程管理

2013-11-06 14:15:11

流程

2010-06-29 15:45:57

UML業(yè)務流程

2020-01-19 20:07:32

物聯(lián)網(wǎng)大數(shù)據(jù)IOT

2025-01-20 00:00:03

高并發(fā)秒殺業(yè)務

2013-11-06 14:16:23

流程

2011-05-18 11:34:54

云端業(yè)務流程管理

2021-05-06 17:22:22

區(qū)塊鏈加密貨幣技術(shù)

2023-01-04 13:41:23

RPA自動化機器人

2017-08-11 11:41:53

迭代BPM開發(fā)

2013-10-14 15:36:44

流程

2012-12-03 13:50:40

IBMdW

2023-10-17 13:35:00

數(shù)字化轉(zhuǎn)型供應商

2020-05-06 10:07:15

價值流圖VSM可視化圖形
點贊
收藏

51CTO技術(shù)棧公眾號