Commit 610f39de05caea8f7fcb9094de9053540cb09321

Authored by jason
1 parent 62aabc63

【缺陷修复】 解决并行分支拒绝任务时,流程未结束问题

yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/framework/flowable/core/listener/BpmProcessInstanceEventListener.java
@@ -6,6 +6,7 @@ import jakarta.annotation.Resource; @@ -6,6 +6,7 @@ import jakarta.annotation.Resource;
6 import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; 6 import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent;
7 import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType; 7 import org.flowable.common.engine.api.delegate.event.FlowableEngineEventType;
8 import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener; 8 import org.flowable.engine.delegate.event.AbstractFlowableEngineEventListener;
  9 +import org.flowable.engine.delegate.event.FlowableCancelledEvent;
9 import org.flowable.engine.runtime.ProcessInstance; 10 import org.flowable.engine.runtime.ProcessInstance;
10 import org.springframework.context.annotation.Lazy; 11 import org.springframework.context.annotation.Lazy;
11 import org.springframework.stereotype.Component; 12 import org.springframework.stereotype.Component;
@@ -22,6 +23,7 @@ public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEvent @@ -22,6 +23,7 @@ public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEvent
22 23
23 public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder() 24 public static final Set<FlowableEngineEventType> PROCESS_INSTANCE_EVENTS = ImmutableSet.<FlowableEngineEventType>builder()
24 .add(FlowableEngineEventType.PROCESS_COMPLETED) 25 .add(FlowableEngineEventType.PROCESS_COMPLETED)
  26 + .add(FlowableEngineEventType.PROCESS_CANCELLED)
25 .build(); 27 .build();
26 28
27 @Resource 29 @Resource
@@ -37,4 +39,10 @@ public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEvent @@ -37,4 +39,10 @@ public class BpmProcessInstanceEventListener extends AbstractFlowableEngineEvent
37 processInstanceService.processProcessInstanceCompleted((ProcessInstance)event.getEntity()); 39 processInstanceService.processProcessInstanceCompleted((ProcessInstance)event.getEntity());
38 } 40 }
39 41
  42 + @Override
  43 + // 特殊情况:当跳转到 EndEvent 流程实例未结束, 会执行 deleteProcessInstance 方法。
  44 + protected void processCancelled(FlowableCancelledEvent event) {
  45 + ProcessInstance processInstance = processInstanceService.getProcessInstance(event.getProcessInstanceId());
  46 + processInstanceService.processProcessInstanceCompleted(processInstance);
  47 + }
40 } 48 }
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmProcessInstanceServiceImpl.java
@@ -668,7 +668,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService @@ -668,7 +668,7 @@ public class BpmProcessInstanceServiceImpl implements BpmProcessInstanceService
668 runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, reason); 668 runtimeService.setVariable(id, BpmnVariableConstants.PROCESS_INSTANCE_VARIABLE_REASON, reason);
669 669
670 // 2. 结束流程 670 // 2. 结束流程
671 - taskService.moveTaskToEnd(id); 671 + taskService.moveTaskToEnd(id, reason);
672 } 672 }
673 673
674 @Override 674 @Override
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskService.java
@@ -195,8 +195,9 @@ public interface BpmTaskService { @@ -195,8 +195,9 @@ public interface BpmTaskService {
195 * 将指定流程实例的、进行中的流程任务,移动到结束节点 195 * 将指定流程实例的、进行中的流程任务,移动到结束节点
196 * 196 *
197 * @param processInstanceId 流程编号 197 * @param processInstanceId 流程编号
  198 + * @param reason 原因
198 */ 199 */
199 - void moveTaskToEnd(String processInstanceId); 200 + void moveTaskToEnd(String processInstanceId, String reason);
200 201
201 /** 202 /**
202 * 将任务退回到指定的 targetDefinitionKey 位置 203 * 将任务退回到指定的 targetDefinitionKey 位置
yudao-module-bpm/yudao-module-bpm-biz/src/main/java/cn/iocoder/yudao/module/bpm/service/task/BpmTaskServiceImpl.java
@@ -166,7 +166,7 @@ public class BpmTaskServiceImpl implements BpmTaskService { @@ -166,7 +166,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
166 166
167 // 4. 任务表单 167 // 4. 任务表单
168 BpmFormDO taskForm = null; 168 BpmFormDO taskForm = null;
169 - if (StrUtil.isNotBlank(todoTask.getFormKey())){ 169 + if (StrUtil.isNotBlank(todoTask.getFormKey())) {
170 taskForm = formService.getForm(NumberUtils.parseLong(todoTask.getFormKey())); 170 taskForm = formService.getForm(NumberUtils.parseLong(todoTask.getFormKey()));
171 } 171 }
172 172
@@ -652,7 +652,7 @@ public class BpmTaskServiceImpl implements BpmTaskService { @@ -652,7 +652,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
652 } 652 }
653 // 3.2 情况二:直接结束,审批不通过 653 // 3.2 情况二:直接结束,审批不通过
654 processInstanceService.updateProcessInstanceReject(instance, reqVO.getReason()); // 标记不通过 654 processInstanceService.updateProcessInstanceReject(instance, reqVO.getReason()); // 标记不通过
655 - moveTaskToEnd(task.getProcessInstanceId()); // 结束流程 655 + moveTaskToEnd(task.getProcessInstanceId(), BpmCommentTypeEnum.REJECT.formatComment(reqVO.getReason())); // 结束流程
656 } 656 }
657 657
658 /** 658 /**
@@ -823,7 +823,7 @@ public class BpmTaskServiceImpl implements BpmTaskService { @@ -823,7 +823,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
823 } 823 }
824 824
825 @Override 825 @Override
826 - public void moveTaskToEnd(String processInstanceId) { 826 + public void moveTaskToEnd(String processInstanceId, String reason) {
827 List<Task> taskList = getRunningTaskListByProcessInstanceId(processInstanceId, null, null); 827 List<Task> taskList = getRunningTaskListByProcessInstanceId(processInstanceId, null, null);
828 if (CollUtil.isEmpty(taskList)) { 828 if (CollUtil.isEmpty(taskList)) {
829 return; 829 return;
@@ -849,6 +849,13 @@ public class BpmTaskServiceImpl implements BpmTaskService { @@ -849,6 +849,13 @@ public class BpmTaskServiceImpl implements BpmTaskService {
849 .processInstanceId(processInstanceId) 849 .processInstanceId(processInstanceId)
850 .moveActivityIdsToSingleActivityId(activityIds, endEvent.getId()) 850 .moveActivityIdsToSingleActivityId(activityIds, endEvent.getId())
851 .changeState(); 851 .changeState();
  852 +
  853 + // 3. 如果跳转到 EndEvent 流程还未结束, 执行 deleteProcessInstance 方法。
  854 + List<Execution> executionList = runtimeService.createExecutionQuery().processInstanceId(processInstanceId).list();
  855 + if (CollUtil.isNotEmpty(executionList)) {
  856 + log.warn("执行跳转到 EndEvent 后, 流程实例未结束。执行 [deleteProcessInstance] 方法");
  857 + runtimeService.deleteProcessInstance(processInstanceId, reason);
  858 + }
852 } 859 }
853 860
854 @Override 861 @Override
@@ -1197,7 +1204,7 @@ public class BpmTaskServiceImpl implements BpmTaskService { @@ -1197,7 +1204,7 @@ public class BpmTaskServiceImpl implements BpmTaskService {
1197 } 1204 }
1198 } 1205 }
1199 // 注意:需要基于 instance 设置租户编号,避免 Flowable 内部异步时,丢失租户编号 1206 // 注意:需要基于 instance 设置租户编号,避免 Flowable 内部异步时,丢失租户编号
1200 - FlowableUtils.execute(processInstance.getTenantId(),()-> { 1207 + FlowableUtils.execute(processInstance.getTenantId(), () -> {
1201 AdminUserRespDTO startUser = adminUserApi.getUser(Long.valueOf(processInstance.getStartUserId())); 1208 AdminUserRespDTO startUser = adminUserApi.getUser(Long.valueOf(processInstance.getStartUserId()));
1202 messageService.sendMessageWhenTaskAssigned(BpmTaskConvert.INSTANCE.convert(processInstance, startUser, task)); 1209 messageService.sendMessageWhenTaskAssigned(BpmTaskConvert.INSTANCE.convert(processInstance, startUser, task));
1203 }); 1210 });