上一章我们讲到用户任务分为个人任务和组任务,这一章我们来具体看看怎么使用组任务。
这里我们用到的流程还是最简单的审批流程:
方式一:定义流程时指定组任务候选人(该方式不够灵活,使用较少)
方式二:使用流程变量指定组任务候选人
流程图代码
<?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="groupTask1" name="GroupTask1" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="审批" activiti:candidateUsers="#{userIds}"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_groupTask1">
<bpmndi:BPMNPlane bpmnElement="groupTask1" id="BPMNPlane_groupTask1">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="410.0" y="40.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="375.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="410.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="427.0" y="75.0"></omgdi:waypoint>
<omgdi:waypoint x="427.0" y="120.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="427.0" y="175.0"></omgdi:waypoint>
<omgdi:waypoint x="427.0" y="220.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
方式三:使用监听类设置组成员
流程图代码
<?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="groupTask3" name="GroupTask3" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="usertask1" name="审批">
<extensionElements>
<activiti:taskListener event="create" class="com.activiti.test.GroupTaskListenerImpl"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="usertask1"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="usertask1" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_groupTask3">
<bpmndi:BPMNPlane bpmnElement="groupTask3" id="BPMNPlane_groupTask3">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="390.0" y="40.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="usertask1" id="BPMNShape_usertask1">
<omgdc:Bounds height="55.0" width="105.0" x="355.0" y="120.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="390.0" y="220.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="407.0" y="75.0"></omgdi:waypoint>
<omgdi:waypoint x="407.0" y="120.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
<bpmndi:BPMNEdge bpmnElement="flow2" id="BPMNEdge_flow2">
<omgdi:waypoint x="407.0" y="175.0"></omgdi:waypoint>
<omgdi:waypoint x="407.0" y="220.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
java测试代码
package com.activiti.test;
import org.activiti.engine.delegate.DelegateTask;
import org.activiti.engine.delegate.TaskListener;
public class GroupTaskListenerImpl implements TaskListener {
/**
* 指定个人任务和组任务的办理人
*/
@Override
public void notify(DelegateTask delegateTask) {
delegateTask.addCandidateUser("crystal");//分配组任务的办理人
delegateTask.addCandidateUser("barry");
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
package com.activiti.test;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import org.activiti.engine.ProcessEngine;
import org.activiti.engine.ProcessEngines;
import org.activiti.engine.history.HistoricIdentityLink;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.IdentityLink;
import org.activiti.engine.task.Task;
import org.junit.Test;
public class GroupTask {
ProcessEngine processEngine = ProcessEngines.getDefaultProcessEngine();
/**
* 部署流程定义,启动流程实例
*/
@Test
public void deployAndStart() {
processEngine.getRepositoryService()
.createDeployment()
.name("指定组任务")
.addClasspathResource("diagrams/groupTask3.bpmn")
.addClasspathResource("diagrams/groupTask3.png")
.deploy();
System.out.println("部署成功!");
// 方式一
/*ProcessInstance pi = processEngine.getRuntimeService()
.startProcessInstanceByKey("groupTask1");*/
// 方式二
/*Map<String, Object> variables = new HashMap<String, Object>();
variables.put("userIds", "crystal,lina,barry");
ProcessInstance pi = processEngine.getRuntimeService()
.startProcessInstanceByKey("groupTask1", variables);*/
// 方式三
ProcessInstance pi = processEngine.getRuntimeService()
.startProcessInstanceByKey("groupTask3");
System.out.println("流程实例启动成功!实例ID:" + pi.getId());
}
/**
* 查询我的个人任务列表
*/
@Test
public void findMyTaskList(){
String userId = "张三";
List<Task> list = processEngine.getTaskService()
.createTaskQuery()
.taskAssignee(userId)//指定个人任务查询
.list();
if (list != null && list.size()>0) {
for(Task task : list){
System.out.println("task id="+task.getId());
System.out.println("name="+task.getName());
System.out.println("assinee="+task.getAssignee());
System.out.println("executionId="+task.getExecutionId());
System.out.println("=====================================");
}
}
}
/**
* 查看组任务
* 个人认领任务后,组任务不再存在,若将个人任务回退到组任务,又可以看到组任务
*/
@Test
public void findMyGroupTask() {
String userId = "crystal";
List<Task> list = processEngine.getTaskService()
.createTaskQuery()
.taskCandidateUser(userId)// 指定组任务查询
.list();
if (list != null && list.size()>0) {
for(Task task : list){
System.out.println("task id="+task.getId());
System.out.println("name="+task.getName());
System.out.println("assinee="+task.getAssignee());
System.out.println("executionId="+task.getExecutionId());
System.out.println("=====================================");
}
}
}
/**
* 查看组任务成员列表
*/
@Test
public void findGroupUser() {
String taskId = "20008";
List<IdentityLink> list = processEngine.getTaskService()
.getIdentityLinksForTask(taskId);// 获取列表
if (list != null && list.size() > 0) {
for (IdentityLink il : list) {
System.out.println("用户ID:" + il.getUserId());
}
}
}
/**
* 查询组任务成员历史列表
*/
@Test
public void findHisGroupUser() {
String taskId = "20008";
List<HistoricIdentityLink> list = processEngine.getHistoryService()
.getHistoricIdentityLinksForTask(taskId);
if (list != null && list.size() > 0) {
for (HistoricIdentityLink il : list) {
System.out.println("用户ID:" + il.getUserId());
}
}
}
/**
* 将组任务分配给个人任务,拾取任务
* 注意:认领任务的时候,可以是组任务成员中的人,也可以不是组任务成员的人,此时通过Type的类型为participant来指定任务的办理人
* 由1个人去完成任务
*/
@Test
public void claim() {
String taskId = "20008";//任务ID
String userId = "crystal";//分配的办理人
processEngine.getTaskService()
.claim(taskId, userId);
}
/**
* 完成我的个人任务
*/
@Test
public void complete() {
String taskId = "20008";
processEngine.getTaskService().complete(taskId);
System.out.println("完成任务");
}
/**
* 可以分配个人任务回退到组任务,(前提:之前是个组任务)
*/
@Test
public void setAssigneeTask(){
String taskId = "20008";//任务ID
processEngine.getTaskService()//
.setAssignee(taskId, null);
}
/**
* 添加组成员
*/
@Test
public void addGroupUser(){
String taskId = "20008";
String userId = "ken";
processEngine.getTaskService().addCandidateUser(taskId, userId);
}
/**
* 删除组成员
*/
@Test
public void deleteGroupUser(){
String taskId = "2509";
String userId = "ken";
processEngine.getTaskService()
.deleteCandidateUser(taskId, userId);
}
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
- 76
- 77
- 78
- 79
- 80
- 81
- 82
- 83
- 84
- 85
- 86
- 87
- 88
- 89
- 90
- 91
- 92
- 93
- 94
- 95
- 96
- 97
- 98
- 99
- 100
- 101
- 102
- 103
- 104
- 105
- 106
- 107
- 108
- 109
- 110
- 111
- 112
- 113
- 114
- 115
- 116
- 117
- 118
- 119
- 120
- 121
- 122
- 123
- 124
- 125
- 126
- 127
- 128
- 129
- 130
- 131
- 132
- 133
- 134
- 135
- 136
- 137
- 138
- 139
- 140
- 141
- 142
- 143
- 144
- 145
- 146
- 147
- 148
- 149
- 150
- 151
- 152
- 153
- 154
- 155
- 156
- 157
- 158
- 159
- 160
- 161
- 162
- 163
- 164
- 165
- 166
- 167
- 168
- 169
- 170
总结:
组任务及三种分配方式:
1:在taskProcess.bpmn中直接写 candidate-users=“张三,李四,王五”
2:在taskProcess.bpmn中写 candidate-users =“#{userIds}”,变量的值要是String的(使用流程变量指定办理人)。
3:使用TaskListener接口,使用类实现该接口,在类中定义添加组任务的用户
个人任务和组任务存放办理人对应的表:
act_ru_identitylink表存放任务的办理人,包括个人任务和组任务,表示正在执行的任务
act_hi_identitylink表存放任务的办理人,包括个人任务和组任务,表示历史任务
区别在于:如果是个人任务TYPE的类型表示participant(参与者), 如果是组任务TYPE的类型表示candidate(候选者)和participant(参与者)