SpringBoot整合Activiti工作流框架的使用
Activiti 介紹
Activiti是一個開源的工作流引擎,它實現(xiàn)了BPMN 2.0規(guī)范,可以發(fā)布設(shè)計好的流程定義,并通過api進(jìn)行流程調(diào)度。Activiti作為一個遵從Apache許可的工作流和業(yè)務(wù)流程管理開源平臺,其核心是基于Java的超快速、超穩(wěn)定的BPMN2.0流程引擎,強調(diào)流程服務(wù)的可嵌入性和可擴(kuò)展性,同時更加強調(diào)面向業(yè)務(wù)人員。- 簡單來說
activiti是一個業(yè)務(wù)流程管理引擎,會沿著設(shè)計者設(shè)計好的流程,一步一步的執(zhí)行下去,直到終點。
SpringBoot 整合
配置
activiti會框架會創(chuàng)建一系列的表,所以要配置相關(guān)數(shù)據(jù)庫的信息,需要注意的是,在url中,添加了針對數(shù)據(jù)庫的條件,其中最后一條nullCatalogMeansCurrent=true非常重要,至于有什么用就不概述了,但是沒有這條語句的話就無法自動創(chuàng)建對應(yīng)的二十八張表。
server:
port: 8014
spring:
application:
name: workflow
datasource:
name: mysqlDatasource
url: jdbc:mysql://localhost:3306/core?useUnicode=true&nullCatalogMeansCurrent=true
username: root
password: root
driver-class-name: com.mysql.jdbc.Driver
type: com.alibaba.druid.pool.DruidDataSource
# 監(jiān)控統(tǒng)計攔截的filters,如果啟用log4j記得添加依賴
filters: stat,wall
# activiti
activiti:
#每次應(yīng)用啟動不檢查Activiti數(shù)據(jù)表是否存在及版本號是否匹配,提升應(yīng)用啟動速度
database-schema-update: true
#在項目單獨作為一個引擎,本身不部署流程的時候,如果resources目錄沒有“processes”目錄,啟動項目報錯–找不到processes目錄。需要在配置文件中添加以下內(nèi)容:
check-process-definitions: false
process-definition-location-prefix: classpath:/processes/
process-definition-location-suffixes:
-**.bpmn
-**.bpmn20.xml
#保存歷史數(shù)據(jù)級別設(shè)置為full最高級別,便于歷史數(shù)據(jù)的追溯
history-level: full
# activiti 安全訪問
security:
basic:
enabled: true
user:
name: root
password: root
版本問題
- 注意
SpringBoot和Activiti的版本問題 springboot2.0不能與activiti6.0.0直接集成使用,因為activiti6.0.0出來的時候springboot2.0還沒有出來,activiti6.0.0支持springboot1.2.6以上,2.0.0以下的版本。
使用 starter
依賴
這個版本滿足高版本的springboot,直接使用就行
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M3.1</version>
</dependency>
需要注意的是,這里的依賴版本,需要對應(yīng)數(shù)據(jù)庫中act_ge_property表中schema.version版本信息,所以一般不建議在創(chuàng)建完表之后修改依賴信息
啟動項目成功后自動創(chuàng)建表

需要在配置文件中加上 activiti-security的配置
# activiti 安全訪問
security:
basic:
enabled: true
user:
name: root
password: root
不使用 starter
依賴
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring</artifactId>
<version>6.0.0</version>
</dependency>
配置代碼
@Configuration
public class ActivitiConfig {
private static final Logger logger = LoggerFactory.getLogger(ActivitiConfig.class);
/**
* 配置分為以下幾步驟
* 1. 創(chuàng)建ActivitiConfig
* 2. 使用ActivitiConfig創(chuàng)建ProcessEngineFactoryBean
* 3. 使用ProcessEngineFactoryBean創(chuàng)建ProcessEngine對象
* 4. 使用ProcessEngine對象創(chuàng)建需要的服務(wù)對象
*/
private final DataSource dataSource;
private final PlatformTransactionManager platformTransactionManager;
@Autowired
public ActivitiConfig(DataSource dataSource, PlatformTransactionManager transactionManager) {
this.dataSource = dataSource;
platformTransactionManager = transactionManager;
}
/*
* 1. 創(chuàng)建配置文件,也就是提供一些配置信息,這樣就可以自定義自己的創(chuàng)建信息了
* 需要一些參數(shù),1. 數(shù)據(jù)源。2. 事務(wù)管理器。
* 這里還加入了自動掃描process包下的bpmn(流程定義文件)的設(shè)置,這樣就可以省去了部署
* */
@Bean
public SpringProcessEngineConfiguration springProcessEngineConfiguration() {
SpringProcessEngineConfiguration spec = new SpringProcessEngineConfiguration();
spec.setDataSource(dataSource);
spec.setTransactionManager(platformTransactionManager);
spec.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
Resource[] resources = null;
// 啟動自動部署流程
try {
resources = new PathMatchingResourcePatternResolver().getResources("classpath*:processes/*.*.xml");
} catch (IOException e) {
logger.error("Error Occur:", e);
}
spec.setDeploymentResources(resources);
return spec;
}
@Bean
public ProcessEngineFactoryBean processEngine() {
ProcessEngineFactoryBean engineFactoryBean = new ProcessEngineFactoryBean();
engineFactoryBean.setProcessEngineConfiguration(springProcessEngineConfiguration());
return engineFactoryBean;
}
@Bean
public RepositoryService repositoryService() throws Exception {
return Objects.requireNonNull(processEngine().getObject()).getRepositoryService();
}
@Bean
public RuntimeService runtimeService() throws Exception {
return Objects.requireNonNull(processEngine().getObject()).getRuntimeService();
}
@Bean
public TaskService taskService() throws Exception {
return Objects.requireNonNull(processEngine().getObject()).getTaskService();
}
@Bean
public HistoryService historyService() throws Exception {
return Objects.requireNonNull(processEngine().getObject()).getHistoryService();
}
}
在resources中創(chuàng)建process文件夾,文件夾的路徑和名字需要和ActivitiConfig中的配置保持一致啟動springBoot項目即可創(chuàng)建完成
使用 Activiti
Idea 安裝 Activiti BPMN visualizer 插件
編寫測試 bpmn.xml
<?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-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:activiti="http://activiti.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.activiti.org/processdef">
<process id="test" name="test" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="HelloWorld" activiti:assignee="goxcheer"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_test">
<bpmndi:BPMNPlane bpmnElement="test" id="BPMNPlane_test">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="41.0" x="220.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="640.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="390.0" y="170.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="261.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="390.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="495.0" y="197.0"></omgdi:waypoint>
<omgdi:waypoint x="640.0" y="197.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
編寫測試代碼
測試代碼
@RequestMapping("/test")
@RestController
public class ActivitiTestController {
private static final Logger logger = LoggerFactory.getLogger(ActivitiTestController.class);
@Autowired
RuntimeService runtimeService;
@Autowired
private TaskService taskService;
@RequestMapping("/test1")
public void test1() {
logger.info("Start.........");
ProcessInstance pi = runtimeService.startProcessInstanceByKey("test");
logger.info("流程啟動成功,流程id:{}", pi.getId());
}
@RequestMapping("/test2")
public void test2() {
String userId = "root";
List<Task> resultTask = taskService.createTaskQuery().processDefinitionKey("test").taskCandidateOrAssigned(userId).list();
logger.info("任務(wù)列表:{}", resultTask);
}
}
…簡單配置到此結(jié)束
完整配置代碼見 :https://gitee.com/Marlon_Brando/onlineshop_back/tree/develop/os_workflow
到此這篇關(guān)于SpringBoot整合Activiti工作流框架的使用的文章就介紹到這了,更多相關(guān)SpringBoot Activiti工作流內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java foreach循環(huán)為什么不能賦值的講解
這篇文章主要介紹了java foreach循環(huán)為什么不能賦值的講解,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-09-09
springboot實現(xiàn)防重復(fù)提交和防重復(fù)點擊的示例
這篇文章主要介紹了springboot實現(xiàn)防重復(fù)提交和防重復(fù)點擊的示例,幫助大家更好的理解和學(xué)習(xí)springboot框架,感興趣的朋友可以了解下2020-09-09
Springcould多模塊搭建Eureka服務(wù)器端口過程詳解
這篇文章主要介紹了Springcould多模塊搭建Eureka服務(wù)器端口過程詳解,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11
Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn)
這篇文章主要介紹了Java 迪杰斯特拉算法實現(xiàn)查找最短距離的實現(xiàn),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-09-09
spring-cloud-stream結(jié)合kafka使用詳解
這篇文章主要介紹了spring-cloud-stream結(jié)合kafka使用詳解,本文通過實例代碼給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友可以參考下2020-08-08

