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

深入解析Flowable工作流引擎:从原理到实践

1.概述

最近在研究工作流功能,所以趁此机会在这里深入了解下工作流相关知识点和流程引擎开源框架。工作流也可以叫审批流,在当下互联网快速发展和自动化办公的背景下,有很多业务场景都是需要走流程审批的,比如说日常的请假流程:提交申请→领导审批→通过或拒绝→抄送人事→结束。那到底什么是工作流呢?

工作流是什么?

工作流是对工作流程及其各操作步骤之间业务规则的抽象、概括描述。工作流建模,即将工作流程中的工作如何前后组织在一起的逻辑和规则,在计算机中以恰当的模型表达并对其实施计算。工作流要解决的主要问题是:为实现某个业务目标,利用计算机在多个参与者之间按某种预定规则自动传递文档、信息或者任务。简单来说,工作流就是对业务的流程化抽象。

开源的工作流引擎很多,比如 activitiFlowableCamunda 等,后两个都是基于activiti分叉出来开发的,所以这些框架都是同宗同源的,研究一个即可,其他的使用套路,实现原理都是差不多的。这里就来聊聊当下使用较多,比较主流框架:Flowable

2.Flowable核心概念和原理

Flowable是一个基于Apache 2.0许可证的开源工作流引擎,源自Activiti项目分支。它实现了BPMN 2.0规范,提供了完整的流程定义、执行和监控能力。

BPMN(Bussiness Process Model Notation):业务流程管理和符号, 简单来说就是工作流模型,是一种流行的业务流程建模语言。它是为业务流程建模而设计的,用于支持业务流程管理,包括分析、设计、优化和实施。通过提供易于理解的、视觉化的图形表示法,使得非技术性的业务人员也能很好地理解业务过程。

下面是Flowable官网的核心api架构图:

服务接口职责说明典型方法示例
RepositoryService流程定义和部署管理deploy(), createDeployment()
RuntimeService流程运行时控制startProcessInstanceByKey()
TaskService用户任务操作complete(), claim()
HistoryService历史数据查询createHistoricTaskInstanceQuery()
ManagementService引擎管理和维护executeCustomSql()

ProcessEngine:整个Flowable引擎的核心入口,通过它可以获取所有Service

ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
RuntimeService runtimeService = processEngine.getRuntimeService();

核心流程:

3.Spring Boot整合实战

3.1 依赖配置

<dependency><groupId>org.flowable</groupId><artifactId>flowable-spring-boot-starter</artifactId><version>6.7.2</version>
</dependency>

3.2 流程模型定义

设计一个请假流程如下图所示:

在服务的资源文件resources下新建一个processes目录,然后定义一个流程文件:leave-process.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"><process id="leave-process" name="leave-process" isExecutable="true"><startEvent id="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7"/><userTask id="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad" name="请假" flowable:assignee="#{leaveTask}"><documentation>员工请假</documentation></userTask><sequenceFlow id="sid-ff0c2afe-1c1c-4e2f-b2eb-5083c5fadb41" sourceRef="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7" targetRef="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad"/><userTask id="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab" name="组长审批" flowable:assignee="#{teamTask}"/><sequenceFlow id="sid-32a2a643-9ce6-44f3-bb94-f9df3d97c49b" sourceRef="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad" targetRef="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab"/><exclusiveGateway id="sid-c03f8946-a6cd-4e38-881a-70d450e32748" name="组长审批网关"/><sequenceFlow id="sid-bbed6467-cc35-41d2-ad08-2c3b17b8ca83" sourceRef="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab" targetRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748"/><sequenceFlow id="sid-afe09a37-7634-4166-8617-68d5f35edef7" sourceRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748" targetRef="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" name="组长审批通过"><conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"通过")}</conditionExpression></sequenceFlow><sequenceFlow id="sid-df2ad71d-6aa1-4d43-a9f9-bbdad8687e3a" sourceRef="sid-c03f8946-a6cd-4e38-881a-70d450e32748" targetRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" name="组长审批拒绝"><conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"拒绝")}</conditionExpression></sequenceFlow><userTask id="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" name="经理审批" flowable:assignee="#{manageTask}"/><exclusiveGateway id="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" name="经理审批网关"/><sequenceFlow id="sid-ffa6dd8b-ae8d-47a5-a2ff-45d25e58dcac" sourceRef="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687" targetRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53"/><serviceTask id="sid-e720a425-9f57-4a42-9833-b5f4f0175840" flowable:exclusive="true" name="发送失败提示" isForCompensation="true" flowable:class="com.xx.process.service.LeaveFailService"/><endEvent id="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb"/><sequenceFlow id="sid-6e2fc1e3-43be-4768-96f5-d9c7861cbce7" sourceRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" targetRef="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb"/><sequenceFlow id="sid-c90c5e3e-e5c2-40f0-8324-44d548de0ef2" sourceRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" targetRef="sid-e720a425-9f57-4a42-9833-b5f4f0175840" name="经理审批拒绝"><conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"拒绝")}</conditionExpression></sequenceFlow><endEvent id="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1"/><sequenceFlow id="sid-5cad8e7b-8ee3-4e34-b94f-920a362f578d" sourceRef="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53" targetRef="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1" name="经理审批通过"><conditionExpression xsi:type="tFormalExpression">${var:equals(checkResult,"通过")}</conditionExpression></sequenceFlow></process><bpmndi:BPMNDiagram id="BPMNDiagram_ask_for_leave"><bpmndi:BPMNPlane bpmnElement="ask_for_leave" id="BPMNPlane_ask_for_leave"><bpmndi:BPMNShape id="shape-013aa23c-13e6-466e-942b-6a1a308eeda7" bpmnElement="sid-3cb4e010-243b-401b-9ad3-4a1f8ecf35d7"><omgdc:Bounds x="-2235.0" y="-1200.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-10dbd0c8-a3b1-4a9e-b214-5c506e80fd1d" bpmnElement="sid-c60eebb9-3a5b-48f9-963c-d0b064344fad"><omgdc:Bounds x="-2145.0" y="-1224.9999" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-97bf72f7-edcb-4813-9e03-73bc597d1492" bpmnElement="sid-ff0c2afe-1c1c-4e2f-b2eb-5083c5fadb41"><omgdi:waypoint x="-2205.0" y="-1185.0"/><omgdi:waypoint x="-2145.0" y="-1184.9999"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-df2f6c22-1d44-4180-94fb-87753f871822" bpmnElement="sid-dafc35fa-fa0f-49af-9f46-7074fa5a60ab"><omgdc:Bounds x="-1945.0" y="-1225.0001" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-e0d1746e-e6b5-481a-a492-83881d2a9c22" bpmnElement="sid-32a2a643-9ce6-44f3-bb94-f9df3d97c49b"><omgdi:waypoint x="-2045.0" y="-1184.9999"/><omgdi:waypoint x="-1945.0" y="-1185.0001"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-6e0e9d80-5884-4ca5-847d-d3e555005c51" bpmnElement="sid-c03f8946-a6cd-4e38-881a-70d450e32748"><omgdc:Bounds x="-1735.0" y="-1205.0" width="40.0" height="40.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-9dbfbe2b-c839-44eb-b74b-fceee48631b7" bpmnElement="sid-bbed6467-cc35-41d2-ad08-2c3b17b8ca83"><omgdi:waypoint x="-1845.0" y="-1185.0001"/><omgdi:waypoint x="-1735.0" y="-1185.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-1ff96c95-d96d-4241-9d0f-85414bbb3859" bpmnElement="sid-c9544983-0465-43e0-b6fc-1dbd0f1e0687"><omgdc:Bounds x="-1570.0" y="-1224.9999" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-f8f6488f-354a-490c-9d22-1f57e9a654d4" bpmnElement="sid-afe09a37-7634-4166-8617-68d5f35edef7"><omgdi:waypoint x="-1695.0" y="-1185.0"/><omgdi:waypoint x="-1632.5" y="-1185.0"/><omgdi:waypoint x="-1570.0" y="-1184.9999"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-bb52e23c-ee53-4073-9b74-c5dca8459100" bpmnElement="sid-970d87f2-84d0-4603-ae25-163a0ae7ca53"><omgdc:Bounds x="-1395.0" y="-1205.0" width="40.0" height="40.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-6e5e32bc-5b44-41f5-97e6-458c1aa7b7f3" bpmnElement="sid-ffa6dd8b-ae8d-47a5-a2ff-45d25e58dcac"><omgdi:waypoint x="-1470.0" y="-1184.9999"/><omgdi:waypoint x="-1395.0" y="-1185.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-26269b4b-a620-4e2f-a463-145fdfd8c0b1" bpmnElement="sid-e720a425-9f57-4a42-9833-b5f4f0175840"><omgdc:Bounds x="-1764.9999" y="-1055.0" width="100.0" height="80.0"/></bpmndi:BPMNShape><bpmndi:BPMNShape id="shape-47431d28-2f8b-4b2c-81e0-19dc832d1880" bpmnElement="sid-f158c31d-d760-4a06-bb4a-6ff8b156d2fb"><omgdc:Bounds x="-1910.0" y="-1030.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-cfc9460b-eaf6-44ee-9baa-300e89b18f4d" bpmnElement="sid-6e2fc1e3-43be-4768-96f5-d9c7861cbce7"><omgdi:waypoint x="-1764.9999" y="-1015.0"/><omgdi:waypoint x="-1880.0" y="-1015.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-46d26639-0c9b-40da-b5ec-10678783920b" bpmnElement="sid-c90c5e3e-e5c2-40f0-8324-44d548de0ef2"><omgdi:waypoint x="-1375.0" y="-1165.0"/><omgdi:waypoint x="-1375.0" y="-1015.0"/><omgdi:waypoint x="-1664.9998" y="-1015.0"/></bpmndi:BPMNEdge><bpmndi:BPMNShape id="shape-cfc8c0f0-f529-4023-83a6-e8635533be1b" bpmnElement="sid-ea363fd0-f4ad-4537-9faf-a9349aac16f1"><omgdc:Bounds x="-1200.0" y="-1200.0" width="30.0" height="30.0"/></bpmndi:BPMNShape><bpmndi:BPMNEdge id="edge-5c22485e-bc92-4af7-b814-6f384c98ba5d" bpmnElement="sid-5cad8e7b-8ee3-4e34-b94f-920a362f578d"><omgdi:waypoint x="-1355.0" y="-1185.0"/><omgdi:waypoint x="-1200.0" y="-1185.0"/></bpmndi:BPMNEdge><bpmndi:BPMNEdge id="edge-eb5ecc69-91e8-4e50-81e2-14123d77ee2d" bpmnElement="sid-df2ad71d-6aa1-4d43-a9f9-bbdad8687e3a"><omgdi:waypoint x="-1715.0" y="-1165.0"/><omgdi:waypoint x="-1714.9999" y="-1055.0"/></bpmndi:BPMNEdge></bpmndi:BPMNPlane></bpmndi:BPMNDiagram>
</definitions>

这样项目启动之后,Flowable会自动加载这个流程定义文件,写入数据库相关表。

3.3 流程模型使用

基于模型发起一次审批流程:

  @AutowiredRuntimeService runtimeService;@AutowiredTaskService taskService;// 员工idpublic static final String userId = "user_001";// 组长idpublic static final String teamId = "team_001";// 部门经理idpublic static final String depId = "dep_001";@Testpublic void leaveRequest() {HashMap<String, Object> map = new HashMap<>();map.put("leaveTask", userId);// 开启流程的key,就是流程定义文件里 process 标签的idProcessInstance processInstance = runtimeService.startProcessInstanceByKey("leave-process", map);// 设置一些参数runtimeService.setVariable(processInstance.getId(), "name", "java-boy");runtimeService.setVariable(processInstance.getId(), "reason", "想休息个几天,心累了");runtimeService.setVariable(processInstance.getId(), "days", 3);log.info("======>>>创建请假流程, 流程实例processInstanceId:{}", processInstance.getId());}

上面的实现逻辑其实就是员工user001发起了一次请假申请,并附带了一些变量信息,比如说请假几天之类的。

提交请假:

   /*** 员工提交请假*/@Testpublic void submitToTeam() {// 员工查找到自己的任务,然后提交给组长审批List<Task> list = taskService.createTaskQuery().taskAssignee(userId).orderByTaskId().desc().list();for (Task task: list) {Map<String, Object> map = new HashMap<>();// 提交给组长的时候,需要指定组长的 idmap.put("teamTask", teamId);taskService.complete(task.getId(), map);}}

审批:

    /*** 组长批准请假*/@Testpublic void teamApprove() {List<Task> list = taskService.createTaskQuery().taskAssignee(teamId).orderByTaskId().desc().list();for (Task task: list) {Map<String, Object> map = new HashMap<>();//提交给组长的时候,需要指定组长的 idmap.put("manageTask", depId);map.put("checkResult", "通过");map.put("teamTask", teamId);try {taskService.complete(task.getId(), map);} catch (Exception e) {log.error("组长审批失败{} {}", task.getId(), task.getAssignee(), e);}}}}

4.总结

Flowable在功能丰富性和易用性之间取得了良好平衡,虽然表结构看似复杂,但通过合理配置可以大幅简化。对于需要完整BPM功能又希望保持轻量集成的Java项目,它仍然是目前最好的选择之一,非常适合Spring技术栈的项目快速整合工作流。新项目建议从6.7.x版本开始,并合理规划历史数据归档策略。

http://www.xdnf.cn/news/1440577.html

相关文章:

  • 2 XSS
  • 深入掌握sed:Linux文本处理的流式编辑器利器
  • PHP如何解决使用国密SM4解密Base64数据错误问题?(基于lpilp/guomi)
  • 协议分析基础
  • 以技术共享点燃全球能源变革新引擎的智慧能源开源了
  • 低代码革命遇瓶颈?这个“套娃神技“才是破局关键!
  • 在Excel和WPS表格中隔多行插入一个空白行
  • 多场景对练数据的 Excel 横向导出方案(EasyExcel 动态表头实践)
  • 【XR硬件系列】Vivo Vision 与 Apple VisionPro 深度技术对比:MR 时代的轻量化革命与生态霸权
  • 单元测试数据库回滚问题
  • Android音频学习(十六)——CreateTrack
  • 资产管理还靠Excel?深度体验系统如何让企业高效数字化升级!
  • 自然语言处理深层语义分析中公理化体系的可行性、挑战与前沿进展
  • php:PHP 8 新特性深度解析与实战应用:提升开发效率的关键技巧
  • 为何 React JSX 循环需要使用 key
  • 一文弄懂C/C++不定参数底层原理
  • Zygote 进程启动流程
  • 视频判重需求:别为同一内容花两次钱!
  • 涨了一倍多的顺丰同城,还能继续做大即时零售基建的蛋糕吗?
  • HTML5 标题标签、段落、换行和水平线
  • 光谱相机的探测器类型
  • 相机在两个机械臂上安装方式比较
  • 字节跳动后端 一面凉经
  • 单片机:GPIO、按键、中断、定时器、蜂鸣器
  • 知微传感Dkam系列3D相机SDK例程篇:CSharp连接相机及保存数据
  • Debezium日常分享系列之:Debezium 3.3.0.Alpha2发布
  • Gemini CLI源码解析:Agent与上下文管理实现细节
  • Airsim 笔记:Python API 总结
  • ESXI8多网卡链路聚合
  • 渗透测试中的常见误区与最佳实践