Activiti接受任务(receiveTask)
阅读原文时间:2023年07月11日阅读:3

Activiti接受任务(receiveTask)

作者:Jesai

前言:

Activiti接受任务(receiveTask)其实和Activiti的手动任务是差不多的,不过手动任务是直接通过,而Activiti接受任务(receiveTask)则在任务启动到该节点的时候停下来等待信号。当任务接收到信号的时候,该流程就会继续往下执行。

官方解释:

描述
接收任务是一个等待消息到来的简单任务。目前,我们仅实现了该任务的 Java 语义。 当流程执行到接收任务时,流程状
态被提交到持久化数据库中。这意味着,流程将进入一种等待状态,直到引擎接收到明确的消息,来触发流程通过接收
任务继续执行。
图形化符号
接收任务被形象化成左上角带有消息图标的任务(椭圆)。消息是白色的(黑色的消息图标表示已经发送的意思)。
XML 表示

要继续当前等待在这样一个接收任务的流程实例,需要调用使用了执行到此接收任务的执行路径的 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());

流程设计:

流程图源码:

 <startEvent id="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" />

 <receiveTask id="receiveTask" name="接受任务">

   <extensionElements>

     <activiti:executionListener event="end" class="light.mvc.workflow.serviceTask.receiveTestRunningTask" />

   </extensionElements>

 </receiveTask>

 <sequenceFlow id="sid-46A38F6A-4413-48E2-ADAA-5DC86682086C" sourceRef="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" targetRef="receiveTask" />

 <endEvent id="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" />

 <sequenceFlow id="sid-2CCD7E71-70FE-418C-8365-5CD7636E9657" sourceRef="receiveTask" targetRef="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" />

 <bpmndi:BPMNPlane bpmnElement="receiveTaskprocess" id="BPMNPlane\_receiveTaskprocess">

   <bpmndi:BPMNShape bpmnElement="sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2" id="BPMNShape\_sid-4AF5399C-5241-4F58-BE58-6B2D0A1249D2">

     <omgdc:Bounds height="30.0" width="30.0" x="173.75" y="107.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="receiveTask" id="BPMNShape\_receiveTask">

     <omgdc:Bounds height="80.0" width="100.0" x="324.75" y="81.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="sid-156246B5-C81C-4AAB-A183-FCA197EC6901" id="BPMNShape\_sid-156246B5-C81C-4AAB-A183-FCA197EC6901">

     <omgdc:Bounds height="28.0" width="28.0" x="469.75" y="107.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNEdge bpmnElement="sid-46A38F6A-4413-48E2-ADAA-5DC86682086C" id="BPMNEdge\_sid-46A38F6A-4413-48E2-ADAA-5DC86682086C">

     <omgdi:waypoint x="203.7497832166316" y="121.91935600421166" />

     <omgdi:waypoint x="324.75" y="121.26881720430107" />

   </bpmndi:BPMNEdge>

   <bpmndi:BPMNEdge bpmnElement="sid-2CCD7E71-70FE-418C-8365-5CD7636E9657" id="BPMNEdge\_sid-2CCD7E71-70FE-418C-8365-5CD7636E9657">

     <omgdi:waypoint x="424.75" y="121.0" />

     <omgdi:waypoint x="469.75" y="121.0" />

   </bpmndi:BPMNEdge>

 </bpmndi:BPMNPlane>

给该流程设置一个监听器,用来监听流程是否执行:

/**

*

*/

package light.mvc.workflow.serviceTask;

import org.activiti.engine.delegate.DelegateExecution;

import org.activiti.engine.delegate.JavaDelegate;

/**

*

* 项目名称:lightmvc

* 类名称:ServiceTask

* 类描述:

* 创建人:邓家海

* 创建时间:2017年6月4日 下午6:18:11

* 修改人:deng

* 修改时间:2017年6月4日 下午6:18:11

* 修改备注:

* @version

*

*/

public class receiveTestRunningTask implements JavaDelegate{

//重写委托的提交方法

@Override

public void execute(DelegateExecution execution) throws Exception {

//receiveTaskprocess

System.out.println("receiveTestRunningTask is running!");

}

}

在这里,我们通过一个手动任务来启动这个接受任务。

手动任务流程设计:

手动任务源码:

 <startEvent id="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" />

 <manualTask id="sid-B57FA289-A98B-45B8-8550-211ED155778E" name="手工任务">

   <extensionElements>

     <activiti:executionListener event="start" class="light.mvc.workflow.serviceTask.TestRunningTask" />

   </extensionElements>

 </manualTask>

 <endEvent id="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />

 <sequenceFlow id="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" sourceRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" targetRef="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />

 <sequenceFlow id="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" sourceRef="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" targetRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" />

 <bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane\_process">

   <bpmndi:BPMNShape bpmnElement="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" id="BPMNShape\_sid-8D52A572-DD68-4640-BCEE-AA46FD19F535">

     <omgdc:Bounds height="30.0" width="30.0" x="233.75" y="102.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="sid-B57FA289-A98B-45B8-8550-211ED155778E" id="BPMNShape\_sid-B57FA289-A98B-45B8-8550-211ED155778E">

     <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="77.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" id="BPMNShape\_sid-DF898F06-5F35-4C64-B84D-6A405ADECA91">

     <omgdc:Bounds height="28.0" width="28.0" x="630.0" y="103.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNEdge bpmnElement="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" id="BPMNEdge\_sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834">

     <omgdi:waypoint x="475.0" y="117.0" />

     <omgdi:waypoint x="630.0" y="117.0" />

   </bpmndi:BPMNEdge>

   <bpmndi:BPMNEdge bpmnElement="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" id="BPMNEdge\_sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185">

     <omgdi:waypoint x="263.75" y="117.0" />

     <omgdi:waypoint x="375.0" y="117.0" />

   </bpmndi:BPMNEdge>

 </bpmndi:BPMNPlane>

 <startEvent id="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" />

 <manualTask id="sid-B57FA289-A98B-45B8-8550-211ED155778E" name="手工任务">

   <extensionElements>

     <activiti:executionListener event="start" class="light.mvc.workflow.serviceTask.TestRunningTask" />

   </extensionElements>

 </manualTask>

 <endEvent id="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />

 <sequenceFlow id="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" sourceRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" targetRef="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" />

 <sequenceFlow id="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" sourceRef="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" targetRef="sid-B57FA289-A98B-45B8-8550-211ED155778E" />

 <bpmndi:BPMNPlane bpmnElement="process" id="BPMNPlane\_process">

   <bpmndi:BPMNShape bpmnElement="sid-8D52A572-DD68-4640-BCEE-AA46FD19F535" id="BPMNShape\_sid-8D52A572-DD68-4640-BCEE-AA46FD19F535">

     <omgdc:Bounds height="30.0" width="30.0" x="233.75" y="102.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="sid-B57FA289-A98B-45B8-8550-211ED155778E" id="BPMNShape\_sid-B57FA289-A98B-45B8-8550-211ED155778E">

     <omgdc:Bounds height="80.0" width="100.0" x="375.0" y="77.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNShape bpmnElement="sid-DF898F06-5F35-4C64-B84D-6A405ADECA91" id="BPMNShape\_sid-DF898F06-5F35-4C64-B84D-6A405ADECA91">

     <omgdc:Bounds height="28.0" width="28.0" x="630.0" y="103.0" />

   </bpmndi:BPMNShape>

   <bpmndi:BPMNEdge bpmnElement="sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834" id="BPMNEdge\_sid-BD48A7F5-D206-4E76-AB78-0437FBDC5834">

     <omgdi:waypoint x="475.0" y="117.0" />

     <omgdi:waypoint x="630.0" y="117.0" />

   </bpmndi:BPMNEdge>

   <bpmndi:BPMNEdge bpmnElement="sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185" id="BPMNEdge\_sid-6F9CC662-16AB-4058-BA4A-2A0D3D99C185">

     <omgdi:waypoint x="263.75" y="117.0" />

     <omgdi:waypoint x="375.0" y="117.0" />

   </bpmndi:BPMNEdge>

 </bpmndi:BPMNPlane>

给该流程设置一个监听器,用来通知接受任务继续往下执行。

/**

*

*/

package light.mvc.workflow.serviceTask;

import java.util.List;

import org.activiti.engine.delegate.DelegateExecution;

import org.activiti.engine.delegate.JavaDelegate;

import org.activiti.engine.runtime.Execution;

/**

*

* 项目名称:lightmvc

* 类名称:ServiceTask

* 类描述:

* 创建人:邓家海

* 创建时间:2017年6月4日 下午6:18:11

* 修改人:deng

* 修改时间:2017年6月4日 下午6:18:11

* 修改备注:

* @version

*

*/

public class TestRunningTask implements JavaDelegate{

//重写委托的提交方法

@Override

public void execute(DelegateExecution execution) throws Exception {

//receiveTaskprocess

List executions= execution.getEngineServices().getRuntimeService().createExecutionQuery().processDefinitionKey("receiveTaskprocess")

.activityId("receiveTask").list();

for(Execution e:executions){

execution.getEngineServices().getRuntimeService().signal(e.getId());

}

System.out.println("TestRunningTask is running!");

}

}

1)首先部署启动接受任务,这个时候,接受任务处于等待的状态

2)然后启动手动任务,手动任务执行,触发监听类,监听类通知接受任务执行,手动任务结束

3)接受任务收到信号,继续往下执行,结束

4)整个过程结束。

运行结果:

可以看到,两个监听器都完成了。下面我们可以用一个图示来解释这整个过程:

Activiti交流QQ群:634320089

手机扫一扫

移动阅读更方便

阿里云服务器
腾讯云服务器
七牛云服务器

你可能感兴趣的文章