SpringBoot整合Activiti工作流框架的使用
作者:MarlonBrando1998 发布时间:2022-03-02 12:32:44
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,如果启用log4j记得添加依赖
filters: stat,wall
# activiti
activiti:
#每次应用启动不检查Activiti数据表是否存在及版本号是否匹配,提升应用启动速度
database-schema-update: true
#在项目单独作为一个引擎,本身不部署流程的时候,如果resources目录没有“processes”目录,启动项目报错–找不到processes目录。需要在配置文件中添加以下内容:
check-process-definitions: false
process-definition-location-prefix: classpath:/processes/
process-definition-location-suffixes:
-**.bpmn
-**.bpmn20.xml
#保存历史数据级别设置为full最高级别,便于历史数据的追溯
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>
需要注意的是,这里的依赖版本,需要对应数据库中act_ge_property
表中schema.version
版本信息,所以一般不建议在创建完表之后修改依赖信息
启动项目成功后自动创建表
需要在配置文件中加上 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. 创建ActivitiConfig
* 2. 使用ActivitiConfig创建ProcessEngineFactoryBean
* 3. 使用ProcessEngineFactoryBean创建ProcessEngine对象
* 4. 使用ProcessEngine对象创建需要的服务对象
*/
private final DataSource dataSource;
private final PlatformTransactionManager platformTransactionManager;
@Autowired
public ActivitiConfig(DataSource dataSource, PlatformTransactionManager transactionManager) {
this.dataSource = dataSource;
platformTransactionManager = transactionManager;
}
/*
* 1. 创建配置文件,也就是提供一些配置信息,这样就可以自定义自己的创建信息了
* 需要一些参数,1. 数据源。2. 事务管理器。
* 这里还加入了自动扫描process包下的bpmn(流程定义文件)的设置,这样就可以省去了部署
* */
@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
来源:https://blog.csdn.net/qq_37248504/article/details/122992229


猜你喜欢
- 我们主要介绍一下:java中如何通过最简单的方式实现链式创建json对象,解决创建json代码臃肿的问题。1、假设我们要创建一个json对象
- 环境介绍 IDEA我用的是2020.2Gradle 安装参考 Gradle安装配置我这安装的是6.6.1C:\Users\herion>
- 准备工作HALCON示例程序的描述部分一直是英文的,看起来很不方便。我决定汉化一下HALCON示例程序的描述,准备工作如下:拿到HALCON
- 1、什么是GradleGradle是一种结合了Ant和Maven两者优势的下一代构建工具,既有Ant构建灵活性的优点,也保留Maven约定优
- 微信公众号发送模版消息 背景:如下图,当用户发布需求的时候,公众号自定推送消息。例如:微信支付的时候,公众号会推送支付成功消息前提:发送模版
- 本文实例讲述了C#中DataSet转化为实体集合类的方法,分享给大家供大家参考。具体实现方法如下:/// <summary>//
- 随着Flash应用的流行,网上出现了多种在线Flash版本“连连看”。如“水晶连连看”、“果蔬连连看”等,流行的“水晶连连看”以华丽界面吸引
- 本文代码为原创一个简陋的管理系统,只做功能的测试。并没有去完善所有应有的功能,只做了输入输出查找,仅供参考! 菜单部分:
- 安装Jenkins提示:首先Jenkins安装方式有2中,一种是yum安装,另一种是使用war的方式进行安装(war就需要安装tomcat)
- 前文传送门:NioEventLoop处理IO事件执行任务队列继续回到NioEventLoop的run()方法:protected void
- 本文实例为大家分享了C#实现网页画图的具体代码,供大家参考,具体内容如下代码贴着保存下using System;using System.C
- dom4j是一个Java的XML API,类似于jdom,用来读写XML文件的。dom4j是一个非常非常优秀的Java XML API,具有
- BottomBarBottomBar是Github上的一个开源框架,因为从1.3.3开始不支持fragments了,要自己配置,弄了很久,不
- 如下所示:package java.util;public interface Enumeration<E> { boolean
- 具体代码如下所示:<?xml version="1.0"?><LinearLayout android
- 最近项目中用到了Elasticsearch5.4(ES)是比较新的一个版本,使用的过程中出现了很多的问题,很是头疼,但是问题最终还是解决掉了
- 本文实例为大家分享了java实现图片角度旋转并获得图片信息的具体代码,供大家参考,具体内容如下public class Demo {/**
- 在开源中国看到的操作ini文件的,写的还不看,留着以后用using System;using System.IO;using System.
- 前言在使用IDEA2020.2.3版本时,创建web工程遇到了一些问题,经过一番摸索之后得到解决方案。一、新建javaweb工程1.先创建一
- 先给大家展示下效果图,感觉不错请参考实例代码。实现思路在flutter中,如果想实现上面的页面切换效果,必然会想到pageView。page