1、创建流程引擎对象
private ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
2、发布一个流程
1 /** 2 * 2、发布一个流程 3 */ 4 @Test 5 public void testDeployProcess() { 6 RepositoryService repositoryService = processEngine.getRepositoryService(); 7 DeploymentBuilder builder = repositoryService.createDeployment(); 8 // 加载发布资源 9 builder.name("UserTask共有任务流程测试") // 设置流程显示别名 10 .addClasspathResource("userPublicTask.bpmn") // 设置流程规则文件 11 .addClasspathResource("userPublicTask.png"); // 设置流程规则的图片 12 // 发布流程 13 builder.deploy(); 14 }
3、启动流程、执行任务,并查看状态
1 /** 2 * 3、启动流程、执行任务,并查看状态 3 */ 4 @Test 5 public void testUserTask() { 6 //1.获取RuntimeService服务对象的实例 7 RuntimeService runtimeService = processEngine.getRuntimeService(); 8 String processDefinitionKey = "userPublicTask"; 9 //2.自动执行与Key相对应的流程的最高版本 10 ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey); 11 String processInstanceId = processInstance.getId(); 12 logger.info("processInstanceId:" + processInstance.getId()); 13 14 //3.获取TaskService服务对象的实例 15 TaskService taskService = processEngine.getTaskService(); 16 //4.私有任务执行 17 //4.1、获取私有任务 18 String assignee = "取号"; 19 List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list(); 20 Assert.assertEquals(1, tasks.size()); 21 Task task = tasks.get(0); 22 logger.info("taskName:" + task.getName()); 23 Assert.assertEquals("取号",task.getName()); 24 25 //4.2完成任务 26 taskService.complete(task.getId()); 27 28 //4.3检查流程状态 29 ProcessInstance p1 = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); 30 Assert.assertNotNull(p1); 31 logger.info("取号私有任务流程成功执行!"); 32 33 34 //5、共有任务执行 35 //5.1、确认柜员1私有任务列表中没有相关任务 36 String assignee1 = "柜员1"; 37 List<Task> tasks1 = taskService.createTaskQuery().taskAssignee(assignee1).list(); 38 Assert.assertEquals(0, tasks1.size()); 39 logger.info("柜员1的私有任务数量:" + tasks1.size()); 40 41 //5.2、确认柜员2私有任务列表中没有相关任务 42 String assignee2 = "柜员2"; 43 List<Task> tasks2 = taskService.createTaskQuery().taskAssignee(assignee2).list(); 44 Assert.assertEquals(0, tasks2.size()); 45 logger.info("柜员2的私有任务数量:" + tasks1.size()); 46 47 //5.3、获取共有任务列表 48 //5.3.1、获取柜员1共有任务列表 49 String candidateUser1 = "柜员1"; 50 List<Task> tasks3 = taskService.createTaskQuery().taskCandidateUser(candidateUser1).orderByTaskCreateTime().desc().list(); 51 Task task3 = tasks3.get(0); 52 logger.info("柜员1的共有任务数量:" + tasks3.size() + " ID: " + task3.getId() + " Name: " + task3.getName()); 53 54 //5.3.2、获取柜员2共有任务列表 55 String candidateUser2 = "柜员2"; 56 List<Task> tasks4 = taskService.createTaskQuery().taskCandidateUser(candidateUser2).orderByTaskCreateTime().desc().list(); 57 Task task4 = tasks4.get(0); 58 logger.info("柜员2的共有任务数量:" + tasks4.size() + " ID: " + task4.getId() + " Name: " + task4.getName()); 59 60 //5.4、柜员2受理任务 61 taskService.claim(task4.getId(), "柜员2"); 62 logger.info("柜员2受理任务!"); 63 64 //5.5、查看柜员1和柜员2的私有与共有任务数量 65 String assignee3 = "柜员1"; 66 List<Task> tasks5 = taskService.createTaskQuery().taskAssignee(assignee3).list(); 67 logger.info("柜员1的私有任务数量:" + tasks5.size()); 68 69 String assignee4 = "柜员2"; 70 List<Task> tasks6 = taskService.createTaskQuery().taskAssignee(assignee4).list(); 71 logger.info("柜员2的私有任务数量:" + tasks6.size()); 72 73 String candidateUser3 = "柜员1"; 74 List<Task> tasks7 = taskService.createTaskQuery().taskCandidateUser(candidateUser3).orderByTaskCreateTime().desc().list(); 75 logger.info("柜员1的共有任务数量:" + tasks7.size()); 76 77 String candidateUser4 = "柜员2"; 78 List<Task> tasks8 = taskService.createTaskQuery().taskCandidateUser(candidateUser4).orderByTaskCreateTime().desc().list(); 79 logger.info("柜员2的共有任务数量:" + tasks8.size()); 80 81 //5.6、柜员2完成任务 82 taskService.complete(task4.getId()); 83 84 //5.6检查结束状态 85 ProcessInstance p2 = runtimeService.createProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); 86 Assert.assertNull(p2); 87 logger.info("UserTask共有任务流程成功执行!"); 88 }
4、执行结果
[INFO][2015-12-02 10:09:44,768][com.mcs.activiti.test.TestPublicUserTask]processInstanceId:115001
[INFO][2015-12-02 10:09:44,792][com.mcs.activiti.test.TestPublicUserTask]taskName:取号
[INFO][2015-12-02 10:09:45,003][com.mcs.activiti.test.TestPublicUserTask]取号私有任务流程成功执行!
[INFO][2015-12-02 10:09:45,010][com.mcs.activiti.test.TestPublicUserTask]柜员1的私有任务数量:0
[INFO][2015-12-02 10:09:45,016][com.mcs.activiti.test.TestPublicUserTask]柜员2的私有任务数量:0
[INFO][2015-12-02 10:09:45,036][com.mcs.activiti.test.TestPublicUserTask]柜员1的共有任务数量:1 ID: 115007 Name: 取钱
[INFO][2015-12-02 10:09:45,041][com.mcs.activiti.test.TestPublicUserTask]柜员2的共有任务数量:1 ID: 115007 Name: 取钱
[INFO][2015-12-02 10:09:45,117][com.mcs.activiti.test.TestPublicUserTask]柜员2受理任务!
[INFO][2015-12-02 10:09:45,126][com.mcs.activiti.test.TestPublicUserTask]柜员1的私有任务数量:0
[INFO][2015-12-02 10:09:45,138][com.mcs.activiti.test.TestPublicUserTask]柜员2的私有任务数量:1
[INFO][2015-12-02 10:09:45,159][com.mcs.activiti.test.TestPublicUserTask]柜员1的共有任务数量:0
[INFO][2015-12-02 10:09:45,162][com.mcs.activiti.test.TestPublicUserTask]柜员2的共有任务数量:0
[INFO][2015-12-02 10:09:45,256][com.mcs.activiti.test.TestPublicUserTask]UserTask共有任务流程成功执行!
5、共有流程定义 userPublicTask.bpmn
1 <?xml version="1.0" encoding="UTF-8"?> 2 <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"> 3 <process id="userPublicTask" name="共有任务" isExecutable="true"> 4 <startEvent id="startevent1" name="Start"></startEvent> 5 <userTask id="取号" name="取号" activiti:assignee="取号"></userTask> 6 <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="取号"></sequenceFlow> 7 <userTask id="取钱" name="取钱" activiti:candidateUsers="柜员1,柜员2"></userTask> 8 <sequenceFlow id="flow3" sourceRef="取号" targetRef="取钱"></sequenceFlow> 9 <endEvent id="endevent2" name="End"></endEvent> 10 <sequenceFlow id="flow4" sourceRef="取钱" targetRef="endevent2"></sequenceFlow> 11 </process> 12 <bpmndi:BPMNDiagram id="BPMNDiagram_userPublicTask"> 13 <bpmndi:BPMNPlane bpmnElement="userPublicTask" id="BPMNPlane_userPublicTask"> 14 <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1"> 15 <omgdc:Bounds height="35.0" width="35.0" x="335.0" y="40.0"></omgdc:Bounds> 16 </bpmndi:BPMNShape> 17 <bpmndi:BPMNShape bpmnElement="取号" id="BPMNShape_取号"> 18 <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="110.0"></omgdc:Bounds> 19 </bpmndi:BPMNShape> 20 <bpmndi:BPMNShape bpmnElement="取钱" id="BPMNShape_取钱"> 21 <omgdc:Bounds height="55.0" width="105.0" x="300.0" y="210.0"></omgdc:Bounds> 22 </bpmndi:BPMNShape> 23 <bpmndi:BPMNShape bpmnElement="endevent2" id="BPMNShape_endevent2"> 24 <omgdc:Bounds height="35.0" width="35.0" x="335.0" y="310.0"></omgdc:Bounds> 25 </bpmndi:BPMNShape> 26 <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1"> 27 <omgdi:waypoint x="352.0" y="75.0"></omgdi:waypoint> 28 <omgdi:waypoint x="352.0" y="110.0"></omgdi:waypoint> 29 </bpmndi:BPMNEdge> 30 <bpmndi:BPMNEdge bpmnElement="flow3" id="BPMNEdge_flow3"> 31 <omgdi:waypoint x="352.0" y="165.0"></omgdi:waypoint> 32 <omgdi:waypoint x="352.0" y="210.0"></omgdi:waypoint> 33 </bpmndi:BPMNEdge> 34 <bpmndi:BPMNEdge bpmnElement="flow4" id="BPMNEdge_flow4"> 35 <omgdi:waypoint x="352.0" y="265.0"></omgdi:waypoint> 36 <omgdi:waypoint x="352.0" y="310.0"></omgdi:waypoint> 37 </bpmndi:BPMNEdge> 38 </bpmndi:BPMNPlane> 39 </bpmndi:BPMNDiagram> 40 </definitions>