
正文
SpringBoot系列——Activiti7工作流引擎
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
前言
工作流程是我们日常开发项目中常见的功能,本文记录springboot整合activiti7。
Activiti介绍
官网:https://www.activiti.org
数据库表
act_hi_*:'hi’表示 history,此前缀的表包含历史数据,如历史(结束)流程实例,变量,任务等等。
act_ge_*:'ge’表示 general,此前缀的表为通用数据,用于不同场景中。
act_evt_*:'evt’表示 event,此前缀的表为事件日志。
act_procdef_*:'procdef’表示 processdefine,此前缀的表为记录流程定义信息。
act_re_*:'re’表示 repository,此前缀的表包含了流程定义和流程静态资源(图片,规则等等)。
act_ru_*:'ru’表示 runtime,此前缀的表是记录运行时的数据,包含流程实例,任务,变量,异步任务等运行中的数据。Activiti只在流程实例执行过程中保存这些数据,在流程结束时就会删除这些记录。
| 表名 | 表注释 |
| act_ge_bytearray | 二进制数据表,存储通用的流程定义和流程资源。 |
| act_ge_property | 系统相关属性,属性数据表存储整个流程引擎级别的数据,初始化表结构时,会默认插入三条记录。 |
| act_re_deployment | 部署信息表 |
| act_re_model | 流程设计模型部署表 |
| act_re_procdef | 流程定义数据表 |
| act_ru_deadletter_job | 作业死亡信息表,作业失败超过重试次数 |
| act_ru_event_subscr | 运行时事件表 |
| act_ru_execution | 运行时流程执行实例表 |
| act_ru_identitylink | 运行时用户信息表 |
| act_ru_integration | 运行时积分表 |
| act_ru_job | 运行时作业信息表 |
| act_ru_suspended_job | 运行时作业暂停表 |
| act_ru_task | 运行时任务信息表 |
| act_ru_timer_job | 运行时定时器作业表 |
| act_ru_variable | 运行时变量信息表 |
| act_hi_actinst | 历史节点表 |
| act_hi_attachment | 历史附件表 |
| act_hi_comment | 历史意见表 |
| act_hi_detail | 历史详情表,提供历史变量的查询 |
| act_hi_identitylink | 历史流程用户信息表 |
| act_hi_procinst | 历史流程实例表 |
| act_hi_taskinst | 历史任务实例表 |
| act_hi_varinst | 历史变量表 |
| act_evt_log | 流程引擎的通用事件日志记录表 |
| act_procdef_info | 流程定义的动态变更信息 |
开发前准备
Idea安装actiBPM插件,用于绘制流程图

画流程图
创建bpmn文件,画一个简单的请假流程

代码编写
项目结构

引入依赖包
<!-- activiti -->
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-spring-boot-starter</artifactId>
<version>7.1.0.M5</version>
</dependency>
<dependency>
<groupId>org.activiti</groupId>
<artifactId>activiti-image-generator</artifactId>
<version>7.1.0.M5</version>
</dependency> <!--添加MySQL驱动依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jdbc</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency> <!-- thymeleaf模板 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
springboot整合的activiti,默认自带security框架,为了方便测试,我们直接用官方提供的config类

传统项目分层:controller层、service层

为了扩展、丰富原生的流程图生成器,创建自定义ProcessDiagramGenerator与ProcessDiagramCanvas

三个简单页面

1、流程发起,填写业务表单,发起一个请假流程

2、任务待办,查询指定用户的流程任务列表,并完成任务

3、查看流程,查询指定用户发起的流程列表,并查看实时流程图

效果演示自动建表
数据库无相关表,启动程序,就会自动创建相关的表,共25张

注:自动创建的表中,act_re_deployment少两个字段,部署流程时报错,补全即可(VERSION_、PROJECT_RELEASE_VERSION_)

流程部署
单元测试,部署刚才的请假流程
import org.activiti.engine.*;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringbootActiviti7ApplicationTests { @Autowired
private RepositoryService repositoryService; /**
* 流程部署
*/
@Test
public void test() {
repositoryService.createDeployment()
.addClasspathResource("bpm/askForLeaveBpm.bpmn")
.name("请假流程")
.key("ASK_FOR_LEAVE_ACT")
.deploy(); System.out.println("流程部署成功!");
}
}
流程发起
http://localhost:10010/activiti/index
发起两个请假流程,一个1天、一个5天

查看流程
http://localhost:10010/activiti/getHistoricProcessInstanceByUserName?username=zhangsan
查看指定用户发起的流程

任务待办
http://localhost:10010/activiti/queryUserTaskByUserName?username=wangwu
查看任务待办,项目经理:lisi、部门经理:wangwu,并完当前成流程节点,流程会自动推进直至流程结束

流程结束
key1,请假小于3天,项目经理李四审批后流程直接结束
key2,请假大于3天,项目经理审批后,到部门经理审批,然后流程才结束


代码开源
代码已经开源、托管到我的GitHub、码云:
GitHub:https://github.com/huanzi-qch/springBoot
码云:https://gitee.com/huanzi-qch/springBoot
后记
简单的整合实例暂时先记录到这,后续有空再补充。
参考
https://www.cnblogs.com/xuweiweiwoaini/p/13660394.html
https://www.jianshu.com/nb/41785678
https://blog.csdn.net/zhouchenjun001/article/details/103629559







