• 小白学流程引擎-FLowable(二) — 从零搭建自己的FLowable服务 — 搭建流程服务-FLowable的新手指南


    一、介绍

    纵览Gitee搜索Flowable开源项目,大多都是已开发好的项目,而笔者从零开始搭建属于自己的Flowable引擎,并且是可以拿到生产上使用的。这里搭建一个Springboot + Flowable6.7.2的开箱即用的流程引擎开源软件,代码仓库上传到Gitee,想要的文章末尾拿链接。
    当前Flowable最新版本是6.7.2,学习肯定用最新稳定版本啦。

    二、软件架构

    Springboot + Flowable + modeler + idm + Mysql
    SrpingBoot version:2.7.5
    Flowable version:6.7.2
    Mysql version:8.0.26
    JDK:8

    三、SpringBoot整合Flowable

    3.1 使用spring initializer快速创建spring Boot项目

    步骤:
    1,打开idea,创建工程:file -> newproject ,选择spring initializer;
    2,然后填写项目命名后,选择JDK8,Maven;
    3,选择功能模块,选Web和lombok即可;
    4,点Finish,就能够自动创建工程,并能导入相应依赖了;
    5,Maven加载依赖后,运行主程序类看是否成功。

    3.2 pom.xml文件引入依赖配置

    引入Flowable依赖,这里选择Springboot-starter,毕竟是springboot项目。
    添加必要的依赖。

    
        
            org.springframework.boot
            spring-boot-starter-web
        
    
        
            org.projectlombok
            lombok
            true
        
        
            org.springframework.boot
            spring-boot-starter-test
            test
        
    
       
                org.mybatis.spring.boot
                mybatis-spring-boot-starter
                2.2.2
        
    
        
            com.baomidou
            mybatis-plus-boot-starter
            3.0.3
        
    
        
            com.alibaba
            fastjson
            1.2.79
        
    
        
            mysql
            mysql-connector-java
            runtime
        
    
        
        
            org.flowable
            flowable-spring-boot-starter
            6.7.2
        
    
    
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50

    3.3 application.yml配置

    在application.yml文件中加入以下配置。

    server:
      port: 8080
    
    spring:
      datasource:
        driver-class-name: com.mysql.cj.jdbc.Driver
        url: jdbc:mysql://localhost:3306/spider-flowable?charset=utf8mb4&useSSL=false&serverTimezone=Asia/Shanghai&characterEncoding=utf-8
        username: root
        password: 12345678
    
    mybatis-plus:
      configuration:
        log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
        map-underscore-to-camel-case: true
    
    flowable:
      #关闭定时任务JOB
      async-executor-activate: false
      #将databaseSchemaUpdate设置为true。当Flowable发现库与数据库表结构不一致时,会自动将数据库表结构升级至新版本。
      database-schema-update: true
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21

    3.4 运行项目

    项目启动的时候检查如果数据库对应的表结构没有创建,会帮助我们先创建对应的表结构。到此Springboot整合Flowable就搞定了,
    项目启动后,刷新数据库,你会发现多了79张表,都是以ACT和FLW为前缀的表名。
    知道为什么Flowable自带的数据库的表名为啥是以ACT为前缀吗?
    因为Flowable是基于Activiti6衍生出来的版本。

    数据库结构如图:
    在这里插入图片描述

    四、定义流程文件

    FLowable流程的运行,其实是用一个满足BPMN格式的XML文件来执行的,至于XML内容格式是怎么样的后续在学习。

    在项目中的resources下新建一个processes文件夹,processes目录下的任何BPMN 2.0流程定义都会被自动部署。

    1,在processes文件夹下新建holiday-request.bpmn20.xml文件
    2,文件内容复制如下:

    
    
    
        
            
            
            
            
            
            
            
            
            
            
            
            
                
                    
                    
                
            
            
            
                
                    
                    
                
            
            
            
            
            
            
            
            
            
            
            
            
        
    
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56

    五、运行项目并测试

    咱来测试一下功能,小试牛刀,更多功能在后面文章再细谈。
    在test文件夹下新建测试类SpiderFlowableTest,代码如下。

    package com.example.spiderFlowable;
    
    import cn.hutool.json.JSONUtil;
    import com.alibaba.fastjson.JSON;
    import org.flowable.engine.HistoryService;
    import org.flowable.engine.RepositoryService;
    import org.flowable.engine.RuntimeService;
    import org.flowable.engine.TaskService;
    import org.flowable.engine.history.HistoricActivityInstance;
    import org.flowable.engine.repository.Deployment;
    import org.flowable.engine.repository.ProcessDefinition;
    import org.flowable.engine.runtime.ProcessInstance;
    import org.flowable.task.api.Task;
    import org.junit.jupiter.api.Test;
    import org.springframework.beans.factory.annotation.Autowired;
    import org.springframework.boot.test.context.SpringBootTest;
    
    import java.util.HashMap;
    import java.util.List;
    import java.util.Map;
    
    /**
     * @author xiong.bo
     * @version 1.0
     * @date 2022/11/27 10:41 上午
     */
    
    @SpringBootTest
    public class SpiderFlowableTest {
    
        @Autowired
        private RepositoryService repositoryService;
        @Autowired
        private RuntimeService runtimeService;
        @Autowired
        private TaskService taskService;
        @Autowired
        private HistoryService historyService;
    
        /**
         * 模拟-部署流程
         * 说明:
         * 流程定义有版本的概念,bpmn文件有改动,需要部署后才生效
         */
        @Test
        void createDeployment() {
            Deployment deployment = repositoryService.createDeployment()
                    .addClasspathResource("processes/holiday-request.bpmn20.xml")
                    .deploy();
            System.out.println(deployment.getId());
            System.out.println(JSON.toJSONString(deployment));
        }
    
        /**
         * 获取流程定义
         */
        @Test
        void getProcessDefinition() {
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                    .deploymentId("17504")
                    .singleResult();
            System.out.println("definitionId:" + processDefinition.getId());
            System.out.println("definition:" + JSONUtil.toJsonStr(processDefinition));
        }
    
        /**
         * 启动流程
         * 模拟用户发起一个请假流程
         */
        @Test
        void startProcessDefinition() {
            Map variables = new HashMap<>();
            variables.put("employee", "李四");
            variables.put("nrOfHolidays", "4");
            variables.put("description", "外出请假");
            //启动流程实例有多个方法,这里调用流程key的方法来启动
            ProcessInstance holidayProcessInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
            System.out.println("processInstanceId:" + holidayProcessInstance.getProcessInstanceId());
        }
    
        /**
         * 获取任务
         * 用户发起流程后,相关的人员能够查询该任务
         *
         */
        @Test
        void getTask(){
            List tasks = taskService.createTaskQuery()
                    .active()
                    .includeProcessVariables()
                    //值为admin是管理员可以查看所有的,测试
                    .taskCandidateOrAssigned("admin")
                    .list();
            System.out.println("You have " + tasks.size() + " tasks:");
            for (int i = 0; i < tasks.size(); i++) {
                Task task = tasks.get(i);
                System.out.println((i + 1) + ") " + "taskId: " + task.getId() + ", taskName: " + task.getName());
            }
        }
    
        /**
         * 审批任务
         * 说明:
         * 1,变量approved同流程定义文件里面顺序流定义的变量
         * 2,taskId是上一个获取用户任务的taskId值,也就是要指定哪一个用户任务往下执行
         */
        @Test
        void completeTask(){
            Map variables = new HashMap<>();
            variables.put("approved", true);
            String taskId = "20008";
            taskService.complete(taskId,variables);
        }
    
        /**
         * 查看历史任务
         * 说明:
         * taskAssignee: 分配人
         * finished:已完成状态的
         *
         */
        @Test
        void historyTask(){
            List hisList = historyService.createHistoricActivityInstanceQuery()
                    .taskAssignee("admin")
                    .finished()
                    .list();
    
            hisList.stream().forEach(e -> System.out.println(JSONUtil.toJsonStr(e)));
        }
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131

    1,部署流程

    	 /**
         * 模拟-部署流程
         * 说明:
         * 流程定义有版本的概念,bpmn文件有改动,需要部署后才生效
         */
        @Test
        void createDeployment() {
            Deployment deployment = repositoryService.createDeployment()
                    .addClasspathResource("processes/holiday-request.bpmn20.xml")
                    .deploy();
            System.out.println(deployment.getId());
            System.out.println(JSON.toJSONString(deployment));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果:

    27501
    ps: System.out.println(JSON.toJSONString(deployment));输出内容太多这里不贴上了
    
    • 1
    • 2

    2,获取流程定义

    	/**
         * 获取流程定义
         */
        @Test
        void getProcessDefinition() {
            ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery()
                    .deploymentId("17504")
                    .singleResult();
            System.out.println("definitionId:" + processDefinition.getId());
            System.out.println("definition:" + JSONUtil.toJsonStr(processDefinition));
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11

    运行结果:

    definitionId:holidayRequest:5:17506
    definition:{"name":"请假流程","key":"holidayRequest","version":5,"category":"http://www.flowable.org/processdef","deploymentId":"17504","resourceName":"processes/holiday-request.bpmn20.xml","tenantId":"","isGraphicalNotationDefined":false,"hasStartFormKey":false,"suspensionState":1,"derivedVersion":0,"id":"holidayRequest:5:17506","revision":1,"isInserted":false,"isUpdated":false,"isDeleted":false,"originalPersistentState":{"suspensionState":1,"category":"http://www.flowable.org/processdef"}}
    
    
    • 1
    • 2
    • 3

    3,启动流程

    	/**
         * 启动流程
         * 模拟用户发起一个请假流程
         */
        @Test
        void startProcessDefinition() {
            Map<String, Object> variables = new HashMap<>();
            variables.put("employee", "李四");
            variables.put("nrOfHolidays", "4");
            variables.put("description", "外出请假");
            //启动流程实例有多个方法,这里调用流程key的方法来启动
            ProcessInstance holidayProcessInstance = runtimeService.startProcessInstanceByKey("holidayRequest", variables);
            System.out.println("processInstanceId:" + holidayProcessInstance.getProcessInstanceId());
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    运行结果:

    processInstanceId:30001
    
    • 1

    4,获取用户任务

    	/**
         * 获取任务
         * 用户发起流程后,相关的人员能够查询该任务
         *
         */
        @Test
        void getTask(){
            List<Task> tasks = taskService.createTaskQuery()
                    .active()
                    .includeProcessVariables()
                    //值为admin是管理员可以查看所有的,测试
                    .taskCandidateOrAssigned("admin")
                    .list();
            System.out.println("You have " + tasks.size() + " tasks:");
            for (int i = 0; i < tasks.size(); i++) {
                Task task = tasks.get(i);
                System.out.println((i + 1) + ") " + "taskId: " + task.getId() + ", taskName: " + task.getName());
            }
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19

    运行结果:

    You have 4 tasks:
    1) taskId: 10008, taskName: 同意或驳回请求
    2) taskId: 12508, taskName: 同意或驳回请求
    3) taskId: 20008, taskName: 同意或驳回请求
    4) taskId: 7508, taskName: 同意或驳回请求
    
    • 1
    • 2
    • 3
    • 4
    • 5

    5,审批任务

    	/**
         * 审批任务
         * 说明:
         * 1,变量approved同流程定义文件里面顺序流定义的变量
         * 2,taskId是上一个获取用户任务的taskId值,也就是要指定哪一个用户任务往下执行
         */
        @Test
        void completeTask(){
            Map<String, Object> variables = new HashMap<>();
            variables.put("approved", true);
            String taskId = "20008";
            taskService.complete(taskId,variables);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13

    运行结果:
    结果回调了 ApprovalSuccessDelegate ,打印出"审批通过了"

    6,查看历史任务

    	/**
         * 查看历史任务
         * 说明:
         * taskAssignee: 分配人
         * finished:已完成状态的
         *
         */
        @Test
        void historyTask(){
            List<HistoricActivityInstance> hisList = historyService.createHistoricActivityInstanceQuery()
                    .taskAssignee("admin")
                    .finished()
                    .list();
    
            hisList.stream().forEach(e -> System.out.println(JSONUtil.toJsonStr(e)));
    
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17

    运行结果:

    {"activityId":"approveTask","activityName":"同意或驳回请求","activityType":"userTask","executionId":"20005","assignee":"admin","taskId":"20008","tenantId":"","processInstanceId":"20001","processDefinitionId":"holidayRequest:5:17506","startTime":1669521326282,"endTime":1669521596820,"durationInMillis":270538,"id":"20007","revision":2,"isInserted":false,"isUpdated":false,"isDeleted":false,"originalPersistentState":{"executionId":"20005","durationInMillis":270538,"endTime":1669521596820,"assignee":"admin","taskId":"20008"}}
    {"activityId":"approveTask","activityName":"同意或驳回请求","activityType":"userTask","executionId":"30005","assignee":"admin","taskId":"30008","tenantId":"","processInstanceId":"30001","processDefinitionId":"holidayRequest:6:27503","startTime":1669643905412,"endTime":1669644033366,"durationInMillis":127954,"id":"30007","revision":2,"isInserted":false,"isUpdated":false,"isDeleted":false,"originalPersistentState":{"executionId":"30005","durationInMillis":127954,"endTime":1669644033366,"assignee":"admin","taskId":"30008"}}
    
    
    • 1
    • 2
    • 3

    最基本的完成一个流程的核心功能在上面列举了,先部署流程,再启动流程,再获取任务,再完成某一个任务。

    六、总结

    其实你会发现,流程引擎并没有那么神秘,它强大的功能,就是通过满足BPM2.0规范的xml文件和数据库进行流转的,后面文章在学习BPM2.0规范和Flowable数据库各个表的大致作用等。

    以上代码已放到笔者的Gitee仓库地址:https://gitee.com/xiongbomy/spider-flowable.git
    欢迎大家star和fork。
    建议大家fork此项目到你个人仓库,方便你测试或者基于此开发属于你的流程引擎,当然,基于此进行二次扩展,再拿到公司当内部系统也是可以的,只要不商用拿去卖钱~

    题外话,笔者在公司主要负责公司内部的流程引擎系统的开发,使用的是Flowable,后续会记录学习Flowable的笔记,一是让自己的知识形成体系化,二是让更多想要学习Flowable的同学们得到一点点帮助也好。

  • 相关阅读:
    MagicPods(在Windows电脑上可以实现airpods2弹窗,查看单个耳机电量、充电仓电量)
    10分钟掌握Python缓存
    外汇天眼:多位支持加息放缓!美元走弱黄金上涨
    【TI毫米波雷达笔记】IWR6843AOPEVM-G+DCA1000EVM的mmWave Studio数据读取、配置及避坑
    MySQL 事务隔离级别 理论+实战分析
    MySQL数据库——约束
    物联网AI MicroPython传感器学习 之 RTC时钟模块
    使用kaggle运行机器学习代码的几点注意事项(超重要!!!)
    链表
    etcdctl-管理操作etcd集群
  • 原文地址:https://blog.csdn.net/weixin_44143114/article/details/128088024