什么是BPMN

由BPMI(The Business Process Management Initiative)开发了一套标准叫业务流程建模符号(BPMN - Business Process Modeling Notation)。在 BPMI Notation Working Group超过2年的努力,于2004年5月对外发布了BPMN 1.0 规范。后BPMI并入到OMG组织,OMG于2011年推出BPMN2.0标准,对BPMN进行了重新定义**(Business Process Model and Notation**)。BPMN的主要目标是提供一些被所有业务用户容易理解的符号,从创建流程轮廓的业务分析到这些流程的实现,直到最终用户的管理监控。BPMN也支持提供一个内部的模型可以生成可执行的BPEL4WS。因此BPMN的出现,弥补了从业务流程设计到流程开发的间隙。

BPMN定义了一个业务流程图(Business Process Diagram),该业务流程图基于一个流程图(flowcharting),该流程图被设计用于创建业务流程操作的图形化模型。而一个业务流程模型(Business Process Model),指一个由图形对象(graphical objects)组成的网状图,图形对象包括活动(activities)和用于定义这些活动执行顺序的流程控制器(flow controls)。

什么是Flowable

官方描述如下:

Flowable提供了一个组高效的核心开源业务流程引擎,为开发人员,系统管理员和业务用户提供工作流和业务流程管理(BPM)平台。全部用Java编写,并且基于Apache 2.0许可的开源,代码在社区维护。其核心是一个快速,经过试验和测试的动态BPMN流程引擎,附带DMN决策表和CMMN Case管理引擎.

Flowable官网、开源社区

Flowable官方网站:

​https://www.flowable.org/​

Flowable开源代码仓库:

​https://github.com/flowable/flowable-engine​

Flowable 最新版本(V6.4.0)截止目前下载地址

Tomcat 版本安装包:

​https://github.com/flowable/flowable-engine/releases/download/flowable-6.4.0/tomcat-flowable-6.4.0.zip​

EAR版本版本安装包:

​https://github.com/flowable/flowable-engine/releases/download/flowable-6.4.0/flowable-6.4.0.zip​

Flowable源代码(V6.4.0)下载地址

Flowable流程示例

一个简单度假申请流程主要过程如下:

1、员工提交请假信息,如工号,请假天数

2、经理点击审批通过或者拒绝

3、如果拒绝直接进入系统发邮件服务通知员工

4、如果通过系统记录该员工的请假天数,减少可用天数

5、员工看到自己请假审批通过,点击完成请假。

Flowable的BPMN流程图:

Flowable_d3

流程图的BMPN文件如下:

<?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="holidayRequest" name="Holiday Request" isExecutable="true">
<startEvent id="startEvent"></startEvent>
<sequenceFlow id="sequenceFlow-3fa7af74-df9a-4d3a-b0eb-10b20e7dc202" sourceRef="startEvent" targetRef="approveTask"></sequenceFlow>
<userTask id="approveTask" name="经理通过或驳回申请"></userTask>
<sequenceFlow id="sequenceFlow-d9127dc0-158f-4169-b61b-99226ffe8494" sourceRef="approveTask" targetRef="decision"></sequenceFlow>
<exclusiveGateway id="decision"></exclusiveGateway>
<serviceTask id="externalSystemCall" name="系统记录申请天数" flowable:class="org.flowable.CallExternalSystemDelegate"></serviceTask>
<sequenceFlow id="sequenceFlow-6232f578-3975-4d7e-81f2-43b242d33ebd" sourceRef="externalSystemCall" targetRef="holidayApprovedTask"></sequenceFlow>
<userTask id="holidayApprovedTask" name="员工查看申请通过"></userTask>
<sequenceFlow id="sequenceFlow-29fd3375-9bcb-42f2-bb6f-63ae1f98813a" sourceRef="holidayApprovedTask" targetRef="approveEnd"></sequenceFlow>
<serviceTask id="sendRejectionMail" name="发送驳回邮件" flowable:class="org.flowable.SendRejectionMail"></serviceTask>
<sequenceFlow id="sequenceFlow-b390b442-2411-44a9-96eb-3c129d4c8408" sourceRef="sendRejectionMail" targetRef="rejectEnd"></sequenceFlow>
<endEvent id="approveEnd"></endEvent>
<endEvent id="rejectEnd"></endEvent>
<sequenceFlow id="sequenceFlow-6f21d3e2-2e49-4b00-9aae-0b71d861a177" name="驳回" sourceRef="decision" targetRef="sendRejectionMail">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${!approved}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="sequenceFlow-2470619b-fa36-440b-a231-b7bd226c23bb" name="通过" sourceRef="decision" targetRef="externalSystemCall">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${approved}]]></conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_holidayRequest">
<bpmndi:BPMNPlane bpmnElement="holidayRequest" id="BPMNPlane_holidayRequest">
<bpmndi:BPMNShape bpmnElement="startEvent" id="BPMNShape_startEvent">
<omgdc:Bounds height="30.0" width="30.0" x="0.0" y="95.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="approveTask" id="BPMNShape_approveTask">
<omgdc:Bounds height="60.0" width="100.0" x="80.0" y="80.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="decision" id="BPMNShape_decision">
<omgdc:Bounds height="40.0" width="40.0" x="230.0" y="90.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="externalSystemCall" id="BPMNShape_externalSystemCall">
<omgdc:Bounds height="60.0" width="100.0" x="320.0" y="1.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="holidayApprovedTask" id="BPMNShape_holidayApprovedTask">
<omgdc:Bounds height="60.0" width="100.0" x="470.0" y="1.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="sendRejectionMail" id="BPMNShape_sendRejectionMail">
<omgdc:Bounds height="60.0" width="100.0" x="320.0" y="160.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="approveEnd" id="BPMNShape_approveEnd">
<omgdc:Bounds height="28.0" width="28.0" x="620.0" y="16.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="rejectEnd" id="BPMNShape_rejectEnd">
<omgdc:Bounds height="28.0" width="28.0" x="505.0" y="175.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-2470619b-fa36-440b-a231-b7bd226c23bb" id="BPMNEdge_sequenceFlow-2470619b-fa36-440b-a231-b7bd226c23bb">
<omgdi:waypoint x="250.0" y="90.0"></omgdi:waypoint>
<omgdi:waypoint x="250.0" y="31.0"></omgdi:waypoint>
<omgdi:waypoint x="319.99999999999284" y="31.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-3fa7af74-df9a-4d3a-b0eb-10b20e7dc202" id="BPMNEdge_sequenceFlow-3fa7af74-df9a-4d3a-b0eb-10b20e7dc202">
<omgdi:waypoint x="29.949998618355412" y="110.0"></omgdi:waypoint>
<omgdi:waypoint x="80.0" y="110.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-6232f578-3975-4d7e-81f2-43b242d33ebd" id="BPMNEdge_sequenceFlow-6232f578-3975-4d7e-81f2-43b242d33ebd">
<omgdi:waypoint x="419.95000000000005" y="31.0"></omgdi:waypoint>
<omgdi:waypoint x="470.0" y="31.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-b390b442-2411-44a9-96eb-3c129d4c8408" id="BPMNEdge_sequenceFlow-b390b442-2411-44a9-96eb-3c129d4c8408">
<omgdi:waypoint x="419.949999999997" y="189.66442953020135"></omgdi:waypoint>
<omgdi:waypoint x="505.00030595247523" y="189.0936221647077"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-d9127dc0-158f-4169-b61b-99226ffe8494" id="BPMNEdge_sequenceFlow-d9127dc0-158f-4169-b61b-99226ffe8494">
<omgdi:waypoint x="179.9499999999898" y="110.0"></omgdi:waypoint>
<omgdi:waypoint x="230.0" y="110.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-29fd3375-9bcb-42f2-bb6f-63ae1f98813a" id="BPMNEdge_sequenceFlow-29fd3375-9bcb-42f2-bb6f-63ae1f98813a">
<omgdi:waypoint x="569.9499999999988" y="31.0"></omgdi:waypoint>
<omgdi:waypoint x="582.0" y="31.0"></omgdi:waypoint>
<omgdi:waypoint x="582.0" y="31.0"></omgdi:waypoint>
<omgdi:waypoint x="620.0025321198765" y="30.268220825690175"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="sequenceFlow-6f21d3e2-2e49-4b00-9aae-0b71d861a177" id="BPMNEdge_sequenceFlow-6f21d3e2-2e49-4b00-9aae-0b71d861a177">
<omgdi:waypoint x="250.0" y="129.9375468164794"></omgdi:waypoint>
<omgdi:waypoint x="250.0" y="190.0"></omgdi:waypoint>
<omgdi:waypoint x="319.99999999997794" y="190.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>

文件名为Holiday_Request.bpmn20.xml,flowable支持以BPMN和bpmn20.xml结尾的流程文件。


使用spring boot flowble生成所需表

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


不需配置启动项目即可

找到这几个war包rest除外

Flowable_流程图_02


数据库分成两套

项目数据库:项目所需表和flowable通过jar包生成的表(34张)

Flowable数据库:用于流程图创建保存,测试,监控(74张)

项目部署只需项目数据库

可以使用flowable自带的流程一套工具为了降低耦合只是用流程图 bpmn

tomcat配置flowable流程图

访问http://localhost:8888/flowable-admin/


Flowable_xml_03

账户密码:admin test

Flowable_d3_04

主要负责监控,测试

重点是画流程图

​http://localhost:8888/flowable-modeler/​

Flowable_xml_05

创建流程

Flowable_d3_06


Flowable_流程图_07


第一个按钮是保存(存档)对勾是验证是否配置正确

bpmn导出

Flowable_d3_08

点击查询按钮

Flowable_d3_09


下载按钮导出

数据库表介绍

Flowable的数据库名称都以ACT_开头。第二部分是表的用例的双字符标识。此用例也将大致匹配服务API。

ACT_RE_ *:RE代表repository。具有此前缀的表包含静态信息,例如流程定义和流程资源(图像,规则等)。

ACT_RU_ *:RU代表runtime。这些是包含流程实例,用户任务,变量,作业等的运行时数据的运行时表。Flowable仅在流程实例执行期间存储运行时数据,并在流程实例结束时删除记录。这使运行时表保持小而快。

ACT_HI_ *:HI代表history。这些是包含历史数据的表,例如过去的流程实例,变量,任务等。

ACT_GE_ *:general数据,用于各种用例。

事件监听器实现

事件监听器的唯一要求是实现org.flowable.engine.delegate.event.FlowableEventListener。下面是一个侦听器的示例实现,它将收到的所有事件输出到标准输出,但与作业执行相关的事件除外:

public class MyEventListener implements FlowableEventListener {

@Override

public void onEvent(FlowableEvent event) {

if (event.getType().equals(FlowableEngineEventType.JOB_EXECUTION_SUCCESS)) {

System.out.println("一个任务执行成功");

} else if (event.getType().equals(FlowableEngineEventType.JOB_EXECUTION_FAILURE)) {

System.out.println("一个任务执行失败");

} else {

System.out.println(event.getType());

}

}

//该isFailOnException()方法确定onEvent(..)方法在调度事件时抛出异常时的行为。

// 当false返回,异常被忽略。当true返回,异常不会被忽略

@Override
public boolean isFailOnException() {
return false;
}
//该isFireOnTransactionLifecycleEvent()方法确定此事件侦听器是在事件发生时立即触发还是由
// getOnTransaction()方法确定的事务生命周期事件触发。
//支持的事务生命周期事件的值是:COMMITTED,ROLLED_BACK,COMMITTING,ROLLINGBACK。

@Override
public boolean isFireOnTransactionLifecycleEvent() {
return false;
}



@Override
public String getOnTransaction() {
return null;
}

}

基础类的监听


org.flowable.engine.delegate.event.BaseEntityEventListener


一个事件侦听器基类,可用于侦听特定类型的实体或所有实体的实体相关事件。它隐藏掉类型检查,并提供4种方法应覆盖:onCreate(..),onUpdate(..)并onDelete(..)创建实体时,更新或删除。对于所有其他与实体相关的事件,onEntityEvent(..) 调用它

运行的监听器

通过RuntimeService调用

添加监听器
void addEventListener(FlowableEventListener listenerToAdd);
添加监听器并指定类型
void addEventListener(FlowableEventListener listenerToAdd, FlowableEventType... types);
删除监听器
void removeEventListener(FlowableEventListener listenerToRemove);

下面列出了引擎中可能出现的所有事件类型。每种类型对应于中的枚举值org.flowable.engine.common.api.delegate.event.FlowableEventType

Flowable_流程图_10

Flowable_d3_11

Flowable_流程图_12

Flowable_d3_13

Flowable_d3_14

所有ENTITY_\*事件都与引擎内的实体相关


ENTITY_CREATED, ENTITY_INITIALIZED, ENTITY_DELETED:附件,注释,部署,执行,组,IdentityLink,作业,模型,ProcessDefinition,ProcessInstance,任务,用户。


ENTITY_UPDATED:附件,部署,执行,组,IdentityLink,作业,模型,ProcessDefinition,ProcessInstance,任务,用户。


ENTITY_SUSPENDED, ENTITY_ACTIVATED:ProcessDefinition,ProcessInstance / Execution,Task。


Process engine API和服务

Flowable_d3_15

RepositoryService

主要是关于静态信息(数据不会改变,或者至少不是很多)


查询引擎已知的部署和流程定义。


暂停和激活整个部署或特定流程定义。暂停意味着不能对它们执行进一步的操作,而激活则相反并且再次启用操作。


检索各种资源,例如部署中包含的文件或引擎自动生成的流程图。


检索流程定义的POJO版本,该版本可用于使用Java而不是XML来内省流程


RuntimeService

但RuntimeService恰恰相反。它涉及启动流程定义的新流程实例。

TaskService

需要由系统的用户执行的任务是BPM引擎(如Flowable)的核心。围绕任务的所有内容都在TaskService中进行分组:


查询分配给用户或组的任务


创建新的独立任务。这些是与流程实例无关的任务。


操作分配任务的用户或以某种方式参与任务的用户。


声称并完成任务。声称意味着某人决定成为该任务的受让人,这意味着该用户将完成该任务。完成意味着完成任务的工作。通常,这是填写各种形式。


IdentityService

它支持组和用户的管理(创建,更新,删除,查询......)。


formService

是一个可选的服务,将flowable里的表单关联到dpmn中(不使用)。


HistoryService:

暴露了可流动的引擎收集的所有历史数据例如流程实例启动时间,执行哪些任务,完成任务所需的时间,每个流程实例中遵循的路径,等等。此服务主要公开查询功能以访问此数据。

ManagementService

该ManagementService编码使用可流动的自定义应用程序时,通常是没有必要的。它允许检索有关数据库表和表元数据的信息。此外,它还公开了作业的查询功能和管理操作。Flowable中的作业用于各种事物,例如计时器,异步延续,延迟暂停/激活等。稍后,将更详细地讨论这些主题。


DynamicBpmnService(多出来特殊的一个)

可以用来改变流程定义的一部分,而无需重新部署。例如,您可以更改流程定义中用户任务的受理人定义,或更改服务任务的类名称。

ProcessEngines.getDefaultProcessEngine()将在第一次调用时初始化并构建流程引擎,之后始终返回相同的流程引擎。可以使用ProcessEngines.init() 和完成所有流程引擎的正确创建和关闭ProcessEngines.destroy()


异常

org.flowable.engine.FlowableException


FlowableWrongDbException:当Flowable引擎发现数据库架构版本与引擎版本不匹配时抛出。


FlowableOptimisticLockingException:在由同一数据条目的并发访问引起的数据存储中发生乐观锁定时抛出。


FlowableClassLoadingException:在未找到请求加载的类或加载时发生错误(例如JavaDelegates,TaskListeners,...)时抛出。


FlowableObjectNotFoundException: Thrown when an object that is requested or actioned does not exist.


FlowableIllegalArgumentException:一个异常,指示在Flowable API调用中提供了非法参数,在引擎配置中配置了非法值,或者提供了非法值,或者在流程定义中使用了非法值。


FlowableTaskAlreadyClaimedException:当已经声明任务时,在taskService.claim(…)被调用时抛出。



关注公众号 soft张三丰 

Flowable_d3_16