接着 BPMN 2.0 - Activiti, 开始测试动态分配流程任务执行人.
方式一
添加一个单个task级别的listener:
ManagerTaskListenerHandler
1
2
3
4
5
6
7public class ManagerTaskListenerHandler implements TaskListener {
public void notify(DelegateTask delegateTask) {
String assignee = "Tom";
delegateTask.setAssignee(assignee);
}
}创建工作流, 并配置
taskListener
如下: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
<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="userDynamicTask2" name="动态任务处理2" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<endEvent id="endevent1" name="End"></endEvent>
<userTask id="休假" name="休假">
<extensionElements>
<activiti:taskListener event="create"
class="org.wangy.bpmn.demo.v1.bpmn.ManagerTaskListenerHandler"></activiti:taskListener>
</extensionElements>
</userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="休假"></sequenceFlow>
<sequenceFlow id="flow2" sourceRef="休假" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_userDynamicTask2">
<bpmndi:BPMNPlane bpmnElement="userDynamicTask2" id="BPMNPlane_userDynamicTask2">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="0.0" y="10.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="268.0301705943553" y="9.999999999999968"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="休假" id="BPMNShape_休假">
<omgdc:Bounds height="55.0" width="105.0" x="99.01508529717765" y="0.0"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="145.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="270.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>发布上述工作流
1
myService.createDynamicWorkflow("test import xml workflow 2", BPMNResourceType.BPMN_20, bpmnXML);
-
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
27public void test2UserTask() {
// 获取RuntimeService服务对象的实例
String processDefinitionKey = "userDynamicTask2";
// 自动执行与Key相对应的流程的最高版本
ProcessInstance processInstance = myService.startProcess(processDefinitionKey);
String processInstanceId = processInstance.getId();
logger.info("processInstanceId:" + processInstance.getId());
// 获取TaskService服务对象的实例
String assignee = "Tom";
List<Task> tasks = myService.getTasks(assignee);
Assert.assertEquals(1, tasks.size()); // 在ManagerTaskListenerHandler中, task已经本全部分配给了"Tom"
Task task = tasks.get(0);
logger.info("taskName:" + task.getName());
logger.info("taskAssignee:" + task.getAssignee());
Assert.assertEquals("休假", task.getName());
// 完成任务
myService.completeTask(task.getId());
// 检查结束状态
ProcessInstance pInstance = myService.findProcessInstance(processInstanceId);
Assert.assertNull(pInstance);
logger.info("动态任务处理流程2,使用自定义任务分配处理器方式成功执行!");
} -
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
31public void test3UserTask() {
// 获取RuntimeService服务对象的实例
String processDefinitionKey = "userDynamicTask2";
// 自动执行与Key相对应的流程的最高版本
String assignee = "Jeff";
ProcessInstance processInstance = myService.startProcess(processDefinitionKey, assignee);
String processInstanceId = processInstance.getId();
logger.info("processInstanceId:" + processInstance.getId());
// 获取TaskService服务对象的实例
List<Task> tasks = myService.getTasks(assignee);
Assert.assertEquals(0, tasks.size()); // 在ManagerTaskListenerHandler中, task已经本重新分配给分配给了"Tom"了
assignee = "Tom";
tasks = myService.getTasks(assignee);
Assert.assertEquals(1, tasks.size()); // 在ManagerTaskListenerHandler中, task已经本全部分配给了"Tom"
Task task = tasks.get(0);
logger.info("taskName:" + task.getName());
logger.info("taskAssignee:" + task.getAssignee());
Assert.assertEquals("休假", task.getName());
// 完成任务
myService.completeTask(task.getId());
// 检查结束状态
ProcessInstance pInstance = myService.findProcessInstance(processInstanceId);
Assert.assertNull(pInstance);
logger.info("动态任务处理流程2,使用自定义任务分配处理器方式成功执行!");
}
方式二
采用上例中的
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
<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="userDynamicTask1" name="动态任务处理1" isExecutable="true">
<startEvent id="startevent1" name="Start"></startEvent>
<userTask id="休假" name="休假" activiti:assignee="${person}"></userTask>
<sequenceFlow id="flow1" sourceRef="startevent1" targetRef="休假"></sequenceFlow>
<endEvent id="endevent1" name="End"></endEvent>
<sequenceFlow id="flow2" sourceRef="休假" targetRef="endevent1"></sequenceFlow>
</process>
<bpmndi:BPMNDiagram id="BPMNDiagram_userDynamicTask1">
<bpmndi:BPMNPlane bpmnElement="userDynamicTask1" id="BPMNPlane_userDynamicTask1">
<bpmndi:BPMNShape bpmnElement="startevent1" id="BPMNShape_startevent1">
<omgdc:Bounds height="35.0" width="35.0" x="189.5" y="194.5"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="休假" id="BPMNShape_休假">
<omgdc:Bounds height="55.0" width="105.0" x="155.5" y="276.5"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNShape bpmnElement="endevent1" id="BPMNShape_endevent1">
<omgdc:Bounds height="35.0" width="35.0" x="191.5" y="398.5"></omgdc:Bounds>
</bpmndi:BPMNShape>
<bpmndi:BPMNEdge bpmnElement="flow1" id="BPMNEdge_flow1">
<omgdi:waypoint x="357.0" y="145.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="270.0"></omgdi:waypoint>
</bpmndi:BPMNEdge>
</bpmndi:BPMNPlane>
</bpmndi:BPMNDiagram>
</definitions>添加一个全局Listener:
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
38public class TaskListenerTestHandler implements ActivitiEventListener {
//~ Static fields/initializers ---------------------------------------------------------------------------------------
/** TODO: DOCUMENT ME! */
public static final Logger LOGGER = LoggerFactory.getLogger(TaskListenerTestHandler.class);
//~ Methods ----------------------------------------------------------------------------------------------------------
/**
* @see org.activiti.engine.delegate.event.ActivitiEventListener#isFailOnException()
*/
public boolean isFailOnException() {
return false;
}
//~ ------------------------------------------------------------------------------------------------------------------
/**
* @see org.activiti.engine.delegate.event.ActivitiEventListener#onEvent(org.activiti.engine.delegate.event.ActivitiEvent)
*/
public void onEvent(ActivitiEvent event) {
LOGGER.info("Activiti Event --> type: {}, process instance: {}", event.getType(), event.getProcessInstanceId());
switch (event.getType()) {
case TASK_CREATED: {
ActivitiEntityEventImpl evt = (ActivitiEntityEventImpl) event;
String assignee = "Tom";
TaskEntity entity = (TaskEntity) evt.getEntity();
entity.setAssignee(assignee);
break;
}
default: { }
}
}
}配置Listener
1
2
3
4
5
6
7
8
9
10public void init(ProcessEngineConfigurationImpl processEngineConfiguration) {
List<ActivitiEventListener> eventListeners = processEngineConfiguration.getEventListeners();
if (Objects.isNull(eventListeners)) {
eventListeners = new ArrayList<>();
processEngineConfiguration.setEventListeners(eventListeners);
}
eventListeners.add(new TaskListenerTestHandler());
}