專業詞匯
處理分發的事件的另一個方法,是拋出BPMN事件,實體,entityType
可用的值有:attachment
(附件), comment
(備注), execution
(執行), identity-link
(身份關聯), job
(作業), process-instance
(流程實例), process-definition
(流程定義), task
(任務)。throwEvent="globalSignal"
流程定義的xml
文檔位置:2.3.2. 部署流程定義
<?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: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" xmlns:flowable="http://flowable.org/bpmn" 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"/> <sequenceFlow sourceRef="startEvent" targetRef="approveTask"/> <userTask id="approveTask" name="Approve or reject request"/> <sequenceFlow sourceRef="approveTask" targetRef="decision"/> <exclusiveGateway id="decision"/> <sequenceFlow sourceRef="decision" targetRef="externalSystemCall"> <conditionExpression xsi:type="tFormalExpression"> <![CDATA[ ${approved} ]]> </conditionExpression> </sequenceFlow> <sequenceFlow sourceRef="decision" targetRef="sendRejectionMail"> <conditionExpression xsi:type="tFormalExpression"> <![CDATA[ ${!approved} ]]> </conditionExpression> </sequenceFlow> <serviceTask id="externalSystemCall" name="Enter holidays in external system" flowable:class="org.flowable.CallExternalSystemDelegate"/> <sequenceFlow sourceRef="externalSystemCall" targetRef="holidayApprovedTask"/> <userTask id="holidayApprovedTask" name="Holiday approved"/> <sequenceFlow sourceRef="holidayApprovedTask" targetRef="approveEnd"/> <serviceTask id="sendRejectionMail" name="Send out rejection email" flowable:class="org.flowable.SendRejectionMail"/> <sequenceFlow sourceRef="sendRejectionMail" targetRef="rejectEnd"/> <endEvent id="approveEnd"/> <endEvent id="rejectEnd"/> </process> </definitions>
1.選用mysql數據庫的時候選擇5.6.4及其以上,如果要較低版本的flowalbe支持比較高的mysql,需要加入如下配置:
要進行升級,首先需要將下列配置參數放入你的flowable.cfg.xml配置文件:
<beans >
<bean id="processEngineConfiguration" class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration">
<!-- ... --> <property name="databaseSchemaUpdate" value="true" />
<!-- ... --> </bean>
</beans>
同時需要改驅動
2.選擇flowable版本6.0.1版本及其 以上較好性能。
開啟異步執行器配置(async executor)獲得較好性能
<property name="asyncExecutorActivate" value="true" />
3.從Flowable 6.1.0起,添加了異步歷史功能,需要在配置文件中開啟
4.默認情況下,Flowable引擎依賴中不提供SFL4J綁定JAR。你需要自行將其加入你的項目,以便使用所選的日志框架。如果沒有加入實現JAR,SLF4J會使用NOP-logger,需要寫上版本號
<dependency> <groupId>org.slf4j</groupId> <artifactId>jcl-over-slf4j</artifactId> </dependency>
5.重要章節 3.18
對事件監聽器的唯一要求,是要實現org.flowable.engine.delegate.event.FlowableEventListener
接口
在配置文件中配置:
<bean id="processEngineConfiguration" class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration"> ... <property name="eventListeners"> <list> <bean class="org.flowable.engine.example.MyEventListener" /> </list> </property> </bean>
public class MyEventListener implements FlowableEventListener { @Override public void onEvent(FlowableEvent event) { switch (event.getType()) { case JOB_EXECUTION_SUCCESS: System.out.println("A job well done!"); break; case JOB_EXECUTION_FAILURE: System.out.println("A job has failed..."); break; default: System.out.println("Event received: " + event.getType()); } } @Override public boolean isFailOnException() { // onEvent方法中的邏輯並不重要,可以忽略日志失敗異常…… return false; } }
onEvent(..)
方法拋出異常,如果isFailOnException()返回false,將不做額外處理,即不向上拋異常。若返回true
,異常不會被忽略而會被上拋,使當前執行的命令失敗。如果事件是API調用(或其他事務操作,例如作業執行)的一部分,事務將被回滾。如果事件監聽器中並不是重要的業務操作,建議返回false
。
提供了一些實現類,例如:
org.flowable.engine.delegate.event.BaseEntityEventListener: 事件監聽器基類,可用來監聽實體(entity)相關事件,特定或所有實體的事件都可以。它隱藏了類型檢測,提供了4個需要覆蓋的方法:onCreate(..)
, onUpdate(..)
與onDelete(..)
在實體創建、更新及刪除時調用;對所有其他實體相關事件,onEntityEvent(..)
會被調用
各類監聽器配置
1.通用監聽器(所以事件都會觸發,例如JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE等等),
eventListeners這個關鍵字來指定,粉紅色部分。
<bean id="processEngineConfiguration" class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration"> ... <property name="eventListeners"> <list> <bean class="org.flowable.engine.example.MyEventListener" /> </list> </property> </bean>
2.特定事件監聽器:
typedEventListeners這個關鍵字來指定,粉紅色部分。指定了:JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE
<bean id="processEngineConfiguration" class="org.flowable.engine.impl.cfg.StandaloneProcessEngineConfiguration"> ... <property name="typedEventListeners"> <map> <entry key="JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE" > <list> <bean class="org.flowable.engine.example.MyJobEventListener" /> </list> </entry> </map> </property> </bean>
最后監聽的順序是安裝list來監聽的。
3.為流程定義增加監聽器
下面的代碼片段為流程定義增加了2個監聽器。第一個監聽器接收任何類型的事件,使用完全限定類名定義。第二個監聽器只在作業成功執行或失敗時被通知,使用流程引擎配置中beans
參數定義的bean作為監聽器。
<process id="testEventListeners"> <extensionElements> <flowable:eventListener class="org.flowable.engine.test.MyEventListener" /> <flowable:eventListener delegateExpression="${testEventListener}" events="JOB_EXECUTION_SUCCESS,JOB_EXECUTION_FAILURE" /> </extensionElements> ... </process>
實體相關的事件(請假就是一個實體,下面例子即是請假創建:ENTITY_CREATED)也可以在流程定義中增加監聽器,只有在特定實體類型的事件發生時得到通知。下面的代碼片段展示了如何設置。可以響應實體的所有事件(第一個例子),或只響應實體的特定類型事件(第二個例子)。
<process id="testEventListeners"> <extensionElements> <flowable:eventListener class="org.flowable.engine.test.MyEventListener" entityType="task" /> <flowable:eventListener delegateExpression="${testEventListener}" events="ENTITY_CREATED" entityType="task" /> </extensionElements> ... </process>
關於流程定義監聽器的說明(不是很懂)
-
事件監聽器只能作為
extensionElements
的子元素,聲明在process
元素上。不能在個別節點(activity)上定義(事件)監聽器。 -
delegateExpression
中的表達式,與其他表達式(例如在網關中的)不一樣,不可以訪問執行上下文。只能夠引用在流程引擎配置中beans
參數定義的bean;或是在使用spring(且沒有定義beans參數)時,引用任何實現了監聽器接口的spring bean。 -
使用監聽器的
class
屬性時,只會創建唯一一個該類的實例。請確保監聽器實現不依賴於成員變量,或確保多線程/上下文的使用安全。 -
如果
events
屬性使用了不合法的事件類型,或者使用了不合法的throwEvent
值,會在流程定義部署時拋出異常(導致部署失敗)。如果class
或delegateExecution
指定了不合法的值(不存在的類,不存在的bean引用,或者代理類沒有實現監聽器接口),在流程啟動(或該流程定義的第一個有效事件分發給這個監聽器)時,會拋出異常。請確保引用的類在classpath中,並且保證表達式能夠解析為有效的實例。