java

关注公众号 jb51net

关闭
首页 > 软件编程 > java > springboot集成flowable

springboot集成flowable实践

作者:jpq+

本文介绍了Flowable业务流程管理引擎在Spring Boot 2.3版本中的集成,Flowable支持BPMN2.0标准,提供了流程定义、实例管理和数据访问等功能,文章详细介绍了Flowable生成的数据库表及其用途,并通过一个请假审批流程的示例,展示了如何部署和测试Flowable

随着企业对于业务流程管理需求的增加,流程引擎在企业信息化建设中的作用越来越重要。Flowable是一个开源的轻量级业务流程管理(BPM)和工作流引擎,它支持BPMN 2.0标准。

Flowable的一些特点:

集成flowable

pom.xml中引入以下内容:

  <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter</artifactId>
            <version>${flowable.version}</version>
        </dependency>
        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-ui-modeler</artifactId>
            <version>${flowable.version}</version>
        </dependency>

        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-ui-admin</artifactId>
            <version>${flowable.version}</version>
        </dependency>

        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-ui-idm</artifactId>
            <version>${flowable.version}</version>
        </dependency>

        <dependency>
            <groupId>org.flowable</groupId>
            <artifactId>flowable-spring-boot-starter-ui-task</artifactId>
            <version>${flowable.version}</version>
        </dependency>

配置文件追加配置自动建表:

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/tt?useSSL=false&serverTimezone=Asia/Shanghai&nullCatalogMeansCurrent=true
    username: root
    password: pass
    driverClassName: com.mysql.cj.jdbc.Driver
flowable:
  async-executor-activate: false
  database-schema-update: true

默认会启动liquibase, 可关闭:

spring:
  liquibase:
    enabled: false

生成的表如下:

生成的表分别做什么用的

如果用过Activity的同学, 就会发现表名很多相同的, 但Flowable的表多了很多, 毕竟两者早期是同一个产品。表的功能一般可以通过第二个词语缩写来进行区分。

Flowable 生成的数据表主要用于支撑工作流引擎的运行和管理。这些表中包括但不限于以下几类:

简单使用flowable

简单绘制一个请假审批:

下载该文件命名为a.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:xsd="http://www.w3.org/2001/XMLSchema" xmlns:flowable="http://flowable.org/bpmn" 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" typeLanguage="http://www.w3.org/2001/XMLSchema" expressionLanguage="http://www.w3.org/1999/XPath" targetNamespace="http://www.flowable.org/processdef" exporter="Flowable Open Source Modeler" exporterVersion="6.8.1">
  <process id="a" name="a" isExecutable="true">
    <startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
    <userTask id="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7" name="组长" flowable:formFieldValidation="true"></userTask>
    <exclusiveGateway id="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B"></exclusiveGateway>
    <sequenceFlow id="sid-EA5097EB-7C3A-4295-86F5-8EFFC6F3E02A" sourceRef="startEvent1" targetRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B"></sequenceFlow>
    <userTask id="sid-96F84EB8-DB79-4092-89A5-141313627020" name="部门经理" flowable:formFieldValidation="true"></userTask>
    <userTask id="sid-09FB7A15-7582-4785-B635-05E745D56438" name="总经理" flowable:formFieldValidation="true"></userTask>
    <endEvent id="sid-CF77490C-2721-4269-9A2C-301CD4DD6F15"></endEvent>
    <sequenceFlow id="sid-38EF250C-CF01-414A-BDC3-A0E2CBAB9175" sourceRef="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7" targetRef="sid-CF77490C-2721-4269-9A2C-301CD4DD6F15"></sequenceFlow>
    <endEvent id="sid-F9DE2B7F-F2B4-4D7A-872C-D620EC006C80"></endEvent>
    <sequenceFlow id="sid-0537C179-756A-4F54-AF48-BCD3669EC88B" sourceRef="sid-96F84EB8-DB79-4092-89A5-141313627020" targetRef="sid-F9DE2B7F-F2B4-4D7A-872C-D620EC006C80"></sequenceFlow>
    <endEvent id="sid-6224E896-E5DF-4AD8-9C5C-0BB6BBB8125C"></endEvent>
    <sequenceFlow id="sid-B4C4F1DA-257B-4206-950A-133010C19C4A" sourceRef="sid-09FB7A15-7582-4785-B635-05E745D56438" targetRef="sid-6224E896-E5DF-4AD8-9C5C-0BB6BBB8125C"></sequenceFlow>
    <sequenceFlow id="sid-669C693E-E02E-47A1-B8EF-0F6DC4AEF61C" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-96F84EB8-DB79-4092-89A5-141313627020">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day>1 && day<=3}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-2498854B-52F3-4FCE-9994-B5ED2DC350E4" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-1728059A-DB68-4E2F-A07F-4AE72EC7F5D7">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day<=1}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-9D85E323-099E-4F30-861E-0DED4491C1B8" sourceRef="sid-431B47DD-3932-49D2-8B5A-51FBE9C8A50B" targetRef="sid-09FB7A15-7582-4785-B635-05E745D56438">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${day}>3]]></conditionExpression>
    </sequenceFlow>
  </process>
 

</definitions>

部署这个a.bpmn20.xml文件:

 @Test
    void test01() {
        // 配置数据库相关信息 获取 ProcessEngineConfiguration
        ProcessEngineConfiguration cfg = new StandaloneProcessEngineConfiguration()
                .setJdbcUrl("jdbc:mysql://localhost:3306/tt?serverTimezone=UTC&nullCatalogMeansCurrent=true")
                .setJdbcUsername("root")
                .setJdbcDriver("com.mysql.cj.jdbc.Driver")
                .setDatabaseSchemaUpdate(ProcessEngineConfiguration.DB_SCHEMA_UPDATE_TRUE);
        // 获取流程引擎对象
        ProcessEngine processEngine = cfg.buildProcessEngine();

        // 部署流程 获取RepositoryService对象
        RepositoryService repositoryService = processEngine.getRepositoryService();
        Deployment deployment = repositoryService.createDeployment()// 创建Deployment对象
                .addClasspathResource("a.bpmn20.xml") // 添加流程部署文件
                .name("请假") // 设置部署流程的名称
                .deploy(); // 执行部署操作
                }

测试执行:

 Map<String, Object> variables = new HashMap<>();
        //days是流程图条件
        
        variables.put("day", 3);
        ProcessInstance pi = runtimeService.startProcessInstanceByKey("a", variables);
        System.out.println(pi.getName());

我们分别调整day为1,3,5时,此时ACT_RU_TASK表(ACT_RU_TASK表存储了运行时的用户任务数据)中将有三条数据:

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持脚本之家。

您可能感兴趣的文章:
阅读全文