SpringBoot+flowable完美結合,快速實現工作流

java架構師追風 發佈 2022-07-05T07:21:00.952492+00:00

打開見原文連結 用帳戶:admin/test 登錄。英文比較差的兄弟也不要怕,我這裡有翻譯好的中文操作文檔,大家對著照貓畫虎就行,轉發本文後私信【701】三個數字即可無償獲取這份文檔。

總覽

  • 使用flowable自帶的flowable-ui製作流程圖
  • 使用springboot開發流程使用的接口完成流程的業務功能

一、flowable-ui部署運行

  • 從官網下載flowable-6.6.0
  • 將壓縮包中的 flowable-6.6.0\wars\flowable-ui.war 丟到Tomcat中跑起來
  • 打開http://localhost:8080/flowable-ui 用帳戶:admin/test 登錄

4、進入APP.MODELER創建流程,之後可以導出流程到項目中使用,或者配置apache-tomcat-9.0.37\webapps\flowable-ui\WEB-INF\classes\flowable-default.properties連接本地資料庫

注意:需要將java驅動jar(mysql-connector-java-5.1.45.jar)複製到 apache-tomcat-9.0.37\webapps\flowable-rest\WEB-INF\lib

這樣創建的流程後端程序就能直接使用

英文比較差的兄弟也不要怕,我這裡有翻譯好的中文操作文檔,大家對著照貓畫虎就行,轉發本文後私信【701】三個數字即可無償獲取這份文檔

二、繪製流程圖

根據業務需要在 flowable-ui>APP.MODELER裡面繪製流程圖,示例如上圖。先解釋一些概念。

  • 事件(event) 通常用於為流程生命周期中發生的事情建模,圖里是【開始、結束】兩個圈。
  • 順序流(sequence flow) 是流程中兩個元素間的連接器。圖里是【箭頭線段】。
  • 網關(gateway) 用於控制執行的流向。圖里是【菱形(中間有X)】
  • 用戶任務(user task) 用於對需要人工執行的任務進行建模。圖里是【矩形】。

簡單的工作流大概就這些元素(還有很多這裡就不擴展了)。下面描述一下工作流是如何流動的。

首先啟動了工作流後,由【開始】節點自動流向【學生】節點,等待該任務執行。任務被分配的學生用戶執行後流向 【老師】節點,再次等待該任務執行。被分配的老師用戶執行後流向 【網關】,網關以此檢查每個出口,流向符合條件的任務,比如這裡老師執行任務時是同意,就流向【校長】節點,等待該任務執行。執行後跟老師類似,同意後就流向【結束】節點,整個流程到此結束。

繪圖細節:

1、保留流程模型

2、順序流可以設置流條件來限制流動,比如上面的網關出口就設置了條件

3、任務需要分配任務的執行用戶,可以分配到候選組,也可以直接分配到候選人

最後導出工作流文件

文件內容

<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-insmtece" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI" xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC" xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef">
  <process id="leave_approval" name="請假審批" isExecutable="true">
    <startEvent id="start" name="開始" flowable:initiator="startuser" flowable:formFieldValidation="true"></startEvent>
    <userTask id="stu_task" name="學生" flowable:candidateGroups="stu_group" flowable:formFieldValidation="true"></userTask>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="stu_task"></sequenceFlow>
    <userTask id="te_task" name="老師" flowable:candidateGroups="te_group" flowable:formFieldValidation="true"></userTask>
    <exclusiveGateway id="getway1" name="網關1"></exclusiveGateway>
    <userTask id="mte_task" name="校長" flowable:candidateGroups="mte_group" flowable:formFieldValidation="true"></userTask>
    <exclusiveGateway id="getway2" name="網關2"></exclusiveGateway>
    <endEvent id="end" name="結束"></endEvent>
    <sequenceFlow id="flow1" name="請假" sourceRef="stu_task" targetRef="te_task" skipExpression="${command=='agree'}"></sequenceFlow>
    <sequenceFlow id="flow3_1" name="同意" sourceRef="getway1" targetRef="mte_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow2" name="審批" sourceRef="te_task" targetRef="getway1"></sequenceFlow>
    <sequenceFlow id="flow3_2" name="拒絕" sourceRef="getway1" targetRef="stu_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4" name="審批" sourceRef="mte_task" targetRef="getway2"></sequenceFlow>
    <sequenceFlow id="flow4_1" name="同意" sourceRef="getway2" targetRef="end" skipExpression="${command=='free'}">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4_2" name="拒絕" sourceRef="getway2" targetRef="stu_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave_approval">
    這裡先省略
  </bpmndi:BPMNDiagram>
</definitions>

4、bpmn文件導入

如果需要,可以把這個流程文件下載下來,直接導入使用

三、後台項目搭建

後台項目基於jdk8,使用springboot框架

spring 版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

項目依賴pom.xml

<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>6.6.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.45</version>
</dependency>

項目配置application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flowable?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456

四、資料庫

1、Flowable的所有資料庫表都以ACT_開頭。第二部分是說明表用途的兩字符標示符。服務API的命名也大略符合這個規則。

2、ACT_RE_: 'RE』代表repository。帶有這個前綴的表包含「靜態」信息,例如流程定義與流程資源(圖片、規則等)。

3、ACT_RU_: 'RU』代表runtime。這些表存儲運行時信息,例如流程實例(process instance)、用戶任務(user task)、變量(variable)、作業(job)等。Flowable只在流程實例運行中保存運行時數據,並在流程實例結束時刪除記錄。這樣保證運行時表小和快。

4、ACT_HI_: 'HI』代表history。這些表存儲歷史數據,例如已完成的流程實例、變量、任務等。

5、ACT_GE_: 通用數據。在多處使用。

微信搜索公眾號:Java後端編程,回覆:java 領取資料 。

1)通用數據表(2個)

  • act_ge_bytearray:二進位數據表,如流程定義、流程模板、流程圖的字節流文件;
  • act_ge_property:屬性數據表(不常用);

2)歷史表(8個,HistoryService接口操作的表)

  • act_hi_actinst:歷史節點表,存放流程實例運轉的各個節點信息(包含開始、結束等非任務節點);
  • act_hi_attachment:歷史附件表,存放歷史節點上傳的附件信息(不常用);
  • act_hi_comment:歷史意見表;
  • act_hi_detail:歷史詳情表,存儲節點運轉的一些信息(不常用);
  • act_hi_identitylink:歷史流程人員表,存儲流程各節點候選、辦理人員信息,常用於查詢某人或部門的已辦任務;
  • act_hi_procinst:歷史流程實例表,存儲流程實例歷史數據(包含正在運行的流程實例);
  • act_hi_taskinst:歷史流程任務表,存儲歷史任務節點;
  • act_hi_varinst:流程歷史變量表,存儲流程歷史節點的變量信息;

3)用戶相關表(4個,IdentityService接口操作的表)

  • act_id_group:用戶組信息表,對應節點選定候選組信息;
  • act_id_info:用戶擴展信息表,存儲用戶擴展信息;
  • act_id_membership:用戶與用戶組關係表;
  • act_id_user:用戶信息表,對應節點選定辦理人或候選人信息;

4)流程定義、流程模板相關表(3個,RepositoryService接口操作的表)

  • act_re_deployment:部屬信息表,存儲流程定義、模板部署信息;
  • act_re_procdef:流程定義信息表,存儲流程定義相關描述信息,但其真正內容存儲在act_ge_bytearray表中,以字節形式存儲;
  • act_re_model:流程模板信息表,存儲流程模板相關描述信息,但其真正內容存儲在act_ge_bytearray表中,以字節形式存儲;

5)流程運行時表(6個,RuntimeService接口操作的表)

  • act_ru_task:運行時流程任務節點表,存儲運行中流程的任務節點信息,重要,常用於查詢人員或部門的待辦任務時使用;
  • act_ru_event_subscr:監聽信息表,不常用;
  • act_ru_execution:運行時流程執行實例表,記錄運行中流程運行的各個分支信息(當沒有子流程時,其數據與act_ru_task表數據是一一對應的);
  • act_ru_identitylink:運行時流程人員表,重要,常用於查詢人員或部門的待辦任務時使用;
  • act_ru_job:運行時定時任務數據表,存儲流程的定時任務信息;
  • act_ru_variable:運行時流程變量數據表,存儲運行中的流程各節點的變量信息;

五、流程引擎API與服務

引擎API是與Flowable交互的最常用手段。總入口點是ProcessEngine。

1、RepositoryService很可能是使用Flowable引擎要用的第一個服務。這個服務提供了管理與控制部署(deployments)與流程定義(process definitions)的操作。管理靜態信息,

2、RuntimeService用於啟動流程定義的新流程實例。

3、IdentityService很簡單。它用於管理(創建,更新,刪除,查詢……)組與用戶。

4、FormService是可選服務。也就是說Flowable沒有它也能很好地運行,而不必犧牲任何功能。

5、HistoryService暴露Flowable引擎收集的所有歷史數據。要提供查詢歷史數據的能力。

6、ManagementService通常在用Flowable編寫用戶應用時不需要使用。它可以讀取資料庫表與表原始數據的信息,也提供了對作業(job)的查詢與管理操作。

7、DynamicBpmnService可用於修改流程定義中的部分內容,而不需要重新部署它。例如可以修改流程定義中一個用戶任務的辦理人設置,或者修改一個服務任務中的類名。

接下來使用之前的請假流程圖,上代碼

代碼


import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * TestFlowable
 *
 * @Author 
 * @Date: 2021/10/17 23:35
 * @Version 1.0
 */
@Slf4j
public class TestFlowable {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private org.flowable.engine.TaskService taskService;

    @Autowired
    private org.flowable.engine.IdentityService identityService;

    public void createDeploymentZip() {

        /*
         * @Date: 2021/10/17 23:38
         * Step 1: 部署xml(壓縮到zip形式,直接xml需要配置相對路徑,麻煩,暫不用)
         */
        try {
            File zipTemp = new File("f:/leave_approval.bpmn20.zip");
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipTemp));
            Deployment deployment = repositoryService
                    .createDeployment()
                    .addZipInputStream(zipInputStream)
                    .deploy();
            log.info("部署成功:{}", deployment.getId());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        /*
         * @Date: 2021/10/17 23:40
         * Step 2: 查詢部署的流程定義
         */
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").list();
        List<ProcessDefinition> pages = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").listPage(1, 30);

        /*
         * @Date: 2021/10/17 23:40
         * Step 3: 啟動流程,創建實例
         */
        String processDefinitionKey = "leave_approval";//流程定義的key,對應請假的流程圖
        String businessKey = "schoolleave";//業務代碼,根據自己的業務用
        Map<String, Object> variablesDefinition = new HashMap<>();//流程變量,可以自定義擴充
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variablesDefinition);
        log.info("啟動成功:{}", processInstance.getId());

        /*
         * @Date: 2021/10/17 23:40
         * Step 4: 查詢指定流程所有啟動的實例列表
         * 列表,或 分頁 刪除
         */
        List<Execution> executions = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").list();
        List<Execution> executionPages = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").listPage(1, 30);
//        runtimeService.deleteProcessInstance(processInstanceId, deleteReason); //刪除實例

        /*
         * @Date: 2021/10/17 23:40
         * Step 5: 學生查詢可以操作的任務,並完成任務
         */
        String candidateGroup = "stu_group"; //候選組 xml文件裡面的 flowable:candidateGroups="stu_group"
        List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).orderByTaskCreateTime().desc().list();
        for (Task task : taskList) {
            // 申領任務
            taskService.claim(task.getId(), "my");
            // 完成
            taskService.complete(task.getId());
        }

        /*
         * @Date: 2021/10/17 23:40
         * Step 6: 老師查詢可以操作的任務,並完成任務
         */
        String candidateGroupTe = "te_group"; //候選組 xml文件裡面的 flowable:candidateGroups="te_group"
        List<Task> taskListTe = taskService.createTaskQuery().taskCandidateGroup(candidateGroupTe).orderByTaskCreateTime().desc().list();
        for (Task task : taskListTe) {
            // 申領任務
            taskService.claim(task.getId(), "myte");
            // 完成
            Map<String, Object> variables = new HashMap<>();
            variables.put("command","agree"); //攜帶變量,用於網關流程的條件判定,這裡的條件是同意
            taskService.complete(task.getId(), variables);
        }

        /*
         * @Date: 2021/10/18 0:17
         * Step 7: 歷史查詢,因為一旦流程執行完畢,活動的數據都會被清空,上面查詢的接口都查不到數據,但是提供歷史查詢接口
         */
        // 歷史流程實例
        List<HistoricProcessInstance> historicProcessList = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave_approval").list();
        // 歷史任務
        List<HistoricTaskInstance> historicTaskList = historyService.createHistoricTaskInstanceQuery().processDefinitionKey("leave_approval").list();
        // 實例歷史變量 , 任務歷史變量
        // historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId);
        // historyService.createHistoricVariableInstanceQuery().taskId(taskId);

        // *****************************************************分隔符********************************************************************
        // *****************************************************分隔符********************************************************************
        // 可能還需要的API
        // 移動任務,人為跳轉任務
        // runtimeService.createChangeActivityStateBuilder().processInstanceId(processInstanceId)
        //       .moveActivityIdTo(currentActivityTaskId, newActivityTaskId).changeState();

        // 如果在資料庫配置了分組和用戶,還會用到
        List<User> users = identityService.createUserQuery().list();    //用戶查詢,用戶id對應xml 裡面配置的用戶
        List<Group> groups = identityService.createGroupQuery().list(); //分組查詢,分組id對應xml 裡面配置的分組 如 stu_group,te_group 在表里是id的值

        // 另外,每個查詢後面都可以拼條件,內置恁多查詢,包括模糊查詢,大小比較都有
    }
}
關鍵字: