首页 > 代码库 > Activiti接受任务(receiveTask)

Activiti接受任务(receiveTask)

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)