SpringBoot 整合 Activiti 工作流框架
Activiti 介绍
-
Activiti 是一个开源的工作流引擎,它实现了BPMN 2.0 规范,可以发布设计好的流程定义,并通过api 进行流程调度。Activiti 作为一个遵从 Apache 许可的工作流和业务流程管理开源平台,其核心是基于 Java 的超快速、超稳定的 BPMN2.0 流程引擎,强调流程服务的可嵌入性和可扩展性,同时更加强调面向业务人员。 -
简单来说activiti 是一个业务流程管理引擎,会沿着设计者设计好的流程,一步一步的执行下去,直到终点。
SpringBoot 整合
配置
activiti 会框架会创建一系列的表,所以要配置相关数据库的信息,需要注意的是,在url 中,添加了针对数据库的条件,其中最后一条nullCatalogMeansCurrent=true 非常重要,至于有什么用就不概述了,但是没有这条语句的话就无法自动创建对应的二十八张表。
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
filters: stat,wall
activiti:
database-schema-update: true
check-process-definitions: false
process-definition-location-prefix: classpath:/processes/
process-definition-location-suffixes:
-**.bpmn
-**.bpmn20.xml
history-level: full
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>
- 需要在配置文件中加上
activiti-security 的配置
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);
private final DataSource dataSource;
private final PlatformTransactionManager platformTransactionManager;
@Autowired
public ActivitiConfig(DataSource dataSource, PlatformTransactionManager transactionManager) {
this.dataSource = dataSource;
platformTransactionManager = transactionManager;
}
@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 中创建process 文件夹,文件夹的路径和名字需要和ActivitiConfig 中的配置保持一致 - 启动
springBoot 项目即可创建完成
使用 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("任务列表:{}", resultTask);
}
}
…简单配置到此结束
完整配置代码见 :https://gitee.com/Marlon_Brando/onlineshop_back/tree/develop/os_workflow
|