首頁 > 軟體

使用SpringBoot整合Activiti6工作流的操作方法

2022-07-30 18:01:57

1.idea安裝actibpm流程設計器

開啟idea外掛搜尋actibpm,安裝成功後重啟idea

2.建立maven專案,匯入相關依賴,新增組態檔

pom.xml依賴

<?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 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dongmen</groupId>
    <artifactId>testActSpringBoot</artifactId>
    <version>1.0-SNAPSHOT</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.0.RELEASE</version>
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <activiti.version>6.0.0</activiti.version>
        <mysql.version>5.1.29</mysql.version>
    </properties>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-jdbc</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
        </dependency>
        <dependency>
            <groupId>org.activiti</groupId>
            <artifactId>activiti-spring-boot-starter-rest-api</artifactId>
            <version>${activiti.version}</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>${mysql.version}</version>
        </dependency>
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

組態檔

#啟動埠
server.port=81
spring.datasource.url=jdbc:mysql://localhost:3306/acttest?useUnicode=true&characterEncoding=utf8&zeroDateTimeBehavior=convertToNull&useSSL=true&serverTimezone=GMT%2B8&nullCatalogMeansCurrent=true
spring.datasource.username=root
spring.datasource.password=123456
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
#1.false,預設值,acticiti啟動時對比資料庫表中儲存的版本,如果沒有表或者版本不匹配,將丟擲異常
#2.true,acticiti會對資料中所有的表進行更新操作,如果表不存在,則自動建立
#3.create_drop,在acticiti啟動時建立表,關閉時刪除表(必須手動關閉引擎才能刪除表)
#4.drop_create,在acticiti啟動時刪除原來的表,然後建立新表(不需要手動關閉引擎)
spring.activiti.database-schema-update=true
#檢測歷史表是否存在,acticit7預設沒有開啟歷史記錄資訊
spring.activiti.db-history-used=true
#歷史記錄等級
#1.none:不儲存任何歷史記錄,因此在流程執行過程中,這是最高效的
#2.acticiti:級別高於none,儲存流程範例與流程行為,其他資料不儲存
#3.audit:除activiti級別會儲存的資料外,還會儲存全部的流程任務及其屬性,audit為預設值
#4.full:儲存歷史資料的最高階別,除了儲存audit級別的資料外,還會儲存其他流程相關的細節資料,包括一些流程引數等
spring.activiti.history-level=full
#預設true,效驗流程檔案,預設效驗resources下的processes資料夾裡的流程,為true自動部署流程,為false則不部署
#spring.activiti.check-process-definitions=false

3.新增啟動類,剔除掉activiti預設整合springsecurity

package com.dongmen;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

/**
 * @author TANGSHUAI
 * @version 1.0
 * @date 2021-12-13 11:13
 * 專案啟動會自動部署processes資料夾的bpmn檔案
 * 剔除springsecurity
 */
@SpringBootApplication(exclude = {
        org.activiti.spring.boot.SecurityAutoConfiguration.class,
        org.springframework.boot.autoconfigure.security.servlet.SecurityAutoConfiguration.class})
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

4.新建資料夾processes(名稱不能更改),新增Bpmn檔案,畫流程圖

這裡我們用到了三種標籤,

 

連線下一個標籤的線條,需要滑鼠移動到標籤中間來獲取線條

畫好流程圖後點選空白處,左邊會出現流程引數,這裡我們只需要設定流程的id與name即可

設定審批使用者資訊,這裡我第一個節點設定的時zhangsan,第二個節點設定的是lisi,設定成功後儲存

左側選單介紹

5.建立Controller

package com.dongmen.controller;


import org.activiti.engine.HistoryService;
import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.history.HistoricTaskInstance;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author TANGSHUAI
 * @version 1.0
 * @date 2021-12-13 13:21
 */
@RestController
public class TestController {

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private HistoryService historyService;

    //啟動流程
    @RequestMapping("/startAct")
    public void startAct() {
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey("myProcess_1");
        System.out.println(processInstance.getId());
    }

    //查詢代辦任務
    @RequestMapping("taskAgents")
    public void taskAgents(){
        String user="zhangsan";
        List<Task> list = taskService.createTaskQuery().taskAssignee(user).list();
        for (Task task : list) {
            System.out.println("任務id:"+task.getId());
            System.out.println("當前審批使用者:"+task.getAssignee());
            System.out.println("任務名稱:"+task.getName());
        }
    }

    //審批流程
    @RequestMapping("/approvalProcess")
    public void approvalProcess() {
        String key = "myProcess_1";
        String user="zhangsan";
        //流程範例id,有多個流程時傳遞,單個不需要傳,act_ru_task表id
        String id="2505";
        //根據流程key,與使用者名稱稱查詢審批
        Task task = taskService.createTaskQuery()
                .processDefinitionKey(key)
                .taskAssignee(user)
                .taskId(id)
                .singleResult();
        if(task!=null){
            //審批流程
            taskService.complete(task.getId());
            System.out.println("審批成功!");
        }

    }

    //查詢已辦任務
    @RequestMapping("/hasToDoTasks")
    public void hasToDoTasks(){
        String user="zhangsan";
        List<HistoricTaskInstance> list = historyService.createHistoricTaskInstanceQuery().taskAssignee(user).list();
        for (HistoricTaskInstance historicTaskInstance : list) {
            System.out.println("開始時間"+historicTaskInstance.getStartTime()+",結束時間:"+historicTaskInstance.getEndTime());
            System.out.println("流程名稱"+historicTaskInstance.getName());
        }


    }

}

6.啟動專案,Activiti會自動建立28張表,自動部署processes目錄Bpnm檔案

表結構介紹

7.測試Controller

localhost:81/startAct–啟動流程,檢視資料表act_ru_task

localhost:81/taskAgents–查詢待辦任務,存取後檢視後臺列印

localhost:81/approvalProcess–審批流程,點選審批流程後變成第二個節點審批

localhost:81/hasToDoTasks–查詢已辦任務,

到此這篇關於使用SpringBoot整合Activiti6(工作流)的文章就介紹到這了,更多相關SpringBoot整合Activiti6內容請搜尋it145.com以前的文章或繼續瀏覽下面的相關文章希望大家以後多多支援it145.com!


IT145.com E-mail:sddin#qq.com