测试数据
sequenceFlow.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: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/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>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" name="二级费用" sourceRef="usertask1" targetRef="endevent1">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${message=="二级费用"}]]></conditionExpression>
</sequenceFlow>
<userTask id="usertask2" name="总经理审批" activiti:assignee="陈总裁"></userTask>
<sequenceFlow id="flow3" name="一级费用" sourceRef="usertask1" targetRef="usertask2">
<conditionExpression xsi:type="tFormalExpression"><![CDATA[${message=="一级费用"}]]></conditionExpression>
</sequenceFlow>
<sequenceFlow id="flow4" sourceRef="usertask2" targetRef="endevent1"></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="100.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="305.0" y="180.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="340.0" y="390.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask2" id="BPMNShape_usertask2">
<omgdc:Bounds height="55.0" width="105.0" x="510.0" y="260.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="135.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="180.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="357.0" y="235.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="390.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="48.0" x="308.0" y="300.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3">
<omgdi:waypoint x="357.0" y="235.0"></omgdi:waypoint>
<omgdi:waypoint x="562.0" y="260.0"></omgdi:waypoint>
<bpmndi:BPMNLabel>
<omgdc:Bounds height="14.0" width="48.0" x="387.0" y="241.0"></omgdc:Bounds>
</bpmndi:BPMNLabel>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4">
<omgdi:waypoint x="562.0" y="315.0"></omgdi:waypoint>
<omgdi:waypoint x="357.0" y="390.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
/**部署流程定义(从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());
System.out.println("部署时间:"+deployment.getDeploymentTime());
// 部署ID:2601
// 部署名称:连线
// 部署时间:Wed Sep 06 20:23:15 CST 2017
}
/**启动流程实例*/
@Test
public void startProcessInstance(){
//流程定义的key.,key对应helloworld.bpmn文件中id的属性值,使用key值启动,默认是按照最新版本的流程定义启动
String processDefinitionKey = "sequenceFlow";
ProcessInstance pi = processEngine.getRuntimeService()//与正在执行的流程实例和执行对象相关的Service
.startProcessInstanceByKey(processDefinitionKey);//使用流程定义的key启动流程实例,
System.out.println("流程实例ID:"+pi.getId());//
System.out.println("流程定义ID:"+pi.getProcessDefinitionId());//
// 流程实例ID:2701
// 流程定义ID:sequenceFlow:4:2604
}
测试
1先查看陈经理的当前任务
/**查询当前人的个人任务*/
@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){
System.out.println("--关联表: ACT_RU_TASK----");
for(Task task:list){
System.out.println("--------"+task.getAssignee()+"任务:"+task.getName()+"-----------------");
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());
// 任务ID:2704
// 任务名称:部门经理审批
// 任务的创建时间:Wed Sep 06 20:23:31 CST 2017
// 任务的办理人:陈经理
// 流程实例ID:2701
// 执行对象ID:2701
// 流程定义ID:sequenceFlow:4:2604
}
}
}
2通过流程变量来控制流程连线
/**完成我的任务*/
@Test
public void completeMyPersonalTask(){
//任务ID
String taskId = "2704";
//完成任务的同时,设置流程变量,使用流程变量用来指定完成任务后,下一个连线,对应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);
// 完成任务:任务ID:2704
}
查看数据库当前运行的任务表
可以看到任务已经走到总经理审批了。
同理当完成任务那里设置流程变量为二级费用,那么任务就走向结束