Flowable整合SpringBoot實(shí)現(xiàn)的示例代碼
一、SringBoot整合Flowable
1.引入依賴
SpringBoot使用2.7.1,親測3.3.0不能用,JDK使用1.8
建議slf4j版本如下,不會報(bào)錯(cuò),太高了報(bào)錯(cuò)
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.7.2</version>
<!--關(guān)閉自帶的權(quán)限認(rèn)證-->
<exclusions>
<exclusion>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-security</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>8.0.21</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid-spring-boot-starter</artifactId>
<version>1.2.1</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<!-- 日志-->
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.30</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>2.0.0</version>
</dependency>2.安裝流程圖繪制插件
Flowable BPMN visualizer

3.yml配置
server:
port: 8080
spring:
datasource:
driver-class-name: com.mysql.cj.jdbc.Driver
#nullCatalogMeansCurrent=true 設(shè)置為只查當(dāng)前連接的schema庫
url: jdbc:mysql://localhost:3306/flowable?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true
username: root
password: root
flowable:
#關(guān)閉定時(shí)任務(wù)
async-executor-activate: true
#數(shù)據(jù)庫表與flowable最新表不一致會進(jìn)行更新
database-schema-update: true
logging:
level:
org:
flowable: debug二、Spring環(huán)境下的應(yīng)用
1.流程部署與啟動
@Autowired
ProcessEngine processEngine;
@Autowired
RepositoryService repositoryService;
@Autowired
RuntimeService runtimeService;
@Autowired
TaskService taskService;
@Test
void deployFlow(){
//流程引擎的配置對象,關(guān)聯(lián)相關(guān)數(shù)據(jù)源
Deployment deploy = repositoryService.createDeployment()
//一次部署所有processes文件夾內(nèi)的流程
.name("第一次部署")
.deploy();
System.out.println("deploy.getId()="+deploy.getId());
}
/**
* 啟動流程實(shí)例
* 在流程定義表中動態(tài)維護(hù) act_re_procdef
*/
@Test
void startFlow(){
String processId="ask_for_leave.bpmn20:1:4";
String processKey="ask_for_leave.bpmn20";
//1.根據(jù)流程定義di啟動流程實(shí)例
runtimeService.startProcessInstanceById(processId);
//2.根據(jù)流程定義key啟動流程實(shí)例
//runtimeService.startProcessInstanceByKey(processKey);
}
/**
* 任務(wù)的審批
* 需要數(shù)據(jù):任務(wù)id
*/
@Test
void compeleteTask(){
taskService.complete("2506");
}每啟動一個(gè)流程,會在act_hi_procinst中維護(hù)一條數(shù)據(jù)。
啟動一個(gè)流程,可以在act_ru_task表中看到對應(yīng)的記錄,該表記錄的都是當(dāng)前待辦的記錄信息。
act_ru_execution記錄流程的分支
流程定義:相當(dāng)于Java中的類
流程實(shí)例:相當(dāng)于java中的對象
注意:在Spring環(huán)境下,Spring會自動掃描processes文件夾,若不指定文件路徑,則一次把所有bpmn流程全部部署,也就是一次創(chuàng)建所有流程定義。一個(gè)部署對應(yīng)多個(gè)流程定義
@Test
void deployFlow(){
Deployment deploy = repositoryService.createDeployment()
//如果再添加部署文件,會部署兩次,導(dǎo)致流程定義中出現(xiàn)新版本的流程定義
.addClasspathResource("processes/ask_for_leave.bpmn20.xml")
.name("部署名稱")
.deploy();
System.out.println("deploy.getId()="+deploy.getId());
}常用:1.通過bpmn部署。2.通過zip壓縮包部署。3.自己創(chuàng)建model模型,然后保存到數(shù)據(jù)庫中,再通過模型部署。

創(chuàng)建好model之后保存到act_re_model表中(上圖僅僅是舉個(gè)3的例子)。部署也是先從表中獲取model部署。
2.表結(jié)構(gòu)
act_re: repository,包含流程定義和流程靜態(tài)資源(圖片,規(guī)則等)
act_ru: runtime,運(yùn)行時(shí)的表,包含實(shí)例,任務(wù),變量,異步任務(wù),運(yùn)行中的數(shù)據(jù)等
act_hi:history,流程的歷史數(shù)據(jù),如歷史流程實(shí)例,變量,任務(wù)。
- act_ge:general,通用數(shù)據(jù)。

act_ge_bytearray(重要),存放了流程定義的png圖片。
act_id:identity組織機(jī)構(gòu)。包含標(biāo)識的信息,如用戶,用戶組等。
CMMN 表示這都是跟 CMMN 協(xié)議相關(guān)的表。
CO(CONTENT)表示這都是跟內(nèi)容引擎相關(guān)的表。
DMN 表示這都是跟 DMN 協(xié)議相關(guān)的表。
FO(FORM)表示這都是跟表單相關(guān)的表。
3.流程掛起和激活
/**
* 流程定義的掛起和激活
*act_re_procdef
*/
@Test
void suspendedActivity(){
String processDefinitionId="ask_for_leave.bpmn20:1:4";
ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
.processDefinitionId(processDefinitionId)
.singleResult();
//獲取當(dāng)前流程定義的狀態(tài)
boolean suspended = processDefinition.isSuspended();
if (suspended) {
//掛起-->激活
System.out.println("激活流程");
repositoryService.activateProcessDefinitionById(processDefinitionId);
}else {
//激活-->掛起
System.out.println("掛起流程");
repositoryService.suspendProcessDefinitionById(processDefinitionId);
}
}已經(jīng)掛起的流程定義,不允許啟動。
已經(jīng)啟動的流程,不受影響。
可以掛起已經(jīng)啟動的流程實(shí)例,該流程實(shí)例不允許審批。
4.任務(wù)分配表達(dá)式Assignee

Assignee也可以寫表達(dá)式
值表達(dá)式
${ assign1 },assign1是自己定義的變量。
方法表達(dá)式
${ myBean.getAssignee() }
/**
* 任務(wù)的審批
* 需要數(shù)據(jù):任務(wù)id,hashMap "assign1","lisi"
* 把當(dāng)前任務(wù)分給lisi審批,運(yùn)行后lisi可以查到待辦
*/
@Test
void compeleteAssign1(){
HashMap<String, Object> variables = new HashMap<>();
variables.put("assign1","lisi");
//完成任務(wù)審批,根據(jù)任務(wù)id綁定對應(yīng)表達(dá)式的值
taskService.complete("taskId",variables);
}
(act_ru_actinst)出現(xiàn)了lisi
@Test
void findFlow(){
//任務(wù)實(shí)例通過TaskService來實(shí)現(xiàn)
TaskService taskService = getEngine().getTaskService();
//獲取到 act_ru_task中 assignee是lisi的記錄
List<Task> tasks = taskService.createTaskQuery()
.taskAssignee("lisi")
.list();
tasks.forEach(System.out::println);
//Task[id=15004, name=second]
}得到結(jié)果Task[id=15004, name=second],此時(shí)還需要審批。
------ 方法表達(dá)式${ myBean.getAssignee() } -------
//MyBean加入容器
@Component
public class MyBean {
public String getAssignee(){
System.out.println("getAssignee執(zhí)行...");
return "王五";
}
}
?
@Test
void compeleteAssign1(){
TaskService taskService = getEngine().getTaskService();
//再執(zhí)行審批時(shí),會去MyBean中執(zhí)行該方法
taskService.complete("15004");
}
拿到id后再執(zhí)行審批。
5.流程變量
運(yùn)行時(shí)變量
全局變量
局部變量
歷史變量
@Test void startFlow(){ String processId="firstFlow:2:637173cf-1ce6-11ef-8399-005056c00008"; //在流程啟動時(shí)就可以綁定對應(yīng)表達(dá)式的值,因?yàn)榈谝粋€(gè)人就需要指定人 Map<String,Object> variables =new HashMap<>(); variables.put("var1","test1"); variables.put("var2","test2"); variables.put("var3","test3"); //全局變量存在了act_ru_variable runtimeService.startProcessInstanceById(processId,variables); } /** * 獲取流程全局變量 */ @Test //全局變量存在了act_ru_variable void getVariables(){ String execution ="06a934bf-1ce7-11ef-870f-005056c00008"; //還能直接設(shè)置 //runtimeService.setVariable(execution,"var4","test4"); //設(shè)置局部變量,和taskId相關(guān),節(jié)點(diǎn)沒了變量就沒了 //runtimeService.setVariablesLocal(execution,"var4","test4"); Map<String, Object> variables = runtimeService.getVariables(execution); System.out.println(variables); }
輸出{var3=test3, var2=test2, var1=test1}
6.候選人

可以指定多個(gè)候選人,在啟動流程時(shí)進(jìn)行賦值就好。
/**
* 根據(jù)候選人查詢?nèi)蝿?wù)
* 候選人需要拾取任務(wù)才能變成審批人
*只有一個(gè)人能變?yōu)閷徟?,審批人還可以歸還,變成候選人
*/
@Test
void claimTask(){
//act_ru_task中
List<Task> tasks = taskService.createTaskQuery()
//這里有改變
.taskCandidateUser("張三")
.list();
for (Task task : tasks) {
//拾取
taskService.claim(task.getId(),"張三");
//歸還unclaim(task.getId(),"張三")
//指派taskService.setAssignee(task.getId(),"xxx")
}
}
@Test
void findFlow(){
//act_ru_task中
List<Task> tasks = taskService.createTaskQuery()
//這里有改變
.taskCandidateUser("張三")
.list();
tasks.forEach(System.out::println);
}7.候選人組
//先創(chuàng)建用戶
@Test
void createUser(){
User user = identityService.newUser("zhangsan");
user.setEmail("zhansgan@qq.com");
user.setFirstName("zhang");
user.setLastName("san");
user.setPassword("123456");
identityService.saveUser(user);
}
/**
* 用戶組
*/
@Test
void createGroup(){
Group group = identityService.newGroup("xsb");
group.setName("銷售部");
group.setType("type1");
identityService.saveGroup(group);
}
/**
* 用戶與用戶組的關(guān)系
*/
@Test
void createMemberShip(){
Group group = identityService.createGroupQuery().groupId("xsb").singleResult();
List<User> users = identityService.createUserQuery().list();
users.forEach(user -> {identityService.createMembership(user.getId(), group.getId());});
}act_id_group

act_id_membership


直接部署后啟動,act_ru_identitylink中就會出現(xiàn)候選組信息。

/**
* 當(dāng)前登錄用戶根據(jù)候選人組查詢?nèi)蝿?wù)
*/
@Test
void findGroupTask(){
//先查詢當(dāng)前所在的組 如查張三
Group group = identityService.createGroupQuery()
.groupMember("zhangsan").singleResult();
System.out.println("當(dāng)前用戶所在組的id為:"+group.getId());
List<Task> tasks = taskService.createTaskQuery().taskCandidateGroup(group.getId()).list();
for (Task task : tasks) {
//拾取任務(wù)
taskService.claim(task.getId(),"zhangsan");
}
}
/**
* 任務(wù)的審批
*/
@Test
void compeleteTask(){
Map<String,Object> variables =new HashMap<>();
taskService.complete("ae543ec1-1d5f-11ef-a409-005056c00008");
}8.網(wǎng)關(guān)
排他網(wǎng)關(guān)
并行網(wǎng)關(guān)
包容網(wǎng)關(guān)
事件網(wǎng)關(guān)
排他網(wǎng)關(guān)

在審批的時(shí)候加入day天數(shù)就能完成請假步驟,從而轉(zhuǎn)向不同的審批人。
@Test
void compeleteTask(){
Map<String,Object> variables =new HashMap<>();
variables.put("day",3);
taskService.complete("fae74a9a-1d6c-11ef-9a09-005056c00008");
}注意:條件盡量包含所有情況,否則報(bào)錯(cuò)。
并行網(wǎng)關(guān)

提交申請之后,task表中將會出現(xiàn)兩條審批,分別是zhangsan和lisi。

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

可以看成并行和排他的結(jié)合體
如num=2走三條,num=5走兩條,num=8走三條
Tips
每次重新部署一個(gè)id相同的bpmn時(shí),正在執(zhí)行的流程會被自動刪除。
流程定義與流程部署是不一樣的,一個(gè)流程部署可以對應(yīng)多個(gè)流程定義。流程部署的表是act_re_deployment,流程定義的表是act_re_procdef??梢岳斫鉃閍ct_re_procdef是act_re_deployment的從表。
流程定義不需要?jiǎng)h除,當(dāng)你不要這個(gè)流程定義時(shí),就把流程部署給刪除好了。
刪除流程部署,默認(rèn)級聯(lián)刪除,正在執(zhí)行的流程實(shí)例也會被刪除。
與Activiti7的區(qū)別
1.Activiti默認(rèn)不開啟數(shù)據(jù)庫的歷史記錄,flowable默認(rèn)開啟
2.Activiti23張表,flowable79張表。
3.Flowable是Activiti的繼任者,因此Flowable包含了Activiti的所有功能,并且在原有功能的基礎(chǔ)上進(jìn)行了進(jìn)一步的改進(jìn)和優(yōu)化。
4.支持 CMMN 和 DMN 標(biāo)準(zhǔn)
三、SpringBoot項(xiàng)目中引入flowable
1.配置flowable獨(dú)立數(shù)據(jù)源
flowable: async-executor-activate: false #第一次生成后關(guān)閉 database-schema-update: true #保存歷史數(shù)據(jù)級別 history-level: full #解決亂碼 activity-font-name: "宋體" annotation-font-name: "宋體" label-font-name: "宋體" #配置flowable數(shù)據(jù)源 flow: username: root password: root url: jdbc:mysql://localhost:3306/flowable2?serverTimezone=Asia/Shanghai&useUnicode=true&characterEncoding=utf-8&zeroDateTimeBehavior=convertToNull&useSSL=false&allowPublicKeyRetrieval=true&nullCatalogMeansCurrent=true&rewriteBatchedStatements=true driver-class-name: com.mysql.cj.jdbc.Driver maxPoolSize: 30
2.創(chuàng)建配置類
import com.alibaba.druid.pool.DruidDataSource;
import lombok.extern.slf4j.Slf4j;
import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.PropertySource;
?
import javax.sql.DataSource;
import java.beans.PropertyVetoException;
?
/**
* 一共有兩個(gè)配置類,一個(gè)是ProcessEngineConfiguration,一個(gè)是SpringAppEngineConfiguration
* 他們里面都需要重寫configure方法來進(jìn)行配置
* 配置數(shù)據(jù)源應(yīng)該在SpringAppEngineConfiguration中設(shè)定
* 前者是配置ProcessEngine的,如自動生成表,設(shè)置中文,在yml文件中配置的屬性便是在此類中讀取
*/
@Configuration
@PropertySource("classpath:application-dev.yml")
@Slf4j
?
public class FlowableConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration> {
?
//讀取配置
@Value("${flow.username}")
private String user;
@Value("${flow.password}")
String password;
@Value("${flow.url}")
String jdbcUrl;
@Value("${flow.driver-class-name}")
String driverClass;
@Value("${flow.maxPoolSize}")
int maxPoolSize;
?
// @Bean(name = "processEngine")
// public ProcessEngine processEngineConfiguration() throws PropertyVetoException {
// ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration();
// cfg.setDataSource(dataSource2());
// cfg.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_FALSE);
// cfg.setActivityFontName("宋體");
// cfg.setLabelFontName("宋體");
// cfg.setAnnotationFontName("宋體");
// cfg.setAsyncExecutorActivate(false);
// return cfg.buildProcessEngine();
// }
?
//配置數(shù)據(jù)源
public DataSource dataSource2() throws PropertyVetoException {
DruidDataSource dataSource = new DruidDataSource();
dataSource.setUsername(user);
dataSource.setPassword(password);
dataSource.setUrl(jdbcUrl);
dataSource.setDriverClassName(driverClass);
dataSource.setMaxPoolPreparedStatementPerConnectionSize(maxPoolSize);
return dataSource;
}
?
@Override
public void configure(SpringAppEngineConfiguration engineConfiguration) {
try {
//把數(shù)據(jù)源設(shè)置進(jìn)來
engineConfiguration.setDataSource(dataSource2());
log.info("配置flowable數(shù)據(jù)源成功");
} catch (PropertyVetoException e) {
throw new RuntimeException(e);
}
}
}注意這里配置的是SpringAppEngineConfiguration,而不是ProcessEngine,否則將出現(xiàn)報(bào)錯(cuò),或者設(shè)置單獨(dú)的數(shù)據(jù)源失敗。
項(xiàng)目啟動成功就可以看到自動創(chuàng)建的79張表了。然后把表的自動更新關(guān)閉,否則會影響性能。
到此這篇關(guān)于Flowable整合SpringBoot實(shí)現(xiàn)的示例代碼的文章就介紹到這了,更多相關(guān)Flowable整合SpringBoot內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)
這篇文章主要介紹了MAC算法之消息摘要算法HmacMD5的實(shí)現(xiàn)的相關(guān)資料,這里提供實(shí)例,幫助大家學(xué)習(xí)理解這部分知識,需要的朋友可以參考下2017-08-08
詳解Elastic Search搜索引擎在SpringBoot中的實(shí)踐
本篇文章主要介紹了Elastic Search搜索引擎在SpringBoot中的實(shí)踐,小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2018-01-01
java List循環(huán)與Map循環(huán)的總結(jié)
這篇文章主要介紹了java List循環(huán)與Map循環(huán)的總結(jié)的相關(guān)資料,并附代碼實(shí)例,幫助大家學(xué)習(xí)理解,需要的朋友可以參考下2016-11-11
純Java環(huán)境下實(shí)現(xiàn)將在線地圖路網(wǎng)數(shù)據(jù)轉(zhuǎn)為GeoJSON與WKT
這篇文章主要為大家詳細(xì)介紹了如何純Java環(huán)境下實(shí)現(xiàn)將在線地圖路網(wǎng)數(shù)據(jù)轉(zhuǎn)為GeoJSON與WKT,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以了解下2026-02-02
idea2020.1最新版永久破解/pycharm也可用(步驟詳解)
這篇文章主要介紹了idea2020.1最新版永久破解/pycharm也可用,本文給大家分享簡單實(shí)現(xiàn)步驟,通過圖文并茂的形式給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04

