背景
公司在做一款政府类项目,其中涉及到了工作流程审批的相关工作,最初是自己编码,通过数据库记录一些中间状态,来实现流程审批的相关工作,但是随着业务的变更以及各地区对于业务的审批流程存在差异,审批流程难以满足业务要求,因此通过对比目前各大工作流引擎,最终选择使用Flowable来作为项目的工作流引擎,主要是因为
- Flowable可以兼容Activiti,实现Activiti零成本迁移到Flowable,如果有Activiti相关的经验,很容易掌握Flowable,学习成本较低。同时Flowable修复了Activiti的一些Bug,相当于Flowable比Activiti更加先进一些。站在巨人的肩膀上总归是好的。
- Flowable的文档还是比较容易理解和学习的,工作流中的一些基本概念、相关api方法、项目环境搭建,很多都有详细的介绍和代码实现。跟着文档做就可以实现简单的流程。相关资源链接文章底部
项目运行环境
引入相关依赖
在搭建环境的时候,我使用的是springboot2.4+mybatisPlus3.4.0+mysql8.0,下面是我pom.xml的依赖
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.4.5</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.47</version>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>druid</artifactId>
<version>1.1.10</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.75</version>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>6.3.0</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-boot-starter</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.baomidou</groupId>
<artifactId>mybatis-plus-generator</artifactId>
<version>3.4.0</version>
</dependency>
<dependency>
<groupId>com.github.pagehelper</groupId>
<artifactId>pagehelper-spring-boot-starter</artifactId>
<version>1.2.12</version>
<exclusions>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis</artifactId>
</exclusion>
<exclusion>
<groupId>org.mybatis</groupId>
<artifactId>mybatis-spring</artifactId>
</exclusion>
<exclusion>
<groupId>com.github.jsqlparser</groupId>
<artifactId>jsqlparser</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.apache.velocity</groupId>
<artifactId>velocity-engine-core</artifactId>
<version>2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-commons</artifactId>
<version>2.0.8.RELEASE</version>
<scope>compile</scope>
</dependency>
<!-- 工具类 -->
<dependency>
<groupId>cn.hutool</groupId>
<artifactId>hutool-all</artifactId>
<version>5.5.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
</dependencies>
这里需要注意,flowable的依赖中,已经依赖了Mybatis,当在引入MybatisPlus时,由于也引入了Mybatis,可能会存在依赖冲突。解决办法是通过使用exclusion来排除掉冲突的依赖:例如项目中使用 mybatisPlus,那Flowable可以排除掉Mybatis的依赖。可以通过idea的Maven–》Maven Dependencies按钮来查看是否存在依赖冲突:
进行配置
配置我使用的是yml文件的方式,可以根据个人的习惯来决定。配置数据源、mybatisPlus、flowable等相关信息
spring:
application:
name: myflowable
datasource:
type: com.alibaba.druid.pool.DruidDataSource
driver-class-name: com.mysql.jdbc.Driver
url: jdbc:mysql://127.0.0.1:3306/flowable001?useUnicode=true&characterEncoding=utf-8&useSSL=false
username: root
password: root
servlet:
multipart:
max-file-size: 5MB
jackson:
default-property-inclusion: non_null
flowable:
async-executor-activate: false
database-schema-update: true
mybatis-plus:
configuration:
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
cache-enabled: true
mapper-locations: mapper/*.xml
pagehelper:
helper-dialect: mysql
reasonable: true
support-methods-arguments: true
page-size-zero: true
params: count=countSql
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringProcessEngineConfiguration> {
@Override
public void configure(SpringProcessEngineConfiguration engineConfiguration) {
engineConfiguration.setActivityFontName("宋体");
engineConfiguration.setLabelFontName("宋体");
engineConfiguration.setAnnotationFontName("宋体");
engineConfiguration.setAsyncExecutorActivate(true);//开启异步
//事件监听器
/* List<FlowableEventListener> listenerList=new ArrayList<>();
listenerList.add(new MyListener());
engineConfiguration.setEventListeners(listenerList);*/
}
@Bean
public BpmnXMLConverter bpmnXmlConverter(){
return new BpmnXMLConverter();
}
}
启动
1.配置文件配置成功过后,可以启动项目。如果项目启动成功,数据库中会自动创建Flowable工作流引擎所使用的表,我使用的版本创建了60张表,其中有一些表在开发过程中我们可能使用不到。如果你在resources目录下创建processes目录,并且目录中存在对应的BPMN文件,则在项目启动时,flowable会自动将该流程文件进行部署。我在首次启动项目的时候,遇到了一个错误,大概说的是一个schema.version不匹配。我的处理办法是修改了数据库中act_id_property中schema.version的值,是该值与报错信息中的值一致。
其他
在整个过程中,我没有使用到表单的相关功能,由于Idea中对于画BPMN文件的支持也不是很好。我是通过网页在线编辑流程图,然后将生成的流程文件,拷贝到项目当中,在进行相关的修改。
在线编辑工作流:编辑工作流https://www.jq22.com/yanshi4014
flowable官方文档:flowable官方文档 https://flowable.com/open-source/docs/oss-introduction/
至此,就可以进行Flowable相关的开发工作了。后面我会逐一将我近期开发的过程给写出来供大家参考。目前我所开发的工作流程可以实现的效果:上传BPMN文件部署、根据流程定义名称发起流程;任务创建时发送短信;任务到期自动审批;驳回后打回到发起人等功能。
文章中如果中间哪里有不妥之处,请指正~