提交 | 用户 | 时间
|
e7c126
|
1 |
package com.iailab.module.bpm.framework.flowable.core.listener; |
H |
2 |
|
|
3 |
import cn.hutool.core.collection.CollUtil; |
bb2880
|
4 |
import cn.hutool.core.util.ObjectUtil; |
e7c126
|
5 |
import cn.hutool.core.util.StrUtil; |
bb2880
|
6 |
import com.iailab.framework.common.util.number.NumberUtils; |
H |
7 |
import com.iailab.module.bpm.enums.definition.BpmBoundaryEventType; |
|
8 |
import com.iailab.module.bpm.framework.flowable.core.enums.BpmnModelConstants; |
|
9 |
import com.iailab.module.bpm.framework.flowable.core.util.BpmnModelUtils; |
|
10 |
import com.iailab.module.bpm.service.definition.BpmModelService; |
e7c126
|
11 |
import com.iailab.module.bpm.service.task.BpmTaskService; |
H |
12 |
import com.google.common.collect.ImmutableSet; |
|
13 |
import lombok.extern.slf4j.Slf4j; |
bb2880
|
14 |
import org.flowable.bpmn.model.BoundaryEvent; |
H |
15 |
import org.flowable.bpmn.model.BpmnModel; |
|
16 |
import org.flowable.bpmn.model.FlowElement; |
e7c126
|
17 |
import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; |
H |
18 |
import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; |
|
19 |
import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener; |
|
20 |
import org.flowable.engine.delegate.event.FlowableActivityCancelledEvent; |
|
21 |
import org.flowable.engine.history.HistoricActivityInstance; |
bb2880
|
22 |
import org.flowable.job.api.Job; |
e7c126
|
23 |
import org.flowable.task.api.Task; |
H |
24 |
import org.springframework.context.annotation.Lazy; |
|
25 |
import org.springframework.stereotype.Component; |
|
26 |
|
|
27 |
import javax.annotation.Resource; |
|
28 |
import java.util.List; |
|
29 |
import java.util.Set; |
|
30 |
|
|
31 |
/** |
|
32 |
* 监听 {@link Task} 的开始与完成 |
|
33 |
* |
|
34 |
* @author jason |
|
35 |
*/ |
|
36 |
@Component |
|
37 |
@Slf4j |
|
38 |
public class BpmTaskEventListener extends AbstractFlowableEngineEventListener { |
|
39 |
|
|
40 |
@Resource |
bb2880
|
41 |
@Lazy // 延迟加载,避免循环依赖 |
H |
42 |
private BpmModelService modelService; |
e7c126
|
43 |
@Resource |
H |
44 |
@Lazy // 解决循环依赖 |
bb2880
|
45 |
private BpmTaskService taskService; |
e7c126
|
46 |
|
H |
47 |
public static final Set<FlowableEngineEventType> TASK_EVENTS = ImmutableSet.<FlowableEngineEventType>builder() |
|
48 |
.add(FlowableEngineEventType.TASK_CREATED) |
|
49 |
.add(FlowableEngineEventType.TASK_ASSIGNED) |
|
50 |
// .add(FlowableEngineEventType.TASK_COMPLETED) // 由于审批通过时,已经记录了 task 的 status 为通过,所以不需要监听了。 |
|
51 |
.add(FlowableEngineEventType.ACTIVITY_CANCELLED) |
bb2880
|
52 |
.add(FlowableEngineEventType.TIMER_FIRED) // 监听审批超时 |
e7c126
|
53 |
.build(); |
H |
54 |
|
bb2880
|
55 |
public BpmTaskEventListener() { |
e7c126
|
56 |
super(TASK_EVENTS); |
H |
57 |
} |
|
58 |
|
|
59 |
@Override |
|
60 |
protected void taskCreated(FlowableEngineEntityEvent event) { |
bb2880
|
61 |
taskService.processTaskCreated((Task) event.getEntity()); |
e7c126
|
62 |
} |
H |
63 |
|
|
64 |
@Override |
|
65 |
protected void taskAssigned(FlowableEngineEntityEvent event) { |
bb2880
|
66 |
taskService.processTaskAssigned((Task) event.getEntity()); |
e7c126
|
67 |
} |
H |
68 |
|
|
69 |
@Override |
|
70 |
protected void activityCancelled(FlowableActivityCancelledEvent event) { |
bb2880
|
71 |
List<HistoricActivityInstance> activityList = taskService.getHistoricActivityListByExecutionId(event.getExecutionId()); |
e7c126
|
72 |
if (CollUtil.isEmpty(activityList)) { |
H |
73 |
log.error("[activityCancelled][使用 executionId({}) 查找不到对应的活动实例]", event.getExecutionId()); |
|
74 |
return; |
|
75 |
} |
|
76 |
// 遍历处理 |
|
77 |
activityList.forEach(activity -> { |
|
78 |
if (StrUtil.isEmpty(activity.getTaskId())) { |
|
79 |
return; |
|
80 |
} |
bb2880
|
81 |
taskService.processTaskCanceled(activity.getTaskId()); |
e7c126
|
82 |
}); |
H |
83 |
} |
|
84 |
|
bb2880
|
85 |
@Override |
H |
86 |
@SuppressWarnings("PatternVariableCanBeUsed") |
|
87 |
protected void timerFired(FlowableEngineEntityEvent event) { |
|
88 |
// 1.1 只处理 BoundaryEvent 边界计时时间 |
|
89 |
String processDefinitionId = event.getProcessDefinitionId(); |
|
90 |
BpmnModel bpmnModel = modelService.getBpmnModelByDefinitionId(processDefinitionId); |
|
91 |
Job entity = (Job) event.getEntity(); |
|
92 |
FlowElement element = BpmnModelUtils.getFlowElementById(bpmnModel, entity.getElementId()); |
|
93 |
if (!(element instanceof BoundaryEvent)) { |
|
94 |
return; |
|
95 |
} |
|
96 |
// 1.2 判断是否为超时处理 |
|
97 |
BoundaryEvent boundaryEvent = (BoundaryEvent) element; |
|
98 |
String boundaryEventType = BpmnModelUtils.parseBoundaryEventExtensionElement(boundaryEvent, |
|
99 |
BpmnModelConstants.BOUNDARY_EVENT_TYPE); |
|
100 |
BpmBoundaryEventType bpmTimerBoundaryEventType = BpmBoundaryEventType.typeOf(NumberUtils.parseInt(boundaryEventType)); |
|
101 |
if (ObjectUtil.notEqual(bpmTimerBoundaryEventType, BpmBoundaryEventType.USER_TASK_TIMEOUT)) { |
|
102 |
return; |
|
103 |
} |
|
104 |
|
|
105 |
// 2. 处理超时 |
|
106 |
String timeoutHandlerType = BpmnModelUtils.parseBoundaryEventExtensionElement(boundaryEvent, |
|
107 |
BpmnModelConstants.USER_TASK_TIMEOUT_HANDLER_TYPE); |
|
108 |
String taskKey = boundaryEvent.getAttachedToRefId(); |
|
109 |
taskService.processTaskTimeout(event.getProcessInstanceId(), taskKey, NumberUtils.parseInt(timeoutHandlerType)); |
|
110 |
} |
e7c126
|
111 |
} |