Spring Boot + flowable 快速实现工作流,好用到爆,Activiti 可以扔了。。

上一篇:张一鸣:我遇到的优秀年轻人的5个特质

背景

使用flowable自带的flowable-ui制作流程图 使用springboot开发流程使用的接口完成流程的业务功能

flowable-ui部署运行

flowable-6.6.0 运行 官方demo

参考文档:

https://flowable.com/open-source/docs/bpmn/ch14-Applications/

1、从官网下载flowable-6.6.0 : https://github.com/flowable/flowable-engine/releases/download/flowable-6.6.0/flowable-6.6.0.zip

2、将压缩包中的 flowable-6.6.0\wars\flowable-ui.war 丢到Tomcat中跑起来

3、打开http://localhost:8080/flowable-ui 用账户:admin/test 登录

1c9d1ad44cdf9ee4ee6b473d04a7ee5f.png

4、进入APP.MODELER创建流程,之后可以导出流程到项目中使用,或者配置

apache-tomcat-9.0.37\webapps\flowable-ui\WEB-INF\classes\flowable-default.properties连接本地数据库

4bc8cb7d6eb262d102c5e69ff4f9ce75.png

注意:需要将java驱动jar(mysql-connector-java-5.1.45.jar)复制到 apache-tomcat-9.0.37\webapps\flowable-rest\WEB-INF\lib这样创建的流程后端程序就能直接使用

绘制流程图

048d005302302e4eeb89de4989e68b46.png

根据业务需要在 flowable-ui>APP.MODELER里面绘制流程图,示例如上图。先解释一些概念。

事件(event)通常用于为流程生命周期中发生的事情建模,图里是【开始、结束】两个圈。

顺序流(sequence flow)是流程中两个元素间的连接器。图里是【箭头线段】。

网关(gateway)用于控制执行的流向。图里是【菱形(中间有X)】

用户任务(user task)用于对需要人工执行的任务进行建模。图里是【矩形】。

简单的工作流大概就这些元素(还有很多这里就不扩展了)。下面描述一下工作流是如何流动的。另外,Spring 系列面试题和答案全部整理好了,微信搜索互联网架构师,在后台发送:面试,可以在线阅读。

首先启动了工作流后,由【开始】节点自动流向【学生】节点,等待该任务执行。任务被分配的学生用户执行后流向 【老师】节点,再次等待该任务执行。被分配的老师用户执行后流向 【网关】,网关以此检查每个出口,流向符合条件的任务,比如这里老师执行任务时是同意,就流向【校长】节点,等待该任务执行。执行后跟老师类似,同意后就流向【结束】节点,整个流程到此结束。

绘图细节:

1、保留流程模型

5331ac81e20372a35b0a848003202bb1.png

2、顺序流可以设置流条件来限制流动,比如上面的网关出口就设置了条件

0b34791e5d8bafbc2e62a8e0691c16f3.png

3、任务需要分配任务的执行用户,可以分配到候选组,也可以直接分配到候选人

f7ca536b2725abb1034f0c8ea782d37b.png

最后导出工作流文件

d4d9c8472a46d29ef8d0a529f920b7cf.png

文件内容

<?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-insmtece" 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">
  <process id="leave_approval" name="请假审批" isExecutable="true">
    <startEvent id="start" name="开始" flowable:initiator="startuser" flowable:formFieldValidation="true"></startEvent>
    <userTask id="stu_task" name="学生" flowable:candidateGroups="stu_group" flowable:formFieldValidation="true"></userTask>
    <sequenceFlow id="flow1" sourceRef="start" targetRef="stu_task"></sequenceFlow>
    <userTask id="te_task" name="老师" flowable:candidateGroups="te_group" flowable:formFieldValidation="true"></userTask>
    <exclusiveGateway id="getway1" name="网关1"></exclusiveGateway>
    <userTask id="mte_task" name="校长" flowable:candidateGroups="mte_group" flowable:formFieldValidation="true"></userTask>
    <exclusiveGateway id="getway2" name="网关2"></exclusiveGateway>
    <endEvent id="end" name="结束"></endEvent>
    <sequenceFlow id="flow1" name="请假" sourceRef="stu_task" targetRef="te_task" skipExpression="${command=='agree'}"></sequenceFlow>
    <sequenceFlow id="flow3_1" name="同意" sourceRef="getway1" targetRef="mte_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow2" name="审批" sourceRef="te_task" targetRef="getway1"></sequenceFlow>
    <sequenceFlow id="flow3_2" name="拒绝" sourceRef="getway1" targetRef="stu_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4" name="审批" sourceRef="mte_task" targetRef="getway2"></sequenceFlow>
    <sequenceFlow id="flow4_1" name="同意" sourceRef="getway2" targetRef="end" skipExpression="${command=='free'}">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='agree'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="flow4_2" name="拒绝" sourceRef="getway2" targetRef="stu_task">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${command=='refuse'}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_leave_approval">
    这里先省略
  </bpmndi:BPMNDiagram>
</definitions>

4、bpmn文件导入

如果需要,可以把这个流程文件下载下来,直接导入使用

87b18bcc43cffacbc23d1ec7f5b3072f.png

后台项目搭建

后台项目基于jdk8,使用springboot框架

spring 版本

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.3.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

项目依赖pom.xml

<dependency>
    <groupId>org.flowable</groupId>
    <artifactId>flowable-spring-boot-starter</artifactId>
    <version>6.6.0</version>
</dependency>
<dependency>
    <groupId>mysql</groupId>
    <artifactId>mysql-connector-java</artifactId>
    <version>5.1.45</version>
</dependency>

项目配置application.yml

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/flowable?useSSL=false&characterEncoding=UTF-8&serverTimezone=GMT%2B8
    driver-class-name: com.mysql.jdbc.Driver
    username: root
    password: 123456


数据库

1、Flowable的所有数据库表都以ACT_开头。第二部分是说明表用途的两字符标示符。服务API的命名也大略符合这个规则。

2、ACT_RE_: 'RE’代表repository。带有这个前缀的表包含“静态”信息,例如流程定义与流程资源(图片、规则等)。

3、ACT_RU_: 'RU’代表runtime。这些表存储运行时信息,例如流程实例(process instance)、用户任务(user task)、变量(variable)、作业(job)等。Flowable只在流程实例运行中保存运行时数据,并在流程实例结束时删除记录。这样保证运行时表小和快。

4、ACT_HI_: 'HI’代表history。这些表存储历史数据,例如已完成的流程实例、变量、任务等。

5、ACT_GE_: 通用数据。在多处使用。

1)通用数据表(2个)

2)历史表(8个,HistoryService接口操作的表)

3)用户相关表(4个,IdentityService接口操作的表)

4)流程定义、流程模板相关表(3个,RepositoryService接口操作的表)

5)流程运行时表(6个,RuntimeService接口操作的表)


流程引擎API与服务

引擎API是与Flowable交互的最常用手段。总入口点是ProcessEngine。

055f0775cf084a42c1af0bd8fd34368d.png

1、RepositoryService很可能是使用Flowable引擎要用的第一个服务。这个服务提供了管理与控制部署(deployments)与流程定义(process definitions)的操作。管理静态信息,

2、RuntimeService用于启动流程定义的新流程实例。

3、IdentityService很简单。它用于管理(创建,更新,删除,查询……)组与用户。

4、FormService是可选服务。也就是说Flowable没有它也能很好地运行,而不必牺牲任何功能。

5、HistoryService暴露Flowable引擎收集的所有历史数据。要提供查询历史数据的能力。

6、ManagementService通常在用Flowable编写用户应用时不需要使用。它可以读取数据库表与表原始数据的信息,也提供了对作业(job)的查询与管理操作。

7、DynamicBpmnService可用于修改流程定义中的部分内容,而不需要重新部署它。例如可以修改流程定义中一个用户任务的办理人设置,或者修改一个服务任务中的类名。

接下来使用之前的请假流程图,上代码

import lombok.extern.slf4j.Slf4j;
import org.flowable.engine.HistoryService;
import org.flowable.engine.RepositoryService;
import org.flowable.engine.RuntimeService;
import org.flowable.engine.history.HistoricProcessInstance;
import org.flowable.engine.repository.Deployment;
import org.flowable.engine.repository.ProcessDefinition;
import org.flowable.engine.runtime.Execution;
import org.flowable.engine.runtime.ProcessInstance;
import org.flowable.idm.api.Group;
import org.flowable.idm.api.User;
import org.flowable.task.api.Task;
import org.flowable.task.api.history.HistoricTaskInstance;
import org.springframework.beans.factory.annotation.Autowired;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.zip.ZipInputStream;

/**
 * TestFlowable
 *
 * @Author
 * @Date: 2021/10/17 23:35
 * @Version 1.0
 */
@Slf4j
public class TestFlowable {

    @Autowired
    private RepositoryService repositoryService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @Autowired
    private org.flowable.engine.TaskService taskService;

    @Autowired
    private org.flowable.engine.IdentityService identityService;

    public void createDeploymentZip() {
        /*
         * @Date: 2021/10/17 23:38
         * Step 1: 部署xml(压缩到zip形式,直接xml需要配置相对路径,麻烦,暂不用)
         */
        try {
            File zipTemp = new File("f:/leave_approval.bpmn20.zip");
            ZipInputStream zipInputStream = new ZipInputStream(new FileInputStream(zipTemp));
            Deployment deployment = repositoryService
                    .createDeployment()
                    .addZipInputStream(zipInputStream)
                    .deploy();
            log.info("部署成功:{}", deployment.getId());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
        /*
         * @Date: 2021/10/17 23:40
         * Step 2: 查询部署的流程定义
         */
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").list();
        List<ProcessDefinition> pages = repositoryService.createProcessDefinitionQuery().processDefinitionKey("leave_approval").listPage(1, 30);

        /*
         * @Date: 2021/10/17 23:40
         * Step 3: 启动流程,创建实例
         */
        String processDefinitionKey = "leave_approval";//流程定义的key,对应请假的流程图
        String businessKey = "schoolleave";//业务代码,根据自己的业务用
        Map<String, Object> variablesDefinition = new HashMap<>();//流程变量,可以自定义扩充
        ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey, businessKey, variablesDefinition);
        log.info("启动成功:{}", processInstance.getId());
        /*
         * @Date: 2021/10/17 23:40
         * Step 4: 查询指定流程所有启动的实例列表
         * 列表,或 分页 删除
         */
        List<Execution> executions = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").list();
        List<Execution> executionPages = runtimeService.createExecutionQuery().processDefinitionKey("leave_approval").listPage(1, 30);
//        runtimeService.deleteProcessInstance(processInstanceId, deleteReason); //删除实例

        /*
         * @Date: 2021/10/17 23:40
         * Step 5: 学生查询可以操作的任务,并完成任务
         */
        String candidateGroup = "stu_group"; //候选组 xml文件里面的 flowable:candidateGroups="stu_group"
        List<Task> taskList = taskService.createTaskQuery().taskCandidateGroup(candidateGroup).orderByTaskCreateTime().desc().list();
        for (Task task : taskList) {
            // 申领任务
            taskService.claim(task.getId(), "my");
            // 完成
            taskService.complete(task.getId());
        }

        /*
         * @Date: 2021/10/17 23:40
         * Step 6: 老师查询可以操作的任务,并完成任务
         */
        String candidateGroupTe = "te_group"; //候选组 xml文件里面的 flowable:candidateGroups="te_group"
        List<Task> taskListTe = taskService.createTaskQuery().taskCandidateGroup(candidateGroupTe).orderByTaskCreateTime().desc().list();
        for (Task task : taskListTe) {
            // 申领任务
            taskService.claim(task.getId(), "myte");
            // 完成
            Map<String, Object> variables = new HashMap<>();
            variables.put("command","agree"); //携带变量,用于网关流程的条件判定,这里的条件是同意
            taskService.complete(task.getId(), variables);
        }
        /*
         * @Date: 2021/10/18 0:17
         * Step 7: 历史查询,因为一旦流程执行完毕,活动的数据都会被清空,上面查询的接口都查不到数据,但是提供历史查询接口
         */
        // 历史流程实例
        List<HistoricProcessInstance> historicProcessList = historyService.createHistoricProcessInstanceQuery().processDefinitionKey("leave_approval").list();
        // 历史任务
        List<HistoricTaskInstance> historicTaskList = historyService.createHistoricTaskInstanceQuery().processDefinitionKey("leave_approval").list();
        // 实例历史变量 , 任务历史变量
        // historyService.createHistoricVariableInstanceQuery().processInstanceId(processInstanceId);
        // historyService.createHistoricVariableInstanceQuery().taskId(taskId);
        // *****************************************************分隔符********************************************************************
        // *****************************************************分隔符********************************************************************
        // 可能还需要的API
        // 移动任务,人为跳转任务
        // runtimeService.createChangeActivityStateBuilder().processInstanceId(processInstanceId)
        //       .moveActivityIdTo(currentActivityTaskId, newActivityTaskId).changeState();

        // 如果在数据库配置了分组和用户,还会用到
        List<User> users = identityService.createUserQuery().list();    //用户查询,用户id对应xml 里面配置的用户
        List<Group> groups = identityService.createGroupQuery().list(); //分组查询,分组id对应xml 里面配置的分组 如 stu_group,te_group 在表里是id的值

        // 另外,每个查询后面都可以拼条件,内置恁多查询,包括模糊查询,大小比较都有
    }
}

参考资料

【1】分享牛Flowable文档汉化:https://github.com/qiudaoke/flowable-userguide

【2】猫七姑娘 flowable-6.6.0 运行官方 demo

【3】华格瑞沙 https://www.cnblogs.com/yangjiming/p/10938515.html

本文链接:https://blog.csdn.net/zhan107876/article/details/120815560

END

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值