1. 创建 Spring Boot 项目
通过 Spring Initializr(https://start.spring.io/ )创建一个基础的 Spring Boot 项目,添加以下依赖:
- Spring Web
- Spring Data JPA
- MySQL Driver
- Lombok(可选,用于简化代码)
2. 添加 Flowable 依赖
在 pom.xml
中添加 Flowable 相关依赖:
<dependencies><!-- Spring Boot Starter for Flowable --><dependency><groupId>org.flowable</groupId><!--引入flowable基础功能 自动创建46张表--><artifactId>flowable-spring-boot-starter-basic</artifactId><!--引入flowable所有功能 自动创建79张表-->
<!-- <artifactId>flowable-spring-boot-starter</artifactId>--><!-- 根据需要选择版本 --><version>6.8.0</version></dependency><!-- MySQL Driver --><dependency><groupId>mysql</groupId><artifactId>mysql-connector-java</artifactId><version>8.0.27</version></dependency><!-- Spring Web --><dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-web</artifactId></dependency><!-- Lombok --><dependency><groupId>org.projectlombok</groupId><artifactId>lombok</artifactId><optional>true</optional></dependency>
</dependencies>
3. 配置 MySQL 数据库连接
在 application.properties
或 application.yml
中配置 MySQL 数据库连接信息。以下是 application.yml
的示例:
server:port: 8080
spring:datasource:driverClassName: com.mysql.cj.jdbc.Driverurl: jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=trueusername: rootpassword: xxxx
4. 完整配置 Flowable
在 application.yml
中添加完整的 Flowable 相关配置:
flowable:# 数据库模式更新策略,可选值:false, true, create-drop, drop-create,生产环境建议falsedatabase-schema-update: trueactivity-font-name: 宋体label-font-name: 宋体annotation-font-name: 宋体process:# 流程定义缓存中保存流程定义的最大数量。默认值为-1(缓存所有流程定义)。definition-cache-limit: -1# 禁用异步执行器,开发和测试阶段可这样配置async-executor-activate: false# 历史数据级别,可选值:none, activity, audit, fullhistory-level: full# 是否自动检查并部署流程文件,设置为false需要手动部署流程文件check-process-definitions: true
flowable.history-level
配置项用于指定 Flowable 工作流引擎的历史数据记录级别。不同的历史数据级别决定了 Flowable 在流程执行过程中记录哪些历史信息,这对于流程监控、审计和分析等操作非常重要。
-
none
当flowable.history-level=none
时,Flowable 工作流引擎不会记录任何历史数据。也就是说,在流程执行过程中,不会保存流程实例、任务、活动等相关的历史信息。这种配置适用于对历史数据没有需求,只关注流程的实时执行,并且希望减少数据库存储压力和提高性能的场景。例如,一些临时性的、简单的流程,不需要对执行过程进行追溯和分析。 -
activity
若设置flowable.history-level=activity
,Flowable 会记录流程活动的基本历史信息。具体来说,会记录每个流程实例中活动(如任务、网关等)的开始和结束时间,以及活动的状态信息。但不会记录流程变量、任务的详细信息(如任务的分配、完成时间等)。这种配置适用于只需要了解流程活动的大致执行情况,而不需要详细的任务和变量信息的场景。例如,用于监控流程的整体执行进度,查看哪些活动已经完成,哪些还在进行中。 -
audit
当配置为flowable.history-level=audit
时,Flowable 会记录更详细的历史信息,用于审计目的。除了记录活动的开始和结束时间外,还会记录任务的分配信息、任务的完成时间、流程变量的更新情况等。这些信息可以帮助管理员或审计人员了解流程的执行过程,追踪任务的处理情况和变量的变化。例如,在一个请假流程中,可以查看每个审批任务是由谁处理的,处理时间是什么时候,以及请假天数等变量在流程执行过程中是否有修改。 -
full
设置flowable.history-level=full
会记录最完整的历史数据。除了包含audit
级别的信息外,还会记录更多的细节,如活动的所有事件(如活动的创建、取消等)、任务的注释、流程实例的启动和结束原因等。这种配置适用于需要对流程进行全面追溯和分析的场景,例如进行流程优化、合规性检查等。通过完整的历史数据,可以深入了解流程的执行细节,发现潜在的问题和瓶颈。。
5. 创建 Flowable 流程定义文件
在 src/main/resources/processes
目录下创建 BPMN 流程定义文件,例如 leave-request.bpmn20.xml
。以下是一个简单的请假流程示例:
<?xml version="1.0" encoding="UTF-8"?>
<definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:bpmndi="http://www.omg.org/spec/BPMN/20100524/DI"xmlns:omgdc="http://www.omg.org/spec/DD/20100524/DC"xmlns:omgdi="http://www.omg.org/spec/DD/20100524/DI"xmlns:flowable="http://flowable.org/bpmn"id="Definitions_1"targetNamespace="http://www.flowable.org/processdef"><process id="leaveRequestProcess" name="Leave Request Process" isExecutable="true"><startEvent id="startEvent1"></startEvent><userTask id="approveTask" name="Approve Leave Request" flowable:assignee="manager"></userTask><endEvent id="endEvent1"></endEvent><sequenceFlow id="flow1" sourceRef="startEvent1" targetRef="approveTask"></sequenceFlow><sequenceFlow id="flow2" sourceRef="approveTask" targetRef="endEvent1"></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_1"><bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_1"><bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1"><omgdc:Bounds height="36.0" width="36.0" x="173.0" y="102.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="approveTask" id="BPMNShape_approveTask"><omgdc:Bounds height="80.0" width="100.0" x="325.0" y="78.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNShape bpmnElement="endEvent1" id="BPMNShape_endEvent1"><omgdc:Bounds height="36.0" width="36.0" x="501.0" y="102.0"></omgdc:Bounds></bpmndi:BPMNShape><bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"><omgdi:waypoint x="209.0" y="120.0"></omgdi:waypoint><omgdi:waypoint x="325.0" y="118.0"></omgdi:waypoint></bpmndi:BPMNEdge><bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"><omgdi:waypoint x="425.0" y="118.0"></omgdi:waypoint><omgdi:waypoint x="501.0" y="120.0"></omgdi:waypoint></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>
6. 创建服务类启动流程实例
创建一个服务类来启动流程实例:
import org.flowable.engine.RuntimeService;
import org.springframework.stereotype.Service;import java.util.HashMap;
import java.util.Map;@Service
public class LeaveRequestService {private final RuntimeService runtimeService;public LeaveRequestService(RuntimeService runtimeService) {this.runtimeService = runtimeService;}public String startLeaveRequestProcess() {Map<String, Object> variables = new HashMap<>();variables.put("employee", "John Doe");variables.put("leaveDays", 5);return runtimeService.startProcessInstanceByKey("leaveRequestProcess", variables).getId();}
}
7. 创建控制器测试流程启动
创建一个控制器来测试流程启动:
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;@RestController
public class LeaveRequestController {@Autowiredprivate LeaveRequestService leaveRequestService;@GetMapping("/start-leave-request")public String startLeaveRequest() {return "Process instance started with ID: " + leaveRequestService.startLeaveRequestProcess();}
}
8. 启动项目
启动 Spring Boot 项目后, leave-request.bpmn20.xml
文件会自动部署,可以在act_re_procdef
,act_re_deployment
表中查看流程定义的相关信息。
访问 http://localhost:8080/startLeaveRequest
来启动请假流程实例。可以在act_ru_task
表中查看正在运行的流程实例