【Activiti學習之五】BPMN事件


環境
  JDK 1.8
  MySQL 5.6
  Tomcat 7
  Eclipse-Luna
  activiti 6.0

一、事件定義
1、定時器事件
(1)timeDate:指定時間觸發
<timerEventDefinition>
<timeDate>2019-09-12T07:30:00</timeDate>
</timerEventDefinition>
ISO8601時間格式,如果要加時間需要前面加T。

(2)timeDuration:指定一個時間段之后執行
<timerEventDefinition>
<timeDuration>PT1S</timeDuration>
</timerEventDefinition>
S表示秒,M表示分,D表示天;P表示時間段,T表示精確到時間的時間段;

(3)timeCycle:指定定時器的運行周期
<timerEventDefinition>
<timeCycle>R2/PT1M</timeCycle>
</timerEventDefinition>
R表示需要執行的次數,R2/PT1M表示執行兩次,每次持續1分鍾
支持cron表達式:秒 分鍾 小時 月份中的日期 月份 星期中的日期 年份

2、錯誤事件
處理流程中出現的異常。
舉例:
<error id="myerror" errorCode="123"></error>
<process id="testp" name="testp">
<endEvent id="myerrorevent">
<errorEventDefinition errorRef="myerror"></errorEventDefinition>
</endEvent>
</process>

3、信號事件
使用信號給流程發送廣播。
<signal id="signalA" name="signalA"></signal>
<process id="testp" name="testp">
...
<signalEventDefinition signalRef="signalA"></signalEventDefinition>
...
</process>

4、消息事件
消息指向某個接收人,而不是整個流程。
<message id="mymessage" name="mymessage"></message>
<process id="testp" name="testp">
...
<messageEventDefinition messageRef="mymessage"></messageEventDefinition>
...
</process>

5、取消事件
取消事件使用在事務子流程中。
<cancelEventDefinition></cancelEventDefinition>

6、補償事件
補償事件對於已經完成的流程做回退處理。
<componsateEventDefinition></componsateEventDefinition>

7、其他事件
條件事件:<ConditionalEventDefinition>
升級事件:<EscalationEventDefinition>
連接事件:<LinkEventDefinition>
終止事件:<TerminateEventDefinition>

參考:BPMN事件分類與事件定義

二、開始事件
1、無指定開始事件
不為開始事件指定任何觸發條件。
<startEvent id="s" name="s"></startEvent>

2、定時器開始事件
<startEvent id="s" name="s">
<timerEventDefinition id="ss" name="ss">
<timeCycle>PT1S</timeCycle>
</timerEventDefinition>
</startEvent>

3、消息開始事件
<message id="mymessage" name="mymessage"></message>
<process id="testp" name="testp">
<startEvent id="s" name="s">
<messageEventDefinition messageRef="mymessage"></messageEventDefinition>
</startEvent>


4、錯誤開始事件
錯誤開始事件只能用在事件子流程中。
<error id="errorId" errorCode="MyErrorCode"></error>
<process id="errorStartEventTest01" name="錯誤開始事件測試01" isExecutable="true">
<startEvent id="start"></startEvent>
<serviceTask id="serviceTask01" name="Service Task" activiti:class="com.bug315.event.ErrorStartEventDelegate"></serviceTask>
<endEvent id="end"></endEvent>

<sequenceFlow id="sid-1EAADC30-396A-4E19-B181-C688B909510B" sourceRef="serviceTask01" targetRef="end"></sequenceFlow>
<sequenceFlow id="sid-07875D4F-D2C7-47E0-B4C3-6866F110206F" sourceRef="start" targetRef="serviceTask01"></sequenceFlow>

<subProcess id="sid-DDBC6BA0-A097-4FC9-8023-C40E6AD0B499" name="subProcess" triggeredByEvent="true">
<startEvent id="subStart">
<errorEventDefinition errorRef="errorId"></errorEventDefinition>
</startEvent>
<serviceTask id="subServiceTask01" name="Sub Task" activiti:class="com.bug315.event.ErrorHandlerDelegate"></serviceTask>
<endEvent id="subEnd"></endEvent>
<sequenceFlow id="sid-24413ADF-BD05-4BB7-9FA8-E70154B94767" sourceRef="subServiceTask01" targetRef="subEnd"></sequenceFlow>
<sequenceFlow id="sid-A130E45F-9A0B-4362-908E-C8FEB3F640E7" sourceRef="subStart" targetRef="subServiceTask01"></sequenceFlow>
</subProcess>
</process>

參考:開始事件

三、結束事件
1、無指定結束事件
<endEvent id="subEnd" name=""></endEvent>

2、錯誤結束事件
錯誤結束事件一般用在子流程中,一般和錯誤邊界事件一起使用才有意義。
<endEvent id="subEnd">
<errorEventDefinition errorRef="errorId"></errorEventDefinition>
</endEvent>

3、取消結束事件和取消邊界事件
取消結束事件只能用在事務子流程中,該事件表示事務將會取消,並且會觸發附在事務子流程上的取消邊界事件。

參考:結束事件
四、邊界事件
附屬於某個流程元素(如任務、子流程等)的事件為邊界事件。邊界事件是Catching事件,會等待被觸發,當邊界事件被觸發,當前的活動會被中斷,並且當前的順序流會發生轉移。
1、定時器邊界事件
需要注意的是:需要開啟JobExecutor,cancelActivity屬性為true表示原先的流程被中斷,false表示不中斷,繼續執行
示例:
<process id="myProcess" name="My process" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="初級工程師處理"></userTask>
<userTask id="usertask2" name="中級工程師處理"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
<boundaryEvent id="boundarytimer1" name="Timer"
attachedToRef="usertask1" cancelActivity="true">
<timerEventDefinition>
<timeDuration>PT5S</timeDuration>
</timerEventDefinition>
</boundaryEvent>
....
</process>

2、錯誤邊界事件
錯誤邊界事件依附在某個流程活動中,用於捕獲子流程中拋出的錯誤,因此錯誤邊界事件使用在嵌入子流程或調用子流程中。
<process id="ebProcess" name="ebProcess">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<subProcess id="subprocess1" name="Sub Process">
<startEvent id="startevent2" name="Start"></startEvent>
<serviceTask id="servicetask1" name="Throw Error Task"
activiti:class="org.crazyit.activiti.ThrowErrorDelegate"></serviceTask>
<endEvent id="endevent2" name="ErrorEnd"></endEvent>
<sequenceFlow id="flow4" name="" sourceRef="startevent2"
targetRef="servicetask1"></sequenceFlow>
<sequenceFlow id="flow5" name="" sourceRef="servicetask1"
targetRef="endevent2"></sequenceFlow>
</subProcess>
<boundaryEvent id="boundaryerror1" cancelActivity="false"
attachedToRef="subprocess1">
<errorEventDefinition errorRef="abc"></errorEventDefinition>
</boundaryEvent>
....
</process>

3、信號邊界事件
信號邊界事件的觸發條件是接收到信號,但是不一樣的是,信號邊界事件具有全局性,換言之,信號邊界事件會進行全局范圍的信號捕獲。與定時器邊界事件類似,信號邊界事件同樣存在可中斷與不可中斷兩類,可以為boundaryEvent元素設置cancelActivity屬性,如果設置為true,那么原來的執行流將會被中斷,設置為false,則原來的執行流仍然存在。如果多個信號邊界事件使用了相同的信號,當在某個地方發出信號時,即使在不同的流程實例中,這些信號邊界事件均會捕獲到該信號。
<signal id="contactChangeSignal" name="contactChangeSignal"></signal>
<process id="sbProcess" name="sbProcess">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="簽訂查看"></userTask>
<userTask id="usertask2" name="合同確認"></userTask>
<boundaryEvent id="boundarysignal1" cancelActivity="true"
attachedToRef="usertask2">
<signalEventDefinition signalRef="contactChangeSignal"></signalEventDefinition>
</boundaryEvent>
<userTask id="usertask3" name="合同變更"></userTask>
<userTask id="usertask4" name="簽訂合同"></userTask>
<endEvent id="endevent1" name="End"></endEvent>
...
</process>


4、補償邊界事件
使用了取消結束事件和取消邊界事件,當事務子流程被取消時,會觸發事務子流程里面的補償邊界事件,這些補償邊界事件會依附在事務子流程的活動中,除了在事務子流程中可以使用取消事件來觸發補償邊界事件外,還可以使用補償中間事件來觸發補償邊界事件。

參考:邊界事件

五、中間事件
中間事件是指可以單獨作為流程元素的事件,BPMN2.0規范中所指的中間事件也包括邊界事件。中間事件作為流程元素表示對事件的捕獲與事件的觸發,一類中間事件可以在流程中等待被觸發,一類中間事件會在流程中自動被觸發並拋出結果(觸發信息)。
除了無指定Throwing中間事件外,其他事件均需要加入事件定義。

1、中間Catching事件
(1)定時器(Timer)中間事件
定時器中間事件一直等待被觸發,當達到定義的時間條件后,該定時器中間事件會被觸發,流程繼續往下執行。
<process id="tcProcess" name="tcProcess">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="發貨"></userTask>
<userTask id="usertask2" name="接收訂單"></userTask>
<intermediateCatchEvent id="timerintermediatecatchevent1"
name="TimerCatchEvent">
<timerEventDefinition>
<timeDuration>PT1M</timeDuration>
</timerEventDefinition>
</intermediateCatchEvent>
<endEvent id="endevent1" name="End"></endEvent>
...
</process>

(2)信號(Signal)中間事件
一個信號中間Catching事件會等待被觸發,直接到該事件接收到相應的信號,與其他事件不同的是,當信號事件接收到信號后,該信號不會被消耗掉,如果存在多個引用了相同信號的事件,那么當接收到信號時,這些事件被一並被觸發,即使它們不在同一個流程實例中.


(3)消息(Message)中間事件

<intermediateCatchEvent id="myIntermediateCatchEvent" >
<XXXEventDefinition/>
</intermediateCatchEvent>

2、中間Throwing事件:
(1)無指定(None)中間事件
(2)信號(Signal)中間事件
(3)補償(Compensation)中間事件

<intermediateThrowEvent id="myIntermediateThrowEvent" >
<XXXEventDefinition/>
</intermediateThrowEvent>

參考:中間事件 


六、補償中間事件

參考:補償中間事件

 

參考:瘋狂工作流講義(第2版)基於Activiti6.x 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM