Activiti 接收任务活动
流程中往往需要特定人接受任务并进行一定操作才能继续进行下去。
代码如下
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(); /**部署流程定义(从inputStream)*/ @Test public void deploymentProcessDefinition_inputStream(){ InputStream inputStreamBpmn = this.getClass().getResourceAsStream("receiveTask.bpmn"); InputStream inputStreamPng = this.getClass().getResourceAsStream("receiveTask.png"); Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service .createDeployment()//创建一个部署对象 .name("接收活动任务")//添加部署的名称 .addInputStream("receiveTask.bpmn", inputStreamBpmn)// .addInputStream("receiveTask.png", inputStreamPng)// .deploy();//完成部署 System.out.println("部署ID:"+deployment.getId());// System.out.println("部署名称:"+deployment.getName());// } /**启动流程实例+设置流程变量+获取流程变量+向后执行一步*/ @Test public void startProcessInstance(){ //流程定义的key String processDefinitionKey = "receiveTask"; ProcessInstance pi = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service .startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,key对应helloworld.bpmn文件中id的属性值,使用key值启动,默认是按照最新版本的流程定义启动 System.out.println("流程实例ID:"+pi.getId());//流程实例ID 101 System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//流程定义ID helloworld:1:4 /**查询执行对象ID*/ Execution execution1 = processEngine.getRuntimeService()// .createExecutionQuery()//创建执行对象查询 .processInstanceId(pi.getId())//使用流程实例ID查询 .activityId("receivetask1")//当前活动的id,对应receiveTask.bpmn文件中的活动节点id的属性值 .singleResult(); /**使用流程变量设置当日销售额**/ processEngine.getRuntimeService().setVariable(execution1.getId(), "汇总当日销售额", 21000); /**向后执行一步,如果流程处于等待状态,使得流程继续执行*/ processEngine.getRuntimeService() .signal(execution1.getId()); /**查询执行对象ID*/ Execution execution2 = processEngine.getRuntimeService()// .createExecutionQuery()//创建执行对象查询 .processInstanceId(pi.getId())//使用流程实例ID查询 .activityId("receivetask2")//当前活动的id,对应receiveTask.bpmn文件中的活动节点id的属性值 .singleResult(); /**从流程变量中获取汇总当日销售额的值*/ Integer value = (Integer)processEngine.getRuntimeService()// .getVariable(execution2.getId(), "汇总当日销售额"); System.out.println("给老板发送短信:金额是:"+value); /**向后执行一步,如果流程处于等待状态,使得流程继续执行*/ processEngine.getRuntimeService() .signal(execution2.getId()); } }
由于没有任务id,因此只能由流程进行操作,所以使用getRuntimeService()方法。先由流程实例编号查询流程1,并进行汇总销售操作然后进行到下一个流程。同样,由流程实例编号查询流程2,汇报给老板并进行到下一步完成整个流程
bpnm文件如下
<?xml version="1.0" encoding="UTF-8"?> <definitions xmlns="http://www.omg.org/spec/BPMN/20100524/MODEL" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 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/test"> <process id="receiveTask" name="receiveTaskProcess" isExecutable="true"> <startEvent id="startevent1" name="Start"></startEvent> <receiveTask id="receivetask1" name="汇总当日销售额"></receiveTask> <receiveTask id="receivetask2" name="给老板发送信息"></receiveTask> <endEvent id="endevent1" name="End"></endEvent> <sequenceFlow id="flow1" sourceRef="receivetask2" targetRef="endevent1"></sequenceFlow> <sequenceFlow id="flow2" sourceRef="startevent1" targetRef="receivetask1"></sequenceFlow> <sequenceFlow id="flow3" sourceRef="receivetask1" targetRef="receivetask2"></sequenceFlow> </process> <bpmndi:BPMNDiagram id="BPMNDiagram_receiveTask"> <bpmndi:BPMNPlane bpmnElement="receiveTask" id="BPMNPlane_receiveTask"> <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> <omgdc:Bounds height="35.0" width="35.0" x="360.0" y="20.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="receivetask1" id="BPMNShape_receivetask1"> <omgdc:Bounds height="55.0" width="105.0" x="325.0" y="120.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="receivetask2" id="BPMNShape_receivetask2"> <omgdc:Bounds height="55.0" width="105.0" x="325.0" y="250.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1"> <omgdc:Bounds height="35.0" width="35.0" x="360.0" y="360.0"></omgdc:Bounds> </bpmndi:BPMNShape> <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> <omgdi:waypoint x="377.0" y="305.0"></omgdi:waypoint> <omgdi:waypoint x="377.0" y="360.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2"> <omgdi:waypoint x="377.0" y="55.0"></omgdi:waypoint> <omgdi:waypoint x="377.0" y="120.0"></omgdi:waypoint> </bpmndi:BPMNEdge> <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> <omgdi:waypoint x="377.0" y="175.0"></omgdi:waypoint> <omgdi:waypoint x="377.0" y="250.0"></omgdi:waypoint> </bpmndi:BPMNEdge> </bpmndi:BPMNPlane> </bpmndi:BPMNDiagram> </definitions>