diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/event/WorkflowInstanceStatusChangeEvent.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/event/WorkflowInstanceStatusChangeEvent.java new file mode 100644 index 00000000..56f4bd4e --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/event/WorkflowInstanceStatusChangeEvent.java @@ -0,0 +1,20 @@ +package com.qqchen.deploy.backend.workflow.event; + +import com.qqchen.deploy.backend.workflow.enums.WorkflowInstanceStatusEnums; +import lombok.Builder; +import lombok.Data; + +import java.time.LocalDateTime; +import java.util.Date; + +@Data +@Builder +public class WorkflowInstanceStatusChangeEvent { + + private String processInstanceId; + + private WorkflowInstanceStatusEnums status; + + private LocalDateTime endTime; + +} diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/FlowableJobEventListener.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/FlowableJobEventListener.java index 5c6c2a0f..7efa1f58 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/FlowableJobEventListener.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/FlowableJobEventListener.java @@ -1,59 +1,55 @@ package com.qqchen.deploy.backend.workflow.listener; +import cn.hutool.core.date.DateUtil; import com.qqchen.deploy.backend.workflow.enums.WorkflowInstanceStatusEnums; -import com.qqchen.deploy.backend.workflow.enums.WorkflowNodeInstanceStatusEnums; -import com.qqchen.deploy.backend.workflow.service.IWorkflowInstanceService; -import com.qqchen.deploy.backend.workflow.service.IWorkflowNodeInstanceService; +import com.qqchen.deploy.backend.workflow.event.WorkflowInstanceStatusChangeEvent; import jakarta.annotation.Resource; import lombok.extern.slf4j.Slf4j; import org.flowable.common.engine.api.delegate.event.FlowableEngineEntityEvent; import org.flowable.common.engine.api.delegate.event.FlowableEvent; import org.flowable.common.engine.api.delegate.event.FlowableEventListener; import org.flowable.common.engine.api.delegate.event.FlowableEventType; +import org.flowable.engine.HistoryService; +import org.flowable.engine.history.HistoricProcessInstance; +import org.springframework.context.ApplicationEventPublisher; import org.springframework.context.annotation.Lazy; -import org.springframework.context.event.EventListener; import org.springframework.stereotype.Component; -import org.springframework.transaction.event.TransactionPhase; -import org.springframework.transaction.event.TransactionalEventListener; @Component @Slf4j public class FlowableJobEventListener implements FlowableEventListener { + @Resource - @Lazy - private IWorkflowInstanceService workflowInstanceService; + private ApplicationEventPublisher publisher; @Resource @Lazy - private IWorkflowNodeInstanceService workflowNodeInstanceService; + private HistoryService historyService; + @Override - @EventListener - @TransactionalEventListener(phase = TransactionPhase.AFTER_COMMIT, fallbackExecution = true) public void onEvent(FlowableEvent event) { - log.info("FlowableJobEventListener: {}", event); FlowableEventType eventType = event.getType(); if (isProcessLevelEvent(eventType.name())) { + FlowableEngineEntityEvent entity = (FlowableEngineEntityEvent) event; + String processInstanceId = entity.getProcessInstanceId(); WorkflowInstanceStatusEnums status = convertProcessLevelEventToStatus(eventType.name()); - FlowableEngineEntityEvent entity = (FlowableEngineEntityEvent) event; - log.info("Process level event received: {} -> {}, processInstanceId: {}", eventType, status, entity.getProcessInstanceId()); - workflowInstanceService.updateInstanceStatus(entity.getProcessInstanceId(), status); - return; - } - - if (isActivityInstanceEvent(eventType.name())) { - FlowableEngineEntityEvent entity = (FlowableEngineEntityEvent) event; - log.info("Activity level event received: {}, processInstanceId: {}", eventType, entity.getProcessInstanceId()); - WorkflowNodeInstanceStatusEnums status = convertActivityEventToStatus(eventType.name()); - if (status != null) { - workflowNodeInstanceService.updateNodeStatus( - entity.getProcessInstanceId(), - entity.getExecutionId(), - status - ); - } + log.info("Process level event received: {} -> {}, processInstanceId: {}", eventType, status, processInstanceId); + HistoricProcessInstance historicProcessInstance = historyService.createHistoricProcessInstanceQuery().processInstanceId(processInstanceId).singleResult(); + log.info("historicProcessInstance: {}", historicProcessInstance.getEndTime()); + publisher.publishEvent(WorkflowInstanceStatusChangeEvent.builder() + .processInstanceId(processInstanceId) + .status(status) + .endTime(DateUtil.toLocalDateTime(historicProcessInstance.getEndTime())) + .build() + ); } +// +// if (isActivityInstanceEvent(flowableEvent.name())) { +// System.out.println("节点:" + flowableEvent.name()); +// return; +// } } @Override @@ -63,22 +59,18 @@ public class FlowableJobEventListener implements FlowableEventListener { @Override public boolean isFireOnTransactionLifecycleEvent() { - return false; + return true; } @Override public String getOnTransaction() { - return ""; + return "committed"; } private boolean isProcessLevelEvent(String eventType) { return eventType.startsWith("PROCESS_"); } - private boolean isActivityInstanceEvent(String eventType) { - return eventType.startsWith("ACTIVITY_"); - } - /** * 将Flowable流程级别事件类型转换为工作流实例状态 * 只处理流程实例级别的事件,不处理活动节点级别的事件 @@ -121,29 +113,4 @@ public class FlowableJobEventListener implements FlowableEventListener { return null; } } - - /** - * 将Flowable活动节点事件类型转换为工作流节点实例状态 - * - * @param eventType Flowable事件类型 - * @return 工作流节点实例状态枚举,如果不是节点级别事件则返回null - */ - private WorkflowNodeInstanceStatusEnums convertActivityEventToStatus(String eventType) { - switch (eventType) { - case "ACTIVITY_STARTED": - return WorkflowNodeInstanceStatusEnums.RUNNING; - - case "ACTIVITY_COMPLETED": - return WorkflowNodeInstanceStatusEnums.COMPLETED; - - case "ACTIVITY_CANCELLED": - return WorkflowNodeInstanceStatusEnums.TERMINATED; - - case "ACTIVITY_ERROR_RECEIVED": - return WorkflowNodeInstanceStatusEnums.FAILED; - - default: - return null; - } - } } diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/WorkflowInstanceStatusChangeListener.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/WorkflowInstanceStatusChangeListener.java new file mode 100644 index 00000000..04a70afa --- /dev/null +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/listener/WorkflowInstanceStatusChangeListener.java @@ -0,0 +1,25 @@ +package com.qqchen.deploy.backend.workflow.listener; + +import com.qqchen.deploy.backend.workflow.event.WorkflowInstanceStatusChangeEvent; +import com.qqchen.deploy.backend.workflow.service.IWorkflowInstanceService; +import jakarta.annotation.Resource; +import lombok.extern.slf4j.Slf4j; +import org.springframework.context.event.EventListener; +import org.springframework.stereotype.Component; +import org.springframework.transaction.annotation.Propagation; +import org.springframework.transaction.annotation.Transactional; + +@Slf4j +@Component +public class WorkflowInstanceStatusChangeListener { + + @Resource + private IWorkflowInstanceService workflowInstanceService; + + @EventListener + @Transactional(propagation = Propagation.REQUIRES_NEW) + public void handleWorkflowStatusChange(WorkflowInstanceStatusChangeEvent event) { + log.info("Handling workflow status change event: {}", event); + workflowInstanceService.updateInstanceStatus(event.getProcessInstanceId(), event.getStatus(), event.getEndTime()); + } +} diff --git a/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/IWorkflowInstanceService.java b/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/IWorkflowInstanceService.java index 556f7658..67f20fd9 100644 --- a/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/IWorkflowInstanceService.java +++ b/backend/src/main/java/com/qqchen/deploy/backend/workflow/service/IWorkflowInstanceService.java @@ -8,6 +8,8 @@ import com.qqchen.deploy.backend.workflow.entity.WorkflowInstance; import com.qqchen.deploy.backend.workflow.enums.WorkflowInstanceStatusEnums; import org.flowable.engine.runtime.ProcessInstance; +import java.time.LocalDateTime; +import java.util.Date; import java.util.List; import java.util.Map; @@ -28,7 +30,7 @@ public interface IWorkflowInstanceService extends IBaseService new RuntimeException("Workflow instance not found: " + processInstanceId)); - instance.setStatus(status); + instance.setEndTime(endTime); return workflowInstanceRepository.save(instance); }