来源:blog.csdn.net/zhan107876/

article/details/120815560

  • 总览

  • 一、flowable-ui部署运行

  • 二、绘制流程图

    • 绘图细节:

  • 三、后台项目搭建

  • 四、数据库

  • 五、流程引擎API与服务

    • 代码

  • 五、参考资料

今天分享一个不错的工作流引擎 flowable!

1. 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 登录

4、进入APP.MODELER创建流程,之后可以导出流程到项目中使用,或者配置apache-tomcat-9.0.37\webapps\flowable-ui\WEB-INF\classes\flowable-default.properties连接本地数据库

注意:需要将java驱动jar(mysql-connector-java-5.1.45.jar)复制到 apache-tomcat-9.0.37\webapps\flowable-rest\WEB-INF\lib

这样创建的流程后端程序就能直接使用

2. 绘制流程图

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

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

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

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

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

简单的工作流大概就这些元素(还有很多这里就不扩展了)。下面描述一下工作流是如何流动的。

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

绘图细节:

1、保留流程模型

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

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

最后导出工作流文件

文件内容

<?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文件导入

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

3. 后台项目搭建

后台项目基于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%2B8driver-class-name: com.mysql.jdbc.Driverusername: rootpassword: 123456

4. 数据库

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个)

  • act_ge_bytearray:二进制数据表,如流程定义、流程模板、流程图的字节流文件;

  • act_ge_property:属性数据表(不常用);

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

  • act_hi_actinst:历史节点表,存放流程实例运转的各个节点信息(包含开始、结束等非任务节点);

  • act_hi_attachment:历史附件表,存放历史节点上传的附件信息(不常用);

  • act_hi_comment:历史意见表;

  • act_hi_detail:历史详情表,存储节点运转的一些信息(不常用);

  • act_hi_identitylink:历史流程人员表,存储流程各节点候选、办理人员信息,常用于查询某人或部门的已办任务;

  • act_hi_procinst:历史流程实例表,存储流程实例历史数据(包含正在运行的流程实例);

  • act_hi_taskinst:历史流程任务表,存储历史任务节点;

  • act_hi_varinst:流程历史变量表,存储流程历史节点的变量信息;

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

  • act_id_group:用户组信息表,对应节点选定候选组信息;

  • act_id_info:用户扩展信息表,存储用户扩展信息;

  • act_id_membership:用户与用户组关系表;

  • act_id_user:用户信息表,对应节点选定办理人或候选人信息;

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

  • act_re_deployment:部属信息表,存储流程定义、模板部署信息;

  • act_re_procdef:流程定义信息表,存储流程定义相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;

  • act_re_model:流程模板信息表,存储流程模板相关描述信息,但其真正内容存储在act_ge_bytearray表中,以字节形式存储;

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

  • act_ru_task:运行时流程任务节点表,存储运行中流程的任务节点信息,重要,常用于查询人员或部门的待办任务时使用;

  • act_ru_event_subscr:监听信息表,不常用;

  • act_ru_execution:运行时流程执行实例表,记录运行中流程运行的各个分支信息(当没有子流程时,其数据与act_ru_task表数据是一一对应的);

  • act_ru_identitylink:运行时流程人员表,重要,常用于查询人员或部门的待办任务时使用;

  • act_ru_job:运行时定时任务数据表,存储流程的定时任务信息;

  • act_ru_variable:运行时流程变量数据表,存储运行中的流程各节点的变量信息;

5. 流程引擎API与服务

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

  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 {@Autowiredprivate RepositoryService repositoryService;@Autowiredprivate RuntimeService runtimeService;@Autowiredprivate HistoryService historyService;@Autowiredprivate org.flowable.engine.TaskService taskService;@Autowiredprivate 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的值// 另外,每个查询后面都可以拼条件,内置恁多查询,包括模糊查询,大小比较都有}
}

6. 参考资料

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

  • 猫七姑娘 flowable-6.6.0 运行官方 demo

  • 华格瑞沙 https://www.cnblogs.com/yangjiming/p/10938515.html

Spring Boot + flowable 完美结合,快速实现工作流相关推荐

  1. Spring Boot + Security + Thymeleaf + Activiti 快速开发平台项目

    点击关注公众号,实用技术文章及时了解 项目介绍 Spring Boot + Security + MyBatis + Thymeleaf + Activiti 快速开发平台 基于 Layui 的后台管 ...

  2. Spring Boot 专栏:HelloWord快速入门(第一讲)

    写在前面的话: 没有好的文采,但是有一颗乐于分享心,希望用最精简的语言,描绘每一个demo的构建过程,为开发者提供最容易上手的demo样例. SpringBoot简介 Spring Boot来简化Sp ...

  3. Spring Boot + Flowable 工作流引擎

    想必大家再看这篇文章的时候已经对目前主流的工作流引擎有所了解了.目前主流的工作流开源框架也就 Activiti/Camunda/Flowable 这几个了,在我对这三大工作流引擎简单使用了解后,最后选 ...

  4. 使用Spring Boot和MongoDB快速进行Web应用原型设计

    回到我以前的项目之一,我被要求制作一些应急申请. 时间表紧张,范围简单. 内部编码标准是PHP,因此尝试建立经典的Java EE堆栈将是一个真正的挑战. 而且,说实话,完全过大了. 那怎么办 我趁机尝 ...

  5. 快速学习Spring Boot

    Spring Boot项目搭建 从今天开始,小博将撰写Spring Boot系列的博客.由于是菜鸟自撰的备忘录型博客,大神请无视,不喜勿喷. 使用的开发环境是: IDEA 2017.2 JDK 1.8 ...

  6. Spring boot快速开发Rest服务

    Spring Boot 的优点是快速开发,特别适合构建微服务系统,另外给我们封装了各种经常使用的套件,比如mybatis.hibernate.redis.mongodb等.本文中使用hibernate ...

  7. SpringBoot进击 | 一浅出:Spring Boot简单快速上手书

    为了后继的 Spring Cloud 学习和进阶,我们需要先行了解 Spring Boot. 1.前言 1.1 认识 Spring Boot Spring Boot 是由 Pivotal 团队提供的全 ...

  8. security面试_精选41 道 Spring Boot 面试题,附答案!

    今天跟大家分享下SpringBoot 常见面试题的知识. 1 什么是springboot ? 用来简化spring应用的初始搭建以及开发过程 使用特定的方式来进行配置(properties或yml文件 ...

  9. 精选Spring Boot三十五道必知必会知识点!

    Spring Boot 是微服务中最好的 Java 框架. 我们建议你能够成为一名 Spring Boot 的专家.本文精选了三十五个常见的Spring Boot知识点,祝你一臂之力! 问题一 Spr ...

最新文章

  1. centos7 解决chrome提示您的连接不是私密连接的方法
  2. 人脸检测与识别年度进展概述
  3. shell的数组操作
  4. Centos 系统swap虚拟内存添加与删除配置
  5. Linux下配置服务器节点上的时区
  6. scala基础之类和对象
  7. Hexo多客户端同步问题
  8. 网络编程之计算机网络的发展及基础网络概念
  9. eclipse提交代码至GitHub
  10. 09-1.部署 coredns 插件
  11. 污水流量计常见故障形成原因检测方法
  12. 美国地质勘探局官网(USGS)Landsat 8 OLI_TIRS 影像数据下载详解
  13. 初学vue,模仿个静态网站
  14. 推荐 干掉垃圾流氓插件得批处理文件和注册表文件
  15. php网页设计导航栏代码,总结7种常见的导航条制作实例
  16. TCP中的RST标志(Reset)详解
  17. miRNA生物信息数据分析流程初探(一)
  18. 关于微信商户企业付款给用户限额问题的说明
  19. 学生宿舍管理系统毕业设计(python)
  20. 解读2018:13家开源框架谁能统一流计算?

热门文章

  1. 绘制数据图表的又一利器:C3.js
  2. 简历模板免费下载网站(提供免费下载模板网址)
  3. 苹果手机可以用什么蓝牙耳机?推荐四款AirPods平替蓝牙耳机
  4. JAVASE笔记之Java语言概述
  5. 【Python】一个小工具竟是万能视频下载神器?
  6. 配置并检验 S1 上的安全功能
  7. latex页眉页脚设置
  8. Datart 二开, 增加 边框 媒体组件
  9. CSS3 垂直树状图——运用 :before 和 :after
  10. 北森云计算成都软件测试工程师面试题