连线:
新建bpmn文件:
<?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="sequenceFlow" name="sequenceFlowProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="审批【部门经理】" activiti:assignee="赵六"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="审批【总经理】" activiti:assignee="田七"></userTask>
<sequenceFlow id="flow2" name="重要" sourceRef="usertask1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${message=='重要'}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow3" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow4" name="不重要" sourceRef="usertask1" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${message=='不重要'}]]></conditionExpression>
</sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_sequenceFlow">
<bpmndi:BPMNPlane bpmnElement="sequenceFlow" id="BPMNPlane_sequenceFlow">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="460.0" y="240.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="320.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="85.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="140.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="357.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="512.0" y="240.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="24.0" x="6.0" y="-17.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="512.0" y="295.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="320.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="357.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="320.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="36.0" x="10.0" y="0.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
注意:如果将流程图放置在和java类相同的路径,需要配置:
public class SequenceFlowTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**部署流程定义(从inputStream)*/
@Test
public void deploymentProcessDefinition_inputStream(){
InputStream inputStreamBpmn = this.getClass().getResourceAsStream("sequenceFlow.bpmn");
InputStream inputStreamPng = this.getClass().getResourceAsStream("sequenceFlow.png");
Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
.createDeployment()//创建一个部署对象
.name("连线")//添加部署的名称
.addInputStream("sequenceFlow.bpmn", inputStreamBpmn)//
.addInputStream("sequenceFlow.png", inputStreamPng)//
.deploy();//完成部署
System.out.println("部署ID:"+deployment.getId());//
System.out.println("部署名称:"+deployment.getName());//
}
/**启动流程实例*/
@Test
public void startProcessInstance(){
//流程定义的key
String processDefinitionKey = "sequenceFlow";
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
}
/**查询当前人的个人任务*/
@Test
public void findMyPersonalTask(){
String assignee = "赵六";
List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
.createTaskQuery()//创建任务查询对象
/**查询条件(where部分)*/
.taskAssignee(assignee)//指定个人任务查询,指定办理人
// .taskCandidateUser(candidateUser)//组任务的办理人查询
// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
// .processInstanceId(processInstanceId)//使用流程实例ID查询
// .executionId(executionId)//使用执行对象ID查询
/**排序*/
.orderByTaskCreateTime().asc()//使用创建时间的升序排列
/**返回结果集*/
// .singleResult()//返回惟一结果集
// .count()//返回结果集的数量
// .listPage(firstResult, maxResults);//分页查询
.list();//返回列表
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("########################################################");
}
}
}
/**完成我的任务*/
@Test
public void completeMyPersonalTask(){
//任务ID
String taskId = "3103";
//完成任务的同时,设置流程变量,使用流程变量用来指定完成任务后,下一个连线,对应sequenceFlow.bpmn文件中${message=='不重要'}
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("message", "重要");
processEngine.getTaskService()//与正在执行的任务管理相关的Service
.complete(taskId,variables);
System.out.println("完成任务:任务ID:"+taskId);
}
}
说明:
1、一个活动中可以指定一个或多个SequenceFlow(Start中有一个,End中没有)。
* 开始活动中有一个SequenceFlow 。
* 结束活动中没有SequenceFlow 。
* 其他活动中有1条或多条SequenceFlow
2、如果只有一个,则可以不使用流程变量设置codition的名称;
如果有多个,则需要使用流程变量设置codition的名称。message表示流程变量的名称,‘不重要’表示流程变量的值,${}或#{}中间的内容要使用boolean类型的表达式,用来判断应该执行的连线。
排他网关:
新建bpmn文件
<?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="exclusiveGateWay" name="exclusiveGateWayProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="费用报销申请" activiti:assignee="王小五"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<userTask id="usertask2" name="审批【部门经理】" activiti:assignee="赵小六"></userTask>
<userTask id="usertask3" name="财务" activiti:assignee="胡小八"></userTask>
<userTask id="usertask4" name="审批【总经理】" activiti:assignee="田小七"></userTask>
<exclusiveGateway id="exclusivegateway1" name="Exclusive Gateway" default="默认执行财务"></exclusiveGateway>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="exclusivegateway1"></sequenceFlow>
<sequenceFlow id="flow3" name="金额小于等于1000,大于等于500" sourceRef="exclusivegateway1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money>=500 && money<=1000}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="默认执行财务" name="默认执行财务" sourceRef="exclusivegateway1" targetRef="usertask3"></sequenceFlow>
<sequenceFlow id="flow5" name="金额大于1000元" sourceRef="exclusivegateway1" targetRef="usertask4">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${money>1000}]]></conditionExpression>
</sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow6" sourceRef="usertask2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask3" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow8" sourceRef="usertask4" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_exclusiveGateWay">
<bpmndi:BPMNPlane bpmnElement="exclusiveGateWay" id="BPMNPlane_exclusiveGateWay">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="140.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="80.0" y="390.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="560.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="390.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
<omgdc:Bounds height="55.0" width="105.0" x="550.0" y="380.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="exclusivegateway1" id="BPMNShape_exclusivegateway1">
<omgdc:Bounds height="40.0" width="40.0" x="337.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="85.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="140.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="357.0" y="195.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="260.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="132.0" y="390.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="42.0" width="100.0" x="-117.0" y="-35.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="602.0" y="380.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="84.0" x="16.0" y="-15.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="132.0" y="445.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="560.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="357.0" y="445.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="560.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="602.0" y="435.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="560.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="默认执行财务" id="BPMNEdge_默认执行财务">
<omgdi:waypoint x="357.0" y="300.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="390.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="72.0" x="10.0" y="0.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
public class ExclusiveGateWayTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**部署流程定义(从inputStream)*/
@Test
public void deploymentProcessDefinition_inputStream(){
InputStream inputStreamBpmn = this.getClass().getResourceAsStream("exclusiveGateWay.bpmn");
InputStream inputStreamPng = this.getClass().getResourceAsStream("exclusiveGateWay.png");
Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
.createDeployment()//创建一个部署对象
.name("排他网关")//添加部署的名称
.addInputStream("exclusiveGateWay.bpmn", inputStreamBpmn)//
.addInputStream("exclusiveGateWay.png", inputStreamPng)//
.deploy();//完成部署
System.out.println("部署ID:"+deployment.getId());//
System.out.println("部署名称:"+deployment.getName());//
}
/**启动流程实例*/
@Test
public void startProcessInstance(){
//流程定义的key
String processDefinitionKey = "exclusiveGateWay";
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
}
/**查询当前人的个人任务*/
@Test
public void findMyPersonalTask(){
String assignee = "王小五";
List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
.createTaskQuery()//创建任务查询对象
/**查询条件(where部分)*/
.taskAssignee(assignee)//指定个人任务查询,指定办理人
// .taskCandidateUser(candidateUser)//组任务的办理人查询
// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
// .processInstanceId(processInstanceId)//使用流程实例ID查询
// .executionId(executionId)//使用执行对象ID查询
/**排序*/
.orderByTaskCreateTime().asc()//使用创建时间的升序排列
/**返回结果集*/
// .singleResult()//返回惟一结果集
// .count()//返回结果集的数量
// .listPage(firstResult, maxResults);//分页查询
.list();//返回列表
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("########################################################");
}
}
}
/**完成我的任务*/
@Test
public void completeMyPersonalTask(){
//任务ID
String taskId = "3904";
//完成任务的同时,设置流程变量,使用流程变量用来指定完成任务后,下一个连线,对应exclusiveGateWay.bpmn文件中${money>1000}
Map<String, Object> variables = new HashMap<String, Object>();
variables.put("money", 200);
processEngine.getTaskService()//与正在执行的任务管理相关的Service
.complete(taskId,variables);
System.out.println("完成任务:任务ID:"+taskId);
}
}
说明:
- 一个排他网关对应一个以上的顺序流
- 由排他网关流出的顺序流都有个conditionExpression元素,在内部维护返回boolean类型的决策结果。
- 决策网关只会返回一条结果。当流程执行到排他网关时,流程引擎会自动检索网关出口,从上到下检索如果发现第一条决策结果为true或者没有设置条件的(默认为成立),则流出。
- 如果没有任何一个出口符合条件,则抛出异常
- 使用流程变量,设置连线的条件,并按照连线的条件执行工作流,如果没有条件符合的条件,则以默认的连线离开。例如:
则执行连线:
如果使用流程变量设置
则执行连线:
并行网关:
新建bpmn文件:
<?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="parallelGateWay" name="parallelGateWayProcess" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="usertask1" name="付款" activiti:assignee="买家"></userTask>
<userTask id="usertask2" name="发货" activiti:assignee="商家"></userTask>
<userTask id="usertask3" name="收款" activiti:assignee="商家"></userTask>
<userTask id="usertask4" name="收货" activiti:assignee="买家"></userTask>
<parallelGateway id="parallelgateway1" name="Parallel Gateway"></parallelGateway>
<parallelGateway id="parallelgateway2" name="Parallel Gateway"></parallelGateway>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="parallelgateway1"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="parallelgateway1" targetRef="usertask1"></sequenceFlow>
<sequenceFlow id="flow3" sourceRef="usertask1" targetRef="usertask3"></sequenceFlow>
<sequenceFlow id="flow4" sourceRef="usertask3" targetRef="parallelgateway2"></sequenceFlow>
<sequenceFlow id="flow5" sourceRef="parallelgateway2" targetRef="endevent1"></sequenceFlow>
<sequenceFlow id="flow6" sourceRef="parallelgateway1" targetRef="usertask2"></sequenceFlow>
<sequenceFlow id="flow7" sourceRef="usertask2" targetRef="usertask4"></sequenceFlow>
<sequenceFlow id="flow8" sourceRef="usertask4" targetRef="parallelgateway2"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_parallelGateWay">
<bpmndi:BPMNPlane bpmnElement="parallelGateWay" id="BPMNPlane_parallelGateWay">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="323.0" y="20.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="150.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="450.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="330.0" y="460.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask3" id="BPMNShape_usertask3">
<omgdc:Bounds height="55.0" width="105.0" x="150.0" y="280.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask4" id="BPMNShape_usertask4">
<omgdc:Bounds height="55.0" width="105.0" x="450.0" y="280.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="parallelgateway1" id="BPMNShape_parallelgateway1">
<omgdc:Bounds height="40.0" width="40.0" x="320.0" y="100.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="parallelgateway2" id="BPMNShape_parallelgateway2">
<omgdc:Bounds height="40.0" width="40.0" x="330.0" y="380.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="340.0" y="55.0"></omgdi:waypoint>
<omgdi:waypoint x="340.0" y="100.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="340.0" y="140.0"></omgdi:waypoint>
<omgdi:waypoint x="202.0" y="180.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="202.0" y="235.0"></omgdi:waypoint>
<omgdi:waypoint x="202.0" y="280.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="202.0" y="335.0"></omgdi:waypoint>
<omgdi:waypoint x="350.0" y="380.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow5" id="BPMNEdge_flow5">
<omgdi:waypoint x="350.0" y="420.0"></omgdi:waypoint>
<omgdi:waypoint x="347.0" y="460.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow6" id="BPMNEdge_flow6">
<omgdi:waypoint x="340.0" y="140.0"></omgdi:waypoint>
<omgdi:waypoint x="502.0" y="180.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow7" id="BPMNEdge_flow7">
<omgdi:waypoint x="502.0" y="235.0"></omgdi:waypoint>
<omgdi:waypoint x="502.0" y="280.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow8" id="BPMNEdge_flow8">
<omgdi:waypoint x="502.0" y="335.0"></omgdi:waypoint>
<omgdi:waypoint x="350.0" y="380.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
public class ParallelGateWayTest {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**部署流程定义(从inputStream)*/
@Test
public void deploymentProcessDefinition_inputStream(){
InputStream inputStreamBpmn = this.getClass().getResourceAsStream("parallelGateWay.bpmn");
InputStream inputStreamPng = this.getClass().getResourceAsStream("parallelGateWay.png");
Deployment deployment = processEngine.getRepositoryService()//与流程定义和部署对象相关的Service
.createDeployment()//创建一个部署对象
.name("并行网关")//添加部署的名称
.addInputStream("parallelGateWay.bpmn", inputStreamBpmn)//
.addInputStream("parallelGateWay.png", inputStreamPng)//
.deploy();//完成部署
System.out.println("部署ID:"+deployment.getId());//
System.out.println("部署名称:"+deployment.getName());//
}
/**启动流程实例*/
@Test
public void startProcessInstance(){
//流程定义的key
String processDefinitionKey = "parallelGateWay";
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
}
/**查询当前人的个人任务*/
@Test
public void findMyPersonalTask(){
String assignee = "商家";
List<Task> list = processEngine.getTaskService()//与正在执行的任务管理相关的Service
.createTaskQuery()//创建任务查询对象
/**查询条件(where部分)*/
.taskAssignee(assignee)//指定个人任务查询,指定办理人
// .taskCandidateUser(candidateUser)//组任务的办理人查询
// .processDefinitionId(processDefinitionId)//使用流程定义ID查询
// .processInstanceId(processInstanceId)//使用流程实例ID查询
// .executionId(executionId)//使用执行对象ID查询
/**排序*/
.orderByTaskCreateTime().asc()//使用创建时间的升序排列
/**返回结果集*/
// .singleResult()//返回惟一结果集
// .count()//返回结果集的数量
// .listPage(firstResult, maxResults);//分页查询
.list();//返回列表
if(list!=null && list.size()>0){
for(Task task:list){
System.out.println("任务ID:"+task.getId());
System.out.println("任务名称:"+task.getName());
System.out.println("任务的创建时间:"+task.getCreateTime());
System.out.println("任务的办理人:"+task.getAssignee());
System.out.println("流程实例ID:"+task.getProcessInstanceId());
System.out.println("执行对象ID:"+task.getExecutionId());
System.out.println("流程定义ID:"+task.getProcessDefinitionId());
System.out.println("########################################################");
}
}
}
/**完成我的任务*/
@Test
public void completeMyPersonalTask(){
//任务ID
String taskId = "4302";
processEngine.getTaskService()//与正在执行的任务管理相关的Service
.complete(taskId);
System.out.println("完成任务:任务ID:"+taskId);
}
}
说明:
- 一个流程中流程实例只有1个,执行对象有多个
- 并行网关的功能是基于进入和外出的顺序流的:
分支(fork): 并行后的所有外出顺序流,为每个顺序流都创建一个并发分支。
汇聚(join): 所有到达并行网关,在此等待的进入分支, 直到所有进入顺序流的分支都到达以后, 流程就会通过汇聚网关。
- 并行网关的进入和外出都是使用相同节点标识
- 如果同一个并行网关有多个进入和多个外出顺序流, 它就同时具有分支和汇聚功能。 这时,网关会先汇聚所有进入的顺序流,然后再切分成多个并行分支。
- 并行网关不会解析条件。 即使顺序流中定义了条件,也会被忽略。
- 并行网关不需要是“平衡的”(比如, 对应并行网关的进入和外出节点数目不一定相等)。如图中标示是合法的:
接收任务:
接收任务是一个简单任务,它会等待对应消息的到达。 当前,官方只实现了这个任务的java语义。 当流程达到接收任务,流程状态会保存到数据库中。
在任务创建后,意味着流程会进入等待状态, 直到引擎接收了一个特定的消息, 这会触发流程穿过接收任务继续执行。
bpmn文件:
<?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="350.0" y="50.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="350.0" y="380.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="receivetask1" id="BPMNShape_receivetask1">
<omgdc:Bounds height="55.0" width="105.0" x="315.0" y="150.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="receivetask2" id="BPMNShape_receivetask2">
<omgdc:Bounds height="55.0" width="105.0" x="315.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="367.0" y="315.0"></omgdi:waypoint>
<omgdi:waypoint x="367.0" y="380.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="367.0" y="85.0"></omgdi:waypoint>
<omgdi:waypoint x="367.0" y="150.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="367.0" y="205.0"></omgdi:waypoint>
<omgdi:waypoint x="367.0" y="260.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
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());
}
}