当前位置: 首页 > article >正文

Springboot 集成 Flowable 6.8.0

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.propertiesapplication.yml 中配置 MySQL 数据库连接信息。以下是 application.yml 的示例:

server:
  port: 8080
spring:
  datasource:
    driverClassName: com.mysql.cj.jdbc.Driver
    url: jdbc:mysql://localhost:3306/flowable?characterEncoding=UTF-8&useUnicode=true&useSSL=false&tinyInt1isBit=false&allowPublicKeyRetrieval=true&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
    username: root
    password: xxxx

4. 完整配置 Flowable

application.yml 中添加完整的 Flowable 相关配置:

flowable:
  # 数据库模式更新策略,可选值:false, true, create-drop, drop-create,生产环境建议false
  database-schema-update: true
  activity-font-name: 宋体
  label-font-name: 宋体
  annotation-font-name: 宋体
  process:
    # 流程定义缓存中保存流程定义的最大数量。默认值为-1(缓存所有流程定义)。
    definition-cache-limit: -1
  # 禁用异步执行器,开发和测试阶段可这样配置
  async-executor-activate: false
  # 历史数据级别,可选值:none, activity, audit, full
  history-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 {

    @Autowired
    private 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_procdefact_re_deployment表中查看流程定义的相关信息。
在这里插入图片描述
在这里插入图片描述

访问 http://localhost:8080/startLeaveRequest 来启动请假流程实例。可以在act_ru_task表中查看正在运行的流程实例

在这里插入图片描述


http://www.kler.cn/a/612603.html

相关文章:

  • Linux 上使用 Docker 部署 Kafka 集群
  • Brainstorm绘制功能连接图(matlab)
  • 浅聊web前端性能测试
  • [python]基于yolov10实现热力图可视化支持图像视频和摄像头检测
  • Go 语言常见错误——控制结构
  • Selenium Web自动化如何快速又准确的定位元素路径,强调一遍是元素路径
  • VMware Ubuntu 网络配置全攻略:从断网到畅通无阻
  • (UI自动化测试web端)第二篇:元素定位的方法_css定位之css选择器
  • 什么时候用到 JVM 调优,调优哪些参数
  • Android 计算已安装应用的大小
  • 闲聊IT - 面向服务架构(SOA)的发展历史
  • 北理工计算机考研复试上机2017年真题
  • HTML5 新的 Input 类型学习笔记
  • 新一代ITSM:燕千云重构企业智慧服务生态体系
  • 手写数据库MYDB(一):项目启动效果展示和环境配置问题说明
  • 【测试工具】如何使用 burp pro 自定义一个拦截器插件
  • 工具介绍《WireShark》
  • Ubuntu22.04美化MacOS主题
  • 前端技术(28) : 拖拽、粘贴和点击浏览文件上传
  • 3大支柱+8种方法:拆解分布式系统性能优化的底层逻辑