Flowable 工作流引擎与 Spring Boot 整合指南
前言
在Java世界中,Activiti 和 Flowable 是工作流引擎的主流选择。Flowable 是 Activiti 的一个分支,两者用法相似。理想情况下,工作流引擎应提供 REST API 服务,实现引擎与服务的分离,使业务能够直接通过调用 REST API 来控制工作流。
依赖
Flowable 的所有依赖都可以在 Maven Central 找到。以下是一些主要依赖:
Group ID | Artifact ID | 说明 |
---|---|---|
org.flowable | flowable-spring-boot-starter | Spring Boot Flowable 依赖 |
org.flowable | flowable-spring-boot-starter-rest-api | Spring Boot Flowable 全部五个模块的 REST API 依赖(已废弃) |
org.flowable | flowable-spring-boot-starter-process-rest | Spring Boot Flowable 流程引擎 ProcessEngine REST API 依赖 |
org.flowable | flowable-spring-boot-starter-dmn-rest | Spring Boot Flowable 决策引擎 DmnEngine REST API 依赖 |
org.flowable | flowable-spring-boot-starter-cmmn-rest | Spring Boot Flowable CMMN 决策引擎 CMMNEngine API 依赖 |
与 Spring Boot 整合
以下是 pom.xml
文件的配置示例:
<?xml version="1.0" encoding="UTF-8"?>
<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>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.0.4.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>org.unreal</groupId>
<artifactId>flowable-rest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>pom</packaging>
<name>parent</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
</dependency>
<dependency>
<groupId>org.flowable</groupId>
<artifactId>flowable-spring-boot-starter-process-rest</artifactId>
<version>${flowable.version}</version>
</dependency>
</dependencies>
</project>
增加 RestResponseFactory 和 ContentTypeResolver 提供 REST 服务
@Bean
public RestResponseFactory restResponseFactory() {
return new RestResponseFactory(new ObjectMapper());
}
@Bean
public ContentTypeResolver contentTypeResolver() {
return new DefaultContentTypeResolver();
}
解决中文乱码
@Bean
SpringProcessEngineConfiguration processEngineConfiguration(SimpleDriverDataSource dataSource, DataSourceTransactionManager transactionManager, FormEngineConfiguration formEngineConfiguration, IdmEngineConfiguration idmEngineConfiguration) throws IOException {
SpringProcessEngineConfiguration cfg = new SpringProcessEngineConfiguration();
cfg.setDataSource(dataSource).setDatabaseSchemaUpdate("true").setAsyncExecutorActivate(true);
Resource[] resources = this.applicationContext.getResources("classpath:bpmn/process/*.bpmn20.xml");
String classPath = ResourceUtils.getURL("classpath:").getPath();
this.showResourceLog(resources);
cfg.setDeploymentResources(resources);
cfg.setTransactionManager(transactionManager);
FormEngineConfigurator formEngineConfigurator = new FormEngineConfigurator();
formEngineConfigurator.setFormEngineConfiguration(formEngineConfiguration);
cfg.addConfigurator(formEngineConfigurator);
IdmEngineConfigurator idmEngineConfigurator = new IdmEngineConfigurator();
idmEngineConfigurator.setIdmEngineConfiguration(idmEngineConfiguration);
cfg.addConfigurator(idmEngineConfigurator);
cfg.setActivityFontName("宋体"); // Windows 下使用中文字体,Linux 需要换成支持中文的字体
cfg.setLabelFontName("宋体");
cfg.setAnnotationFontName("宋体");
cfg.buildProcessEngine();
return cfg;
}
使用
官方 Tomcat 部署方式需要使用 IDM 登录或在 REST API 上使用 Basic auth 来使用 REST 服务。但本文介绍的方式可以直接访问,不需要验证。默认端口是 8080,请求地址如下:
http://127.0.0.1:8080/process-api/
process-api
依赖了 flowable-spring-boot-starter-process-rest
,dmn-api
需要依赖 flowable-spring-boot-starter-dmn-rest
,cmmn-api
需要依赖 flowable-spring-boot-starter-cmmn-rest
。
REST API 文档
官方网站上有 REST API 的详细说明,请访问: