1. 程式人生 > >Activiti接受任務(receiveTask)

Activiti接受任務(receiveTask)

image eve 持久化數據 wait 手動 手工 修改時間 workflow otn

Activiti接受任務(receiveTask

作者:鄧家海

前言:

Activiti接受任務(receiveTask)其實和Activiti的手動任務是差不多的,不過手動任務是直接通過,而Activiti接受任務(receiveTask)則在任務啟動到該節點的時候停下來等待信號。當任務接收到信號的時候,該流程就會繼續往下執行。

官方解釋:

描述
接收任務是一個等待消息到來的簡單任務。目前,我們僅實現了該任務的 Java 語義。 當流程執行到接收任務時,流程狀
態被提交到持久化數據庫中。這意味著,流程將進入一種等待狀態,直到引擎接收到明確的消息,來觸發流程通過接收
任務繼續執行。
圖形化符號
接收任務被形象化成左上角帶有消息圖標的任務(橢圓)。消息是白色的(黑色的消息圖標表示已經發送的意思)。


XML 表示
<receiveTaskid="waitState"name="wait"/>
要繼續當前等待在這樣一個接收任務的流程實例,需要調用使用了執行到此接收任務的執行路徑的 id 的方法
runtimeService.signal(executionId)。下面的代碼片段展示實際是如何操作的:
ProcessInstance pi = runtimeService.startProcessInstanceByKey("receiveTask");
Execution execution = runtimeService.createExecutionQuery()
.processInstanceId(pi.getId())
.activityId("wait")
.singleResult();
assertNotNull(execution);
runtimeService.signal(execution.getId());

流程設計:

技術分享

流程圖源碼:

 1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
 2 
 3 <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:activiti="http://activiti.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.activiti.org/processdef"> 4 5 <process id="receiveTaskprocess" isExecutable="true"> 6 7 <startEvent id="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" /> 8 9 <receiveTask id="receiveTask" name="接受任務"> 10 11 <extensionElements> 12 13 <activiti:executionListener event="end" class="light.mvc.workflow.serviceTask.receiveTestRunningTask" /> 14 15 </extensionElements> 16 17 </receiveTask> 18 19 <sequenceFlow id="sid-46A38F6A-4413-48E2-ADAA-5DC86682086C" sourceRef="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" targetRef="receiveTask" /> 20 21 <endEvent id="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" /> 22 23 <sequenceFlow id="sid-2CCD7E71-70FE-418C-8365-5CD7636E9657" sourceRef="receiveTask" targetRef="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" /> 24 25 </process> 26 27 <bpmndi:BPMNDiagram id="BPMNDiagram_receiveTaskprocess"> 28 29 <bpmndi:BPMNPlane bpmnElement="receiveTaskprocess" id="BPMNPlane_receiveTaskprocess"> 30 31 <bpmndi:BPMNShape bpmnElement="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" id="BPMNShape_sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2"> 32 33 <omgdc:Bounds height="30.0" width="30.0" x="173.75" y="107.0" /> 34 35 </bpmndi:BPMNShape> 36 37 <bpmndi:BPMNShape bpmnElement="receiveTask" id="BPMNShape_receiveTask"> 38 39 <omgdc:Bounds height="80.0" width="100.0" x="324.75" y="81.0" /> 40 41 </bpmndi:BPMNShape> 42 43 <bpmndi:BPMNShape bpmnElement="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" id="BPMNShape_sid-156246B5-C81C-4AAB-A183-FCA197EC6901"> 44 45 <omgdc:Bounds height="28.0" width="28.0" x="469.75" y="107.0" /> 46 47 </bpmndi:BPMNShape> 48 49 <bpmndi:BPMNEdge bpmnElement="sid-46A38F6A-4413-48E2-ADAA-5DC86682086C" id="BPMNEdge_sid-46A38F6A-4413-48E2-ADAA-5DC86682086C"> 50 51 <omgdi:waypoint x="203.7497832166316" y="121.91935600421166" /> 52 53 <omgdi:waypoint x="324.75" y="121.26881720430107" /> 54 55 </bpmndi:BPMNEdge> 56 57 <bpmndi:BPMNEdge bpmnElement="sid-2CCD7E71-70FE-418C-8365-5CD7636E9657" id="BPMNEdge_sid-2CCD7E71-70FE-418C-8365-5CD7636E9657"> 58 59 <omgdi:waypoint x="424.75" y="121.0" /> 60 61 <omgdi:waypoint x="469.75" y="121.0" /> 62 63 </bpmndi:BPMNEdge> 64 65 </bpmndi:BPMNPlane> 66 67 </bpmndi:BPMNDiagram> 68 69 </definitions>

給該流程設置一個監聽器,用來監聽流程是否執行:

 1 /**
 2 
 3  *
 4 
 5  */
 6 
 7 package light.mvc.workflow.serviceTask;
 8 
 9  
10 
11 import org.activiti.engine.delegate.DelegateExecution;
12 
13 import org.activiti.engine.delegate.JavaDelegate;
14 
15  
16 
17 /**  
18 
19  *   
20 
21  * 項目名稱:lightmvc  
22 
23  * 類名稱:ServiceTask  
24 
25  * 類描述:  
26 
27  * 創建人:鄧家海  
28 
29  * 創建時間:2017年6月4日 下午6:18:11  
30 
31  * 修改人:deng  
32 
33  * 修改時間:2017年6月4日 下午6:18:11  
34 
35  * 修改備註:  
36 
37  * @version   
38 
39  *   
40 
41  */
42 
43  
44 
45 public class receiveTestRunningTask implements JavaDelegate{
46 
47  
48 
49 //重寫委托的提交方法
50 
51 @Override
52 
53 public void execute(DelegateExecution execution) throws Exception {
54 
55 //receiveTaskprocess
56 
57 System.out.println("receiveTestRunningTask is running!");
58 
59 }
60 
61  
62 
63 }

在這裏,我們通過一個手動任務來啟動這個接受任務。

手動任務流程設計:

技術分享

手動任務源碼:

 1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
 2 
 3 <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:activiti="http://activiti.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.activiti.org/processdef">
 4 
 5   <process id="process" isExecutable="true">
 6 
 7     <startEvent id="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" />
 8 
 9     <manualTask id="sid-B57FA289-A98B-45B8-8550-211ED155778E" name="手工任務">
10 
11       <extensionElements>
12 
13         <activiti:executionListener event="start" class="light.mvc.workflow.serviceTask.TestRunningTask" />
14 
15       </extensionElements>
16 
17     </manualTask>
18 
19     <endEvent id="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />
20 
21     <sequenceFlow id="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" sourceRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" targetRef="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />
22 
23     <sequenceFlow id="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" sourceRef="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" targetRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" />
24 
25   </process>
26 
27   <bpmndi:BPMNDiagram id="BPMNDiagram_process">
28 
29     <bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_process">
30 
31       <bpmndi:BPMNShape bpmnElement="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" id="BPMNShape_sid-8D52A572-DD68-4640-BCEE-AA46FD19F535">
32 
33         <omgdc:Bounds height="30.0" width="30.0" x="233.75" y="102.0" />
34 
35       </bpmndi:BPMNShape>
36 
37       <bpmndi:BPMNShape bpmnElement="sid-B57FA289-A98B-45B8-8550-211ED155778E" id="BPMNShape_sid-B57FA289-A98B-45B8-8550-211ED155778E">
38 
39         <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="77.0" />
40 
41       </bpmndi:BPMNShape>
42 
43       <bpmndi:BPMNShape bpmnElement="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" id="BPMNShape_sid-DF898F06-5F35-4C64-B84D-6A405ADECA91">
44 
45         <omgdc:Bounds height="28.0" width="28.0" x="630.0" y="103.0" />
46 
47       </bpmndi:BPMNShape>
48 
49       <bpmndi:BPMNEdge bpmnElement="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" id="BPMNEdge_sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834">
50 
51         <omgdi:waypoint x="475.0" y="117.0" />
52 
53         <omgdi:waypoint x="630.0" y="117.0" />
54 
55       </bpmndi:BPMNEdge>
56 
57       <bpmndi:BPMNEdge bpmnElement="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" id="BPMNEdge_sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185">
58 
59         <omgdi:waypoint x="263.75" y="117.0" />
60 
61         <omgdi:waypoint x="375.0" y="117.0" />
62 
63       </bpmndi:BPMNEdge>
64 
65     </bpmndi:BPMNPlane>
66 
67   </bpmndi:BPMNDiagram>
68 
69 </definitions>
 1 <?xml version=‘1.0‘ encoding=‘UTF-8‘?>
 2 
 3 <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:activiti="http://activiti.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.activiti.org/processdef">
 4 
 5   <process id="process" isExecutable="true">
 6 
 7     <startEvent id="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" />
 8 
 9     <manualTask id="sid-B57FA289-A98B-45B8-8550-211ED155778E" name="手工任務">
10 
11       <extensionElements>
12 
13         <activiti:executionListener event="start" class="light.mvc.workflow.serviceTask.TestRunningTask" />
14 
15       </extensionElements>
16 
17     </manualTask>
18 
19     <endEvent id="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />
20 
21     <sequenceFlow id="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" sourceRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" targetRef="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />
22 
23     <sequenceFlow id="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" sourceRef="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" targetRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" />
24 
25   </process>
26 
27   <bpmndi:BPMNDiagram id="BPMNDiagram_process">
28 
29     <bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane_process">
30 
31       <bpmndi:BPMNShape bpmnElement="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" id="BPMNShape_sid-8D52A572-DD68-4640-BCEE-AA46FD19F535">
32 
33         <omgdc:Bounds height="30.0" width="30.0" x="233.75" y="102.0" />
34 
35       </bpmndi:BPMNShape>
36 
37       <bpmndi:BPMNShape bpmnElement="sid-B57FA289-A98B-45B8-8550-211ED155778E" id="BPMNShape_sid-B57FA289-A98B-45B8-8550-211ED155778E">
38 
39         <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="77.0" />
40 
41       </bpmndi:BPMNShape>
42 
43       <bpmndi:BPMNShape bpmnElement="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" id="BPMNShape_sid-DF898F06-5F35-4C64-B84D-6A405ADECA91">
44 
45         <omgdc:Bounds height="28.0" width="28.0" x="630.0" y="103.0" />
46 
47       </bpmndi:BPMNShape>
48 
49       <bpmndi:BPMNEdge bpmnElement="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" id="BPMNEdge_sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834">
50 
51         <omgdi:waypoint x="475.0" y="117.0" />
52 
53         <omgdi:waypoint x="630.0" y="117.0" />
54 
55       </bpmndi:BPMNEdge>
56 
57       <bpmndi:BPMNEdge bpmnElement="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" id="BPMNEdge_sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185">
58 
59         <omgdi:waypoint x="263.75" y="117.0" />
60 
61         <omgdi:waypoint x="375.0" y="117.0" />
62 
63       </bpmndi:BPMNEdge>
64 
65     </bpmndi:BPMNPlane>
66 
67   </bpmndi:BPMNDiagram>
68 
69 </definitions>

給該流程設置一個監聽器,用來通知接受任務繼續往下執行。

 1 /**
 2 
 3  *
 4 
 5  */
 6 
 7 package light.mvc.workflow.serviceTask;
 8 
 9  
10 
11 import java.util.List;
12 
13  
14 
15 import org.activiti.engine.delegate.DelegateExecution;
16 
17 import org.activiti.engine.delegate.JavaDelegate;
18 
19 import org.activiti.engine.runtime.Execution;
20 
21  
22 
23 /**  
24 
25  *   
26 
27  * 項目名稱:lightmvc  
28 
29  * 類名稱:ServiceTask  
30 
31  * 類描述:  
32 
33  * 創建人:鄧家海  
34 
35  * 創建時間:2017年6月4日 下午6:18:11  
36 
37  * 修改人:deng  
38 
39  * 修改時間:2017年6月4日 下午6:18:11  
40 
41  * 修改備註:  
42 
43  * @version   
44 
45  *   
46 
47  */
48 
49  
50 
51 public class TestRunningTask implements JavaDelegate{
52 
53  
54 
55 //重寫委托的提交方法
56 
57 @Override
58 
59 public void execute(DelegateExecution execution) throws Exception {
60 
61 //receiveTaskprocess
62 
63 List<Execution> executions= execution.getEngineServices().getRuntimeService().createExecutionQuery().processDefinitionKey("receiveTaskprocess")
64 
65 .activityId("receiveTask").list();
66 
67 for(Execution e:executions){
68 
69 execution.getEngineServices().getRuntimeService().signal(e.getId());
70 
71 }
72 
73  
74 
75 System.out.println("TestRunningTask is running!");
76 
77 }
78 
79  
80 
81 }

1)首先部署啟動接受任務,這個時候,接受任務處於等待的狀態

2)然後啟動手動任務,手動任務執行,觸發監聽類,監聽類通知接受任務執行,手動任務結束

3)接受任務收到信號,繼續往下執行,結束

4)整個過程結束。

運行結果:

技術分享

可以看到,兩個監聽器都完成了。下面我們可以用一個圖示來解釋這整個過程:

技術分享

Activiti接受任務(receiveTask)