Activiti学习笔记10 — 动态任务分配

动态任务分配使用的两种方式

一、通过特殊表达式,来获取任务信息 ,在流程 UserTask节点上设置 ${流程变量的Key}

1、流程定义

 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="userDynamicTask1" name="动态任务处理1" isExecutable="true">
 4     <startEvent id="startevent1" name="Start"></startEvent>
 5     <userTask id="休假" name="休假" activiti:assignee="${user}"></userTask>
 6     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="休假"></sequenceFlow>
 7     <endEvent id="endevent1" name="End"></endEvent>
 8     <sequenceFlow id="flow2" sourceRef="休假" targetRef="endevent1"></sequenceFlow>
 9   </process>
10   <bpmndi:BPMNDiagram id="BPMNDiagram_userDynamicTask1">
11     <bpmndi:BPMNPlane bpmnElement="userDynamicTask1" id="BPMNPlane_userDynamicTask1">
12       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
13         <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="110.0"></omgdc:Bounds>
14       </bpmndi:BPMNShape>
15       <bpmndi:BPMNShape bpmnElement="休假" id="BPMNShape_休假">
16         <omgdc:Bounds height="55.0" width="105.0" x="305.0" y="180.0"></omgdc:Bounds>
17       </bpmndi:BPMNShape>
18       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
19         <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="270.0"></omgdc:Bounds>
20       </bpmndi:BPMNShape>
21       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
22         <omgdi:waypoint x="357.0" y="145.0"></omgdi:waypoint>
23         <omgdi:waypoint x="357.0" y="180.0"></omgdi:waypoint>
24       </bpmndi:BPMNEdge>
25       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
26         <omgdi:waypoint x="357.0" y="235.0"></omgdi:waypoint>
27         <omgdi:waypoint x="357.0" y="270.0"></omgdi:waypoint>
28       </bpmndi:BPMNEdge>
29     </bpmndi:BPMNPlane>
30   </bpmndi:BPMNDiagram>
31 </definitions>
View Code

2、发布流程

 1 /**
 2      * 2、发布一个流程:发布通过特殊表达式,来获取任务信息  ${流程变量的Key}的流程
 3      */
 4     @Test
 5     public void  testDeployProcess() {
 6         RepositoryService repositoryService = processEngine.getRepositoryService();
 7         DeploymentBuilder builder = repositoryService.createDeployment();
 8         // 加载发布资源
 9         builder.name("动态任务处理流程1-测试") // 设置流程显示别名
10                 .addClasspathResource("leave3.bpmn") // 设置流程规则文件
11                 .addClasspathResource("leave3.png"); // 设置流程规则的图片
12         // 发布流程
13         builder.deploy();
14     }
View Code

3、启动与测试流程

 1     
 2     /**
 3      * 3、启动流程、执行任务,并查看状态
 4      */
 5     @Test
 6     public void testUserTask() {        
 7         // 获取RuntimeService服务对象的实例
 8         RuntimeService runtimeService = processEngine.getRuntimeService();
 9         String processDefinitionKey = "userDynamicTask1";
10         // 自动执行与Key相对应的流程的最高版本
11         Map<String, Object> variables = new HashMap<String, Object>();
12         //variables.put("user", "张三");
13         variables.put("user", "李四");
14         
15         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey,variables);        
16         String processInstanceId = processInstance.getId();        
17         logger.info("processInstanceId:" + processInstance.getId());
18         
19         // 获取TaskService服务对象的实例
20         TaskService taskService = processEngine.getTaskService();
21         //String assignee = "张三";
22         String assignee = "李四";
23         List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
24         Assert.assertEquals(1, tasks.size());
25         Task task = tasks.get(0);        
26         logger.info("taskName:" + task.getName());
27         logger.info("taskAssignee:" + task.getAssignee());
28         Assert.assertEquals("休假",task.getName());
29         
30         //完成任务
31         taskService.complete(task.getId());        
32         
33         //检查结束状态
34         ProcessInstance pInstance = runtimeService
35                                                         .createProcessInstanceQuery()
36                                                         .processInstanceId(processInstanceId)
37                                                         .singleResult();
38         Assert.assertNull(pInstance);
39         logger.info("动态任务处理流程1,使用${流程变量的Key}方式成功执行!");        
40         
41     }
View Code

4、运行结果

[INFO][2015-12-02 11:44:05,129][com.mcs.activiti.test.TestUserTaskDynamic]processInstanceId:130001
[INFO][2015-12-02 11:44:05,159][com.mcs.activiti.test.TestUserTaskDynamic]taskName:休假
[INFO][2015-12-02 11:44:05,159][com.mcs.activiti.test.TestUserTaskDynamic]taskAssignee:李四
[INFO][2015-12-02 11:44:05,456][com.mcs.activiti.test.TestUserTaskDynamic]动态任务处理流程1,使用${流程变量的Key}方式成功执行!

二、使用任务分配处理器

1、自定义任务分配处理器步骤

  1.1、  创建一个类来充分“任务分配处理器”,需要实现TaskListener

  1.2、  在实现方式中添加响应的处理逻辑;

  1.3、  在流程中,配置/使用任务处理器。

2、创建自定义任务分配处理器,实现TaskListener

 1 package mcs.listener;
 2 
 3 import org.activiti.engine.delegate.DelegateTask;
 4 import org.activiti.engine.delegate.TaskListener;
 5 
 6 public class ManagerTaskListenerHandler implements TaskListener {
 7 
 8     private static final long serialVersionUID = 1L;
 9 
10     @Override
11     public void notify(DelegateTask delegateTask) {
12         String assignee = "王五";
13         delegateTask.setAssignee(assignee);
14         
15         //delegateTask.addCandidateUser("王小二");
16     }
17 
18 }
View Code

3、流程定义

 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="userDynamicTask2" name="动态任务处理2" isExecutable="true">
 4     <startEvent id="startevent1" name="Start"></startEvent>
 5     <endEvent id="endevent1" name="End"></endEvent>
 6     <userTask id="休假" name="休假">
 7       <extensionElements>
 8         <activiti:taskListener event="create" class="mcs.listener.ManagerTaskListenerHandler"></activiti:taskListener>
 9       </extensionElements>
10     </userTask>
11     <sequenceFlow id="flow1" sourceRef="startevent1" targetRef="休假"></sequenceFlow>
12     <sequenceFlow id="flow2" sourceRef="休假" targetRef="endevent1"></sequenceFlow>
13   </process>
14   <bpmndi:BPMNDiagram id="BPMNDiagram_userDynamicTask2">
15     <bpmndi:BPMNPlane bpmnElement="userDynamicTask2" id="BPMNPlane_userDynamicTask2">
16       <bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
17         <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="110.0"></omgdc:Bounds>
18       </bpmndi:BPMNShape>
19       <bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
20         <omgdc:Bounds height="35.0" width="35.0" x="340.0" y="270.0"></omgdc:Bounds>
21       </bpmndi:BPMNShape>
22       <bpmndi:BPMNShape bpmnElement="休假" id="BPMNShape_休假">
23         <omgdc:Bounds height="55.0" width="105.0" x="305.0" y="180.0"></omgdc:Bounds>
24       </bpmndi:BPMNShape>
25       <bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
26         <omgdi:waypoint x="357.0" y="145.0"></omgdi:waypoint>
27         <omgdi:waypoint x="357.0" y="180.0"></omgdi:waypoint>
28       </bpmndi:BPMNEdge>
29       <bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
30         <omgdi:waypoint x="357.0" y="235.0"></omgdi:waypoint>
31         <omgdi:waypoint x="357.0" y="270.0"></omgdi:waypoint>
32       </bpmndi:BPMNEdge>
33     </bpmndi:BPMNPlane>
34   </bpmndi:BPMNDiagram>
35 </definitions>
View Code

4、发布流程

 1 /**
 2      * 4、发布一个流程:发布通过自定义任务分配处理器的流程
 3      *         自定义任务分配处理器步骤:
 4      *         1、创建一个类来充分“任务分配处理器”,需要实现TaskListener
 5     *        2、在实现方式中添加响应的处理逻辑;
 6     *        3、在流程中,配置/使用任务处理器。
 7      */
 8     @Test
 9     public void  testDeployProcess2() {
10         RepositoryService repositoryService = processEngine.getRepositoryService();
11         DeploymentBuilder builder = repositoryService.createDeployment();
12         // 加载发布资源
13         builder.name("动态任务处理流程2-测试") // 设置流程显示别名
14                 .addClasspathResource("leave4.bpmn") // 设置流程规则文件
15                 .addClasspathResource("leave4.png"); // 设置流程规则的图片
16         // 发布流程
17         builder.deploy();
18     }
19     
View Code

5、启动与测试流程

 1     /**
 2      * 5、启动流程、执行任务,并查看状态
 3      */
 4     @Test
 5     public void testUserTask2() {        
 6         // 获取RuntimeService服务对象的实例
 7         RuntimeService runtimeService = processEngine.getRuntimeService();
 8         String processDefinitionKey = "userDynamicTask2";
 9         // 自动执行与Key相对应的流程的最高版本    
10         ProcessInstance processInstance = runtimeService.startProcessInstanceByKey(processDefinitionKey);        
11         String processInstanceId = processInstance.getId();        
12         logger.info("processInstanceId:" + processInstance.getId());
13         
14         // 获取TaskService服务对象的实例
15         TaskService taskService = processEngine.getTaskService();
16         String assignee = "王五";
17         List<Task> tasks = taskService.createTaskQuery().taskAssignee(assignee).list();
18         Assert.assertEquals(1, tasks.size());
19         Task task = tasks.get(0);        
20         logger.info("taskName:" + task.getName());
21         logger.info("taskAssignee:" + task.getAssignee());
22         Assert.assertEquals("休假",task.getName());
23         
24         //完成任务
25         taskService.complete(task.getId());        
26         
27         //检查结束状态
28         ProcessInstance pInstance = runtimeService
29                                                         .createProcessInstanceQuery()
30                                                         .processInstanceId(processInstanceId)
31                                                         .singleResult();
32         Assert.assertNull(pInstance);
33         logger.info("动态任务处理流程2,使用自定义任务分配处理器方式成功执行!");        
34         
35     }
View Code

6、运行结果

[INFO][2015-12-02 11:46:51,818][com.mcs.activiti.test.TestUserTaskDynamic]processInstanceId:132501
[INFO][2015-12-02 11:46:51,879][com.mcs.activiti.test.TestUserTaskDynamic]taskName:休假
[INFO][2015-12-02 11:46:51,879][com.mcs.activiti.test.TestUserTaskDynamic]taskAssignee:王五
[INFO][2015-12-02 11:46:52,192][com.mcs.activiti.test.TestUserTaskDynamic]动态任务处理流程2,使用自定义任务分配处理器方式成功执行!

posted @ 2015-12-02 11:47  小个子猫  阅读(2249)  评论(0编辑  收藏  举报