资源地址
github : https://github.com/flowable/flowable-engine
flowable是什么
flowable是流程管理开源解决方案,从Activiti中分离出来的;由原 Activiti 的核心开发团队创建,旨在提供一个更加轻量级、更专注于核心业务流程管理的解决方案。flowable仅仅关注流程怎么走,不关心流程状态变化,更像一个代码编排工具,数据的可见性,状态流转,操作权限等还是由业务实现。
整合
由于flowable自己的表有70多张,不适合与业务使用同一个库,这里使用分库部署
- pom.xml
xml
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.suozq</groupId>
<artifactId>workflow</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.18</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<properties>
<java.version>8</java.version>
<flowable.version>6.8.1</flowable.version>
<!-- 可选:锁定 MySQL 驱动版本 -->
<mysql.version>8.0.33</mysql.version>
</properties>
<dependencies>
<!-- Web 支持 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Flowable -->
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter</artifactId>
<version>${flowable.version}</version>
</dependency>
<!-- MySQL 驱动 -->
<dependency>
<groupId>com.mysql</groupId>
<artifactId>mysql-connector-j</artifactId>
<scope>runtime</scope>
</dependency>
<!-- (可选)Actuator 监控 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- (可选)Lombok 简化代码 -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<!-- 测试 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
- application.yml
yml
spring:
datasource:
url: jdbc:mysql://localhost:3306/biz_db?characterEncoding=UTF-8&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
flowable:
database-schema-update: true
datasource:
jdbc-url: jdbc:mysql://localhost:3306/flowable_db?useSSL=false&nullCatalogMeansCurrent=true&serverTimezone=Asia/Shanghai&characterEncoding=utf8
username: root
password: root
driver-class-name: com.mysql.cj.jdbc.Driver
activity-font-name: "宋体"
annotation-font-name: "宋体"
label-font-name: "宋体"
database-schema-update:
- true : 启动时检测是否有flowable相关表,没有创建,有就校验;
- false: 启动时不创建表,生产环境;
- create-drop:启动时创建,关闭时删除,测试环境;
- drop-create:启动时先删除再创建,测试环境;
- FlowableConfig.java
java
package com.suozq.workflow.config;
import javax.sql.DataSource;
import org.flowable.app.spring.SpringAppEngineConfiguration;
import org.flowable.spring.boot.EngineConfigurationConfigurer;
import org.flowable.spring.boot.ProcessEngineAutoConfiguration;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Import;
import org.springframework.jdbc.datasource.DataSourceTransactionManager;
import org.springframework.lang.NonNull;
import org.springframework.transaction.PlatformTransactionManager;
import com.zaxxer.hikari.HikariDataSource;
@Configuration
public class FlowableConfig implements EngineConfigurationConfigurer<SpringAppEngineConfiguration>{
@Bean
@ConfigurationProperties(prefix = "flowable.datasource")
@NonNull
public DataSource flowableDataSource() {
DataSource dataSource = new HikariDataSource();
return dataSource;
}
/**
* 配置Flowable专用事务管理器
*
* @param flowableDataSource Flowable数据源
* @return Flowable事务管理器
*/
@Bean
public PlatformTransactionManager flowableTransactionManager() {
return new DataSourceTransactionManager(flowableDataSource());
}
@Override
public void configure(SpringAppEngineConfiguration engineConfiguration) {
engineConfiguration.setDataSource(flowableDataSource());
engineConfiguration.setTransactionManager(flowableTransactionManager());
}
}
- FlowableServiceExample.java
java
@Service
public class FlowableServiceExample {
@Autowired
private RepositoryService repositoryService; // 流程定义管理
@Autowired
private RuntimeService runtimeService; // 运行时流程实例
@Autowired
private TaskService taskService; // 任务管理
@Autowired
private HistoryService historyService; // 历史数据
@Autowired
private ManagementService managementService; // 引擎管理
@Autowired
private DynamicBpmnService dynamicBpmnService; // 动态修改
// 1. 部署流程定义
public Deployment deployProcess() {
return repositoryService.createDeployment()
.addClasspathResource("processes/leave.bpmn20.xml")
.name("请假流程")
.deploy();
}
// 2. 启动流程实例
public ProcessInstance startProcess(String processKey,
Map<String, Object> variables) {
return runtimeService.startProcessInstanceByKey(
processKey, variables);
}
// 3. 查询
public List<Task> getUserTasks(String userId) {
return taskService.createTaskQuery()
.taskAssignee(userId)
.active()
.list();
}
//完成任务
public void completeTask(String taskId,
Map<String, Object> variables) {
taskService.complete(taskId, variables);
}
// 4. 历史数据查询
public List<HistoricProcessInstance> getProcessHistory() {
return historyService.createHistoricProcessInstanceQuery()
.finished()
.orderByProcessInstanceEndTime().desc()
.list();
}
}
前端
从github下载整个压缩包后,里面有flowable-ui.war, 在同级目录下,添加application.properties配置文件,配置数据库为上面的flowable_db,
java
server.port=8081
spring.datasource.url=jdbc:mysql://127.0.0.1:3306/flowable_db?useUnicode=true&characterEncoding=utf8&serverTimezone=Asia/Shanghai
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.driverClassName=com.mysql.cj.jdbc.Driver
flowable.database-schema-update=false

使用java -jar flowable-ui.war 命令启动,即可通过页面配置流程;配置好后,创建应用,关联流程,发布,即可在程序中使用该流程了;
坑点
- 启动报错
no flowable tables in db与Table 'xxxx.act_ge_property' doesn't exist
这种情况多主要原因就是flowable判断表是否存在的语句有漏洞,没有明确指定库名!相同物理库下,一旦也有flowable的表,就会认为表已经创建!
解决方案(仅限MySQL): 数据库url 添加参数&nullCatalogMeansCurrent=true
该参数代表如果不指定库名,则自动使用当前库名