接收任务(ReceiveTask)即等待任务,接收任务是一个简单任务,它会等待对应消息的到达。当前,官方只实现
了这个任务的java语义。 当流程达到接收任务,流程状态会保存到数据库中。在任务创建后,意味着流程会进入等
待状态,直到引擎接收了一个特定的消息, 这会触发流程穿过接收任务继续执行。
一、流程图
![](https://img-blog.csdn.net/20151223110826934)
图一 等待任务流程图
在这里,我们使用每日向老板汇总当日销售额为例,来演示一下等待任务。
二、流程演示
- package cn.gome.j_receiveTask;
-
- import java.io.InputStream;
-
- import org.activiti.engine.ProcessEngine;
- import org.activiti.engine.ProcessEngines;
- import org.activiti.engine.repository.Deployment;
- import org.activiti.engine.runtime.Execution;
- import org.activiti.engine.runtime.ProcessInstance;
- import org.junit.Test;
-
- public class ReceiveTaskTest {
-
-
- ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
-
-
- @Test
- public void deployementAndStartProcess(){
- InputStream inputStreamBpmn = this.getClass().getResourceAsStream("receiveTask.bpmn");
- InputStream inputStreamPng = this.getClass().getResourceAsStream("receiveTask.png");
-
-
- Deployment deployment = processEngine.getRepositoryService()
- .createDeployment()
- .addInputStream("receiveTask.bpmn", inputStreamBpmn)
- .addInputStream("receiveTask.png", inputStreamPng)
- .deploy();
- System.out.println("部署ID:" + deployment.getId());
-
-
- ProcessInstance pi = processEngine.getRuntimeService()
- .startProcessInstanceByKey("receiveTask");
- System.out.println("流程实例ID:"+pi.getId());
- System.out.println("流程定义ID:"+pi.getProcessDefinitionId());
-
-
- String processInstanceId = pi.getId();
- Execution execution1 = processEngine.getRuntimeService()
- .createExecutionQuery()
- .processInstanceId(processInstanceId)
- .activityId("receivetask1")
- .singleResult();
-
-
- processEngine.getRuntimeService().setVariable(execution1.getId(), "当日销售额", 20000);
-
-
- processEngine.getRuntimeService()
- .signal(execution1.getId());
-
-
- Execution execution2 = processEngine.getRuntimeService()
- .createExecutionQuery()
- .processInstanceId(processInstanceId)
- .activityId("receivetask2")
- .singleResult();
-
-
- Integer value = (Integer) processEngine.getRuntimeService()
- .getVariable(execution2.getId(), "当日销售额");
- System.out.println("给老板发送短信:内容,当日销售额:"+value);
-
-
- processEngine.getRuntimeService()
- .signal(execution2.getId());
-
-
- ProcessInstance nowPi = processEngine.getRuntimeService()
- .createProcessInstanceQuery()
- .processInstanceId(pi.getId())
- .singleResult();
- if(nowPi == null){
- System.out.println("流程结束");
- }
- }
-
-
- }
说明:与用户任务(UserTask)不同的是,
接收任务(
ReceiveTask)创建后,会进入一个等待状态,
一般指机器自动完成,但需要耗费一定时间的工作,当完成工作后后,向后推移流程,在上面的代码中也就是向下执行一步,使用runtimeService.signal(executionId),传递接收执行对象的id。
三、数据库表分析
1.流程定义表:act_re_procdef
![](https://img-blog.csdn.net/20151223111831162)
2.执行对象表:act_ru_execution:receiveTask1
![](https://img-blog.csdn.net/20151223112444321)
3.流程变量表:act_ru_variable
![](https://img-blog.csdn.net/20151223112804354)
4.执行对象表:act_ru_execution:receiveTask2
![](https://img-blog.csdn.net/20151223113013800)
总结:
简单介绍了一下等待任务,和我们之前用户任务不太一样的是,在等待任务执行的时候,act_ru_task表中是没有数据的,只有任务节点(UserTask),该表中才有数据。