SpringBoot整合Flowable工作流-1(画流程定义)

6,148 阅读3分钟

1. 前言

由于内容比较多,一篇文章可能还介绍不完,接下来可能会分成几篇文章进行介绍。


相信很多人都听说过或者了解过工作流,也在生活中接触不少,比如OA系统的审批。 目前国内有很多工作流引擎,但是大多数都是集成第三方国外的引擎做二次开发的。

2. Flowable 简单介绍

Flowable 是一个用Java编写的轻量级业务流程引擎。Flowable 流程引擎允许您部署BPMN 2.0流程定义(用于定义流程的行业XML标准)、创建这些流程定义的流程实例、运行查询、访问活动或历史流程实例和相关数据。

Flowable 在将其添加到应用程序/服务/体系结构时非常灵活。您可以将引擎嵌入到您的应用程序或服务中,方法是包含 Flowable库,该库作为JAR提供。因为它是一个JAR,所以可以很容易地将它添加到任何Java环境中:javase;servlet容器,如Tomcat或Jetty、Spring;javaee服务器,如JBoss或WebSphere等。或者,您可以使用可流动的restapi通过HTTP进行通信。还有一些可流动的应用程序(Flowable Modeler、Flowable Admin、Flowable IDM 和 Flowable Task),它们提供了用于处理流程和任务的现成示例ui。

所有设置Flowable的方法都有一个共同点,那就是核心引擎,它可以看作是一个服务集合,公开api来管理和执行业务流程。【摘录于Flowable Getting Started】

在这里插入图片描述

3. 看一下效果

【流程定义管理】

在这里插入图片描述

【任务管理 - 待办任务】

在这里插入图片描述

【任务管理 - 已办任务】

在这里插入图片描述

【任务管理 - 范围任务】

在这里插入图片描述

【任务管理 - 我发起的】

在这里插入图片描述

【业务列表】

在这里插入图片描述

在这里插入图片描述

4. 创建流程定义(画流程图)

问:如何画流程图呢?用什么画呢? 答:Flowable 流程引擎允许部署bpmn2.0流程定义,说明bpmn2.0流程定义是一个比较通用的规范,因此也是有不少工具的,常用的开发工具,如 Eclpise、IDEA 这些都是可以画的,但是我是使用 Flowable 提供的 ui 工具画的,或者你也可以使用我搭建出来的在线工具画。

4.1 自己搭建流程设计器服务

如果你想自己搭建 flowable-ui 也是比较方便的,首先下载文件 Flowable 相关的资源,进入 flowable.com/open-source…,然后点击 【Download Flowable v6.x.x】,下载下来是一个压缩包,解压压缩会看到如下目录结构

├─database                                   # 数据库文件
│  ├─create        
│  └─upgrade  
├─docs                                       # 文档
├─libs                                       # 相关的jar
└─wars
   ├─flowable-rest.war                       # rest 服务(基于 SpringBoot)
   ├─flowable-ui.war                         # 流程ui设计器(基于 SpringBoot)
   └─data

在目录 $/wars/flowable-ui.war 下,看到是一个war包,可以放到tomcat中也可以使用命令跑起来

java -jar flowable-ui.war

默认是8080端口,你可以修改端口,首次登录需要账号和密码。 账号:admin 密码:test

4.2 使用在线流程设计器服务

其实我也是基于 flowable-ui 搭建的,我部署到我的学生服务器上面了,并且提供了外网访问地址,如下

在这里插入图片描述

地址:flowable.ui.ihouyu.cn/flowable-ui… 账号:admin 密码:test

4.3 画第一个流程图

登录后,你可以【创建流程】也可以【导入流程】,如下图 在这里插入图片描述

点击对应的流程进入流程管理页面。

在这里插入图片描述

第一个流程图想实现的效果:

提交流程
----主管审批?
	----主管拒绝,流程结束(失败)
	----主管同意
		----经理审批?
			----经理拒绝,流程结束(失败)
			----经理同意,流程结束(成功)

进入可视化编辑器后,需要重点了解一下的是:流程标识(启动流程的Key)名称(流程的名称)数据对象(存储流程的数据变量等)

在这里插入图片描述

全局数据对象的使用

后面可以通过代码 BpmnModel 对象获取到流程图里边的所有定义信息

在这里插入图片描述

添加开始事件

在这里插入图片描述

添加用户活动(用户审批)

在这里插入图片描述

添加网关

在这里插入图片描述

添加结束事件

在这里插入图片描述

设置【主管审批】的主键ID、分配用户等

1.主键ID:可以结合业务需求,业务可以根据不同步骤执行不同的业务操作 2.分配用户:这里为了方便,直接选择了固定值,这里的候选组是具体的业务系统的角色ID从而实现不同角色审批不同流程的效果

在这里插入图片描述

设置【网关】的流条件

连线条件的输入框是支持EL表达式,因此可以使用【同意的条件】:这里填写了{} 【同意的条件】:这里填写了 {executeType == 'YES'} 的意思是主管审批的时候传递一个 executeType 变量,并且 executeType 变量的值为 YES 的时候,则走到下一步(经理审批)

在这里插入图片描述

【拒绝的条件】:这里填写了 ${executeType == 'NO'} 的意思是主管审批的时候传递一个 executeType 变量,并且 executeType 变量的值为 NO 的时候,则结束流程。

在这里插入图片描述

最后下载下来之后是一个“请假流程1.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="Leave1" name="请假流程1" isExecutable="true">
    <documentation>Leave描述信息1</documentation>
    <dataObject id="businessDataViewUri" name="业务数据视图链接" itemSubjectRef="xsd:string">
      <extensionElements>
        <flowable:value>/workflow/leave-info</flowable:value>
      </extensionElements>
    </dataObject>
    <dataObject id="businessNotifyUri" name="业务通知链接" itemSubjectRef="xsd:string">
      <extensionElements>
        <flowable:value>/workflow/test/leave/workflow-notify</flowable:value>
      </extensionElements>
    </dataObject>
    <startEvent id="startEvent1" flowable:formFieldValidation="true"></startEvent>
    <userTask id="zgsp1" name="主管审批" flowable:candidateGroups="847922232536731648" flowable:formFieldValidation="true"></userTask>
    <sequenceFlow id="sid-28954DF2-874B-43D5-9057-AD04F13D11BC" sourceRef="startEvent1" targetRef="zgsp1"></sequenceFlow>
    <exclusiveGateway id="sid-CFB93847-6029-435E-892E-7FC34AE2A425"></exclusiveGateway>
    <sequenceFlow id="sid-8C1A8676-53FC-4A62-920D-24F38C57B603" sourceRef="zgsp1" targetRef="sid-CFB93847-6029-435E-892E-7FC34AE2A425"></sequenceFlow>
    <userTask id="jlsp1" name="经理审批" flowable:candidateGroups="847922477412782081" flowable:formFieldValidation="true"></userTask>
    <endEvent id="sid-00609504-6F1A-48AE-AA66-058B01A367FB"></endEvent>
    <exclusiveGateway id="sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151"></exclusiveGateway>
    <sequenceFlow id="sid-3F6C0888-623F-42C2-8F06-397C83F44DCE" sourceRef="jlsp1" targetRef="sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151"></sequenceFlow>
    <endEvent id="sid-96443FBA-145F-43DE-AE91-D11B5A0115F2"></endEvent>
    <endEvent id="sid-8DE7ABDB-F814-41D5-B1EC-FE0628D7D810"></endEvent>
    <sequenceFlow id="sid-21A1CF4B-19B7-4DE1-A777-C2C4764A6D05" name="拒绝" sourceRef="sid-CFB93847-6029-435E-892E-7FC34AE2A425" targetRef="sid-00609504-6F1A-48AE-AA66-058B01A367FB">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${executeType == 'NO'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-F7498CFB-1603-4E64-A788-92CBCBBEC794" name="拒绝" sourceRef="sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151" targetRef="sid-8DE7ABDB-F814-41D5-B1EC-FE0628D7D810">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${executeType == 'NO'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-8866076B-0723-41A2-AACF-DDC7B06226D9" name="同意" sourceRef="sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151" targetRef="sid-96443FBA-145F-43DE-AE91-D11B5A0115F2">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${executeType == 'YES'}]]></conditionExpression>
    </sequenceFlow>
    <sequenceFlow id="sid-27560005-7B06-4F2D-BD9A-7FE27C7410A0" name="同意" sourceRef="sid-CFB93847-6029-435E-892E-7FC34AE2A425" targetRef="jlsp1">
      <conditionExpression xsi:type="tFormalExpression"><![CDATA[${executeType == 'YES'}]]></conditionExpression>
    </sequenceFlow>
  </process>
  <bpmndi:BPMNDiagram id="BPMNDiagram_Leave1">
    <bpmndi:BPMNPlane bpmnElement="Leave1" id="BPMNPlane_Leave1">
      <bpmndi:BPMNShape bpmnElement="startEvent1" id="BPMNShape_startEvent1">
        <omgdc:Bounds height="30.0" width="30.0" x="100.0" y="163.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="zgsp1" id="BPMNShape_zgsp1">
        <omgdc:Bounds height="80.0" width="100.0" x="210.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-CFB93847-6029-435E-892E-7FC34AE2A425" id="BPMNShape_sid-CFB93847-6029-435E-892E-7FC34AE2A425">
        <omgdc:Bounds height="40.0" width="40.0" x="390.0" y="158.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="jlsp1" id="BPMNShape_jlsp1">
        <omgdc:Bounds height="80.0" width="100.0" x="525.0" y="138.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-00609504-6F1A-48AE-AA66-058B01A367FB" id="BPMNShape_sid-00609504-6F1A-48AE-AA66-058B01A367FB">
        <omgdc:Bounds height="28.0" width="28.0" x="396.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151" id="BPMNShape_sid-E68077F0-E0AA-49DA-B0C2-BE8A0693E151">
        <omgdc:Bounds height="40.0" width="40.0" x="720.0" y="158.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-96443FBA-145F-43DE-AE91-D11B5A0115F2" id="BPMNShape_sid-96443FBA-145F-43DE-AE91-D11B5A0115F2">
        <omgdc:Bounds height="28.0" width="28.0" x="840.0" y="164.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNShape bpmnElement="sid-8DE7ABDB-F814-41D5-B1EC-FE0628D7D810" id="BPMNShape_sid-8DE7ABDB-F814-41D5-B1EC-FE0628D7D810">
        <omgdc:Bounds height="28.0" width="28.0" x="726.0" y="270.0"></omgdc:Bounds>
      </bpmndi:BPMNShape>
      <bpmndi:BPMNEdge bpmnElement="sid-F7498CFB-1603-4E64-A788-92CBCBBEC794" id="BPMNEdge_sid-F7498CFB-1603-4E64-A788-92CBCBBEC794">
        <omgdi:waypoint x="740.0" y="197.9405984919887"></omgdi:waypoint>
        <omgdi:waypoint x="740.0" y="270.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-21A1CF4B-19B7-4DE1-A777-C2C4764A6D05" id="BPMNEdge_sid-21A1CF4B-19B7-4DE1-A777-C2C4764A6D05">
        <omgdi:waypoint x="410.0" y="197.9405984919887"></omgdi:waypoint>
        <omgdi:waypoint x="410.0" y="270.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-8866076B-0723-41A2-AACF-DDC7B06226D9" id="BPMNEdge_sid-8866076B-0723-41A2-AACF-DDC7B06226D9">
        <omgdi:waypoint x="759.9412576686476" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="840.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-27560005-7B06-4F2D-BD9A-7FE27C7410A0" id="BPMNEdge_sid-27560005-7B06-4F2D-BD9A-7FE27C7410A0">
        <omgdi:waypoint x="429.94395820712947" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="524.9999999999847" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-28954DF2-874B-43D5-9057-AD04F13D11BC" id="BPMNEdge_sid-28954DF2-874B-43D5-9057-AD04F13D11BC">
        <omgdi:waypoint x="129.94999913076796" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="209.99999999998067" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-8C1A8676-53FC-4A62-920D-24F38C57B603" id="BPMNEdge_sid-8C1A8676-53FC-4A62-920D-24F38C57B603">
        <omgdi:waypoint x="309.9499999999581" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="390.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
      <bpmndi:BPMNEdge bpmnElement="sid-3F6C0888-623F-42C2-8F06-397C83F44DCE" id="BPMNEdge_sid-3F6C0888-623F-42C2-8F06-397C83F44DCE">
        <omgdi:waypoint x="624.9499999998898" y="178.0"></omgdi:waypoint>
        <omgdi:waypoint x="720.0" y="178.0"></omgdi:waypoint>
      </bpmndi:BPMNEdge>
    </bpmndi:BPMNPlane>
  </bpmndi:BPMNDiagram>
</definitions>

到这里,就画好了第一个简单的流程图了。 下篇博客将介绍根据流程图 基于 SpringBoot 整合 Flowable 的代码实现。


程序员[ 后宇 ],是一个关注编程,热爱技术的Java后端开发者,热衷于 [ Java后端 ],[ 数据爬虫领域 ]。不定期分享 IT 技能和干货!!欢迎关注 "IT加载中",一个只出 干货 和 实战 的公众号。