再見Activity!SpringBoot+flowable完美結合,快速實現工作流

總覽

使用flowable自帶的flowable-ui製作流程圖

使用springboot開發流程使用的介面完成流程的業務功能

一、flowable-ui部署執行

flowable-6。6。0 執行 官方demo

參考文件:

https://flowable。com/open-source/docs/bpmn/ch14-Applications/

1、從官網下載flowable-6。6。0 :

https://github。com/flowable/flowable-engine/releases/download/flowable-6。6。0/flowable-6。6。0。zip

2、將壓縮包中的

flowable-6。6。0\wars\flowable-ui。war

丟到Tomcat中跑起來

3、開啟

http://localhost:8080/flowable-ui

用賬戶:admin/test 登入

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

4、進入APP。MODELER建立流程,之後可以匯出流程到專案中使用,或者配置

apache-tomcat-9。0。37\webapps\flowable-ui\WEB-INF\classes\flowable-default。properties

連線本地資料庫

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

注意:需要將java驅動jar(

mysql-connector-java-5。1。45。jar

)複製到

apache-tomcat-9。0。37\webapps\flowable-rest\WEB-INF\lib

這樣建立的流程後端程式就能直接使用

二、繪製流程圖

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

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

事件(event)

通常用於為流程生命週期中發生的事情建模,圖裡是【開始、結束】兩個圈。

順序流(sequence flow)

是流程中兩個元素間的聯結器。圖裡是【箭頭線段】。

閘道器(gateway)

用於控制執行的流向。圖裡是【菱形(中間有X)】

使用者任務(user task)

用於對需要人工執行的任務進行建模。圖裡是【矩形】。

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

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

繪圖細節:

1、保留流程模型

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

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

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

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

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

最後匯出工作流程檔案

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

檔案內容

<?xml version=“1。0” encoding=“UTF-8”?> <![CDATA[${command==‘agree’}]]> <![CDATA[${command==‘refuse’}]]> <![CDATA[${command==‘agree’}]]> <![CDATA[${command==‘refuse’}]]> 這裡先省略

4、bpmn檔案匯入

如果需要,可以把這個流程檔案下載下來,直接匯入使用

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

三、後臺專案搭建

後臺專案基於jdk8,使用springboot框架

spring 版本

org。springframework。boot spring-boot-starter-parent 2。3。0。RELEASE <!—— lookup parent from repository ——>

專案依賴pom。xml

org。flowable flowable-spring-boot-starter 6。6。0 mysql mysql-connector-java 5。1。45

專案配置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_

: 通用資料。在多處使用。

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。

再見Activity!SpringBoot+flowable完美結合,快速實現工作流

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 */@Slf4jpublic 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 list = repositoryService。createProcessDefinitionQuery()。processDefinitionKey(“leave_approval”)。list(); List 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 variablesDefinition = new HashMap<>();//流程變數,可以自定義擴充 ProcessInstance processInstance = runtimeService。startProcessInstanceByKey(processDefinitionKey, businessKey, variablesDefinition); log。info(“啟動成功:{}”, processInstance。getId()); /* * @Date: 2021/10/17 23:40 * Step 4: 查詢指定流程所有啟動的例項列表 * 列表,或 分頁 刪除 */ List executions = runtimeService。createExecutionQuery()。processDefinitionKey(“leave_approval”)。list(); List 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 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 taskListTe = taskService。createTaskQuery()。taskCandidateGroup(candidateGroupTe)。orderByTaskCreateTime()。desc()。list(); for (Task task : taskListTe) { // 申領任務 taskService。claim(task。getId(), “myte”); // 完成 Map variables = new HashMap<>(); variables。put(“command”,“agree”); //攜帶變數,用於閘道器流程的條件判定,這裡的條件是同意 taskService。complete(task。getId(), variables); } /* * @Date: 2021/10/18 0:17 * Step 7: 歷史查詢,因為一旦流程執行完畢,活動的資料都會被清空,上面查詢的介面都查不到資料,但是提供歷史查詢介面 */ // 歷史流程例項 List historicProcessList = historyService。createHistoricProcessInstanceQuery()。processDefinitionKey(“leave_approval”)。list(); // 歷史任務 List 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 users = identityService。createUserQuery()。list(); //使用者查詢,使用者id對應xml 裡面配置的使用者 List groups = identityService。createGroupQuery()。list(); //分組查詢,分組id對應xml 裡面配置的分組 如 stu_group,te_group 在表裡是id的值 // 另外,每個查詢後面都可以拼條件,內建恁多查詢,包括模糊查詢,大小比較都有 }}

五、參考資料

分享牛Flowable文件漢化:https://github。com/qiudaoke/flowable-userguide

貓七姑娘 flowable-6。6。0 執行官方 demo

華格瑞沙 https://www。cnblogs。com/yangjiming/p/10938515。html