【Java 开发日记】你会不会使用 SpringBoot 整合 Flowable 快速实现工作流呢?

使用 SpringBoot 整合 Flowable 快速实现工作流

关键要点

  • Flowable 简介:Flowable 是一个轻量级、基于 Java 的开源工作流引擎,支持 BPMN 2.0 标准,广泛用于业务流程管理(如请假、报销流程)。
  • Spring Boot 集成:通过 flowable-spring-boot-starter 依赖,Spring Boot 可以快速集成 Flowable,简化工作流开发。
  • 快速实现:通过配置依赖、数据库、流程引擎和 Flowable Modeler,可以在短时间内搭建一个功能完整的工作流系统。
  • 注意事项:关闭异步执行器和正确配置数据库是开发中的关键点,生产环境需谨慎管理 schema 和安全性。
  • 资源支持:推荐参考腾讯云开发者社区的教程和 Gitee 代码仓库,获取详细步骤和示例代码。

简介

Flowable 是一个强大的工作流引擎,适合处理复杂的业务流程,如 OA 系统中的请假或报销流程。结合 Spring Boot 的快速开发能力,可以轻松实现工作流功能。本文将提供一个简明易懂的中文教程,指导您快速整合 Spring Boot 和 Flowable,搭建一个基本的工作流系统。

实现步骤

以下是快速实现 Spring Boot 整合 Flowable 的步骤,适合初学者和希望快速上手的开发者。

1. 创建 Spring Boot 项目并添加依赖

创建一个新的 Spring Boot 项目(可以使用 Spring Initializr),并在 pom.xml 中添加以下 Flowable 依赖:

<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-spring-boot-starter</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-engine</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-api</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.slf4j</groupId>
        <artifactId>slf4j-log4j12</artifactId>
        <version>1.7.21</version>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-configuration-processor</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.0</version>
    </dependency>
    <dependency>
        <groupId>mysql</groupId>
        <artifactId>mysql-connector-java</artifactId>
        <version>8.0.27</version>
    </dependency>
</dependencies>

说明:使用 Flowable 6.4.1 版本,避免 6.4.2 版本的发布问题。添加 MySQL 驱动以支持数据库连接。

2. 配置数据库

Flowable 需要数据库存储流程定义和运行时数据。以下是使用 MySQL 的配置示例,在 application.yml 中添加:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
flowable:
  async-executor-activate: false
  database-schema-update: true

说明

  • async-executor-activate: false 关闭异步执行器,适合开发阶段。
  • database-schema-update: true 自动创建数据库表,生产环境建议手动管理。
3. 初始化 Flowable 流程引擎

通过代码初始化 Flowable 的流程引擎,确保 Spring Boot 能够与 Flowable 交互。

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlowableConfig {

    @Bean
    public ProcessEngine processEngine() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("password");
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        return configuration.buildProcessEngine();
    }
}

说明:此配置创建一个独立的流程引擎,连接到 MySQL 数据库。

4. 集成 Flowable Modeler

Flowable Modeler 是一个 Web 工具,用于可视化设计 BPMN 2.0 流程图。以下是集成步骤:

  • 下载 Flowable 6.4.1 源码,提取 flowable-ui-modeler-* 模块。
  • pom.xml 中添加 Modeler 依赖:
<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-rest</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-logic</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-conf</artifactId>
        <version>6.4.1</version>
    </dependency>
</dependencies>
  • 启动应用后,访问 http://localhost:8087/,使用默认账号 admin 和密码 test 登录。
5. 设计和部署流程

在 Flowable Modeler 中设计一个简单的流程(如请假流程),保存为 .bpmn 文件。然后通过代码部署流程:

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WorkflowController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @PostMapping("/deploy")
    public String deployProcess() {
        repositoryService.createDeployment()
            .addClasspathResource("leave.bpmn")
            .deploy();
        return "流程部署成功";
    }

    @PostMapping("/start")
    public String startProcess() {
        runtimeService.startProcessInstanceByKey("leave");
        return "流程启动成功";
    }
}

说明leave.bpmn 是流程定义文件,需放置在 resources 目录下。

6. 管理任务

使用 TaskService 查询和完成任务:

import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/tasks")
    public List<Task> getTasks() {
        return taskService.createTaskQuery().list();
    }

    @PostMapping("/complete/{taskId}")
    public String completeTask(@PathVariable String taskId) {
        taskService.complete(taskId);
        return "任务完成";
    }
}
7. 测试和部署
  • 使用 Postman 测试 REST API(如 /deploy/start/tasks)。
  • 生产环境部署时,确保数据库配置安全,关闭 database-schema-update: true

注意事项

  • 版本选择:使用 Flowable 6.4.1,避免 6.4.2 的问题。
  • 安全性:生产环境中修改 Flowable Modeler 的默认账号密码。
  • 数据库管理:生产环境手动创建数据库表,避免自动 schema 更新。

参考资源


使用 SpringBoot 整合 Flowable 快速实现工作流详解

背景与概述

Flowable 是一个轻量级、基于 Java 的开源工作流引擎,支持 BPMN 2.0 标准,广泛应用于 OA 系统、审批流程等场景。Spring Boot 是一个快速开发框架,通过 flowable-spring-boot-starter 依赖,可以轻松集成 Flowable,实现工作流功能。用户请求的“使用 SpringBoot 整合 Flowable 快速实现工作流中文讲解”表明需要一个简明、实用的中文教程,指导如何快速搭建工作流系统。

研究表明,Flowable 是 Activiti 的分支,继承了其核心功能,同时提供了更强大的功能,如 DMN 决策表和 CMMN 案例管理。结合 Spring Boot 的依赖管理和 REST API 支持,可以快速实现流程定义、部署、启动和任务管理。本文将详细讲解整合步骤,结合腾讯云开发者社区、CSDN 和 GitHub 等资源,提供全面的中文指导。

Flowable 与 Spring Boot 的关系

Flowable 是一个流程引擎,支持 BPMN 2.0 标准,用于定义和执行业务流程。Spring Boot 通过 flowable-spring-boot-starter 提供自动配置,简化了流程引擎的初始化和集成。Flowable Modeler 是一个 Web 工具,允许用户通过可视化界面设计流程图,生成 BPMN 文件,极大降低了开发难度。

实现步骤详解

1. 创建 Spring Boot 项目

使用 Spring Initializr 或 IDE(如 IntelliJ IDEA)创建一个 Spring Boot 项目,选择 Maven 或 Gradle 作为构建工具。确保项目包含 Web 和数据库依赖。

2. 添加 Flowable 依赖

pom.xml 中添加以下依赖:

<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-spring-boot-starter</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifact table_id="flowable-dependencies" caption="Flowable 相关依赖">
| 依赖名称                     | 版本   | 用途                     |
|-----------------------------|--------|-------------------------|
| flowable-spring-boot-starter | 6.4.1  | Spring Boot 集成 Flowable |
| flowable-engine             | 6.4.1  | Flowable 核心引擎        |
| slf4j-api                   | 1.7.21 | 日志框架                |
| slf4j-log4j12              | 1.7.21 | 日志实现                |
| mysql-connector-java        | 8.0.27 | MySQL 数据库驱动        |

说明flowable-spring-boot-starter 提供 REST API 和自动配置,flowable-engine 是核心引擎。SLF4J 和 Log4j 用于日志管理。

3. 配置数据库

Flowable 需要数据库存储流程定义、运行时数据和历史数据。以下是 MySQL 配置示例:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8
    username: root
    password: password
    driver-class-name: com.mysql.cj.jdbc.Driver
flowable:
  async-executor-activate: false
  database-schema-update: true

关键配置

  • async-executor-activate: false:关闭异步执行器,确保开发阶段数据插入同步。
  • database-schema-update: true:自动创建数据库表,生产环境建议手动执行 SQL 脚本。
4. 初始化流程引擎

通过代码或 XML 配置初始化 Flowable 流程引擎。以下是代码初始化示例:

import org.flowable.engine.ProcessEngine;
import org.flowable.engine.ProcessEngineConfiguration;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class FlowableConfig {

    @Bean
    public ProcessEngine processEngine() {
        ProcessEngineConfiguration configuration = ProcessEngineConfiguration.createStandaloneProcessEngineConfiguration();
        configuration.setJdbcUrl("jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8");
        configuration.setJdbcUsername("root");
        configuration.setJdbcPassword("password");
        configuration.setJdbcDriver("com.mysql.cj.jdbc.Driver");
        configuration.setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        return configuration.buildProcessEngine();
    }
}

说明:此配置创建一个独立的流程引擎,连接到 MySQL 数据库。生产环境中,可以使用 Spring Boot 的自动配置。

5. 集成 Flowable Modeler

Flowable Modeler 是一个可视化工具,用于设计 BPMN 2.0 流程图。集成步骤如下:

  • 下载源码:从 Flowable 官网下载 6.4.1 版本,提取 flowable-ui-modeler-* 模块。
  • 添加依赖
<dependencies>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-rest</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-logic</artifactId>
        <version>6.4.1</version>
    </dependency>
    <dependency>
        <groupId>org.flowable</groupId>
        <artifactId>flowable-ui-modeler-conf</artifactId>
        <version>6.4.1</version>
    </dependency>
</dependencies>
  • 访问 Modeler:启动应用后,访问 http://localhost:8087/,使用 admin/test 登录。
6. 设计和部署流程

在 Flowable Modeler 中设计流程(如请假流程),保存为 .bpmn 文件。以下是部署和启动流程的代码:

import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class WorkflowController {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @PostMapping("/deploy")
    public String deployProcess() {
        repositoryService.createDeployment()
            .addClasspathResource("leave.bpmn")
            .deploy();
        return "流程部署成功";
    }

    @PostMapping("/start")
    public String startProcess() {
        runtimeService.startProcessInstanceByKey("leave");
        return "流程启动成功";
    }
}

说明leave.bpmn 是流程定义文件,需放置在 resources 目录下。

7. 管理任务

使用 TaskService 查询和完成任务:

import org.flowable.engine.TaskService;
import org.flowable.task.api.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.List;

@RestController
public class TaskController {

    @Autowired
    private TaskService taskService;

    @GetMapping("/tasks")
    public List<Task> getTasks() {
        return taskService.createTaskQuery().list();
    }

    @PostMapping("/complete/{taskId}")
    public String completeTask(@PathVariable String taskId) {
        taskService.complete(taskId);
        return "任务完成";
    }
}
8. 测试和部署
  • 测试:使用 Postman 测试 REST API,确保流程部署、启动和任务管理正常。
  • 部署:生产环境中,配置安全的数据库连接和账号密码,关闭自动 schema 更新。

常见问题与注意事项

  • 版本兼容性:Flowable 6.4.1 是推荐版本,6.4.2 存在问题。
  • 数据库支持:MySQL 5.6.4 及以上版本支持 TIMESTAMP(6) 类型,需确保兼容。
  • 安全性:生产环境中修改 Flowable Modeler 的默认账号密码(admin/test)。
  • 性能优化:开发阶段关闭异步执行器,生产环境根据需求开启。

参考资源

通过以上步骤和资源,您可以快速搭建一个基于 Spring Boot 和 Flowable 的工作流系统,适用于简单的业务流程管理需求。

类似文章

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注