fix: wms任务完成取消消费者
This commit is contained in:
@@ -1,37 +1,31 @@
|
|||||||
package cn.code.nl.framework.execute.core;
|
package cn.code.nl.framework.execute.core;
|
||||||
|
|
||||||
import org.springframework.beans.BeansException;
|
import org.springframework.beans.BeansException;
|
||||||
import org.springframework.beans.factory.annotation.Autowired;
|
|
||||||
import org.springframework.beans.factory.config.BeanPostProcessor;
|
import org.springframework.beans.factory.config.BeanPostProcessor;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
import java.util.HashMap;
|
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.concurrent.ConcurrentHashMap;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
*
|
|
||||||
* @Author: liyongde
|
* @Author: liyongde
|
||||||
* @Date: 2026/7/15 15:32
|
* @Date: 2026/7/15 15:32
|
||||||
*/
|
*/
|
||||||
@Component
|
@Component
|
||||||
public class TaskFactory implements BeanPostProcessor {
|
public class TaskFactory implements BeanPostProcessor {
|
||||||
private final Map<String, AbstractTask> taskMap;
|
|
||||||
|
|
||||||
@Autowired
|
private final Map<String, AbstractTask> taskMap = new ConcurrentHashMap<>();
|
||||||
public TaskFactory() {
|
|
||||||
taskMap = new HashMap<>();
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
public Object postProcessAfterInitialization(Object bean, String beanName) throws BeansException {
|
||||||
if (bean instanceof AbstractTask) {
|
if (bean instanceof AbstractTask task) {
|
||||||
taskMap.put(beanName, (AbstractTask) bean);
|
taskMap.put(beanName, task);
|
||||||
}
|
}
|
||||||
return bean;
|
return bean;
|
||||||
}
|
}
|
||||||
|
|
||||||
public AbstractTask getTask(String handleCode) {
|
public AbstractTask getTask(String handleCode) {
|
||||||
if (handleCode == null) {
|
if (handleCode == null || handleCode.isBlank()) {
|
||||||
return null;
|
return null;
|
||||||
}
|
}
|
||||||
return taskMap.get(handleCode);
|
return taskMap.get(handleCode);
|
||||||
|
|||||||
@@ -10,10 +10,15 @@ import java.util.Map;
|
|||||||
@Data
|
@Data
|
||||||
public class TaskEventMessage {
|
public class TaskEventMessage {
|
||||||
|
|
||||||
|
/** 任务完成 */
|
||||||
|
public static final String EVENT_TYPE_FINISHED = "TASK_FINISHED";
|
||||||
|
/** 任务取消 */
|
||||||
|
public static final String EVENT_TYPE_CANCELLED = "TASK_CANCELLED";
|
||||||
|
|
||||||
/** 事件ID */
|
/** 事件ID */
|
||||||
private String eventId;
|
private String eventId;
|
||||||
|
|
||||||
/** 事件类型:TASK_FINISHED / TASK_CANCELLED */
|
/** 事件类型,取值:{@link #EVENT_TYPE_FINISHED} / {@link #EVENT_TYPE_CANCELLED} */
|
||||||
private String eventType;
|
private String eventType;
|
||||||
|
|
||||||
/** 任务ID */
|
/** 任务ID */
|
||||||
|
|||||||
@@ -37,6 +37,11 @@
|
|||||||
<artifactId>nl-module-system-api</artifactId>
|
<artifactId>nl-module-system-api</artifactId>
|
||||||
<version>${revision}</version>
|
<version>${revision}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>cn.nl.cloud</groupId>
|
||||||
|
<artifactId>nl-module-task-api</artifactId>
|
||||||
|
<version>${revision}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- 业务组件 -->
|
<!-- 业务组件 -->
|
||||||
<dependency>
|
<dependency>
|
||||||
|
|||||||
@@ -1,24 +1,54 @@
|
|||||||
package cn.code.nl.module.wms.mq.consumer;
|
package cn.code.nl.module.wms.mq.consumer;
|
||||||
|
|
||||||
|
import cn.code.nl.framework.execute.core.AbstractTask;
|
||||||
|
import cn.code.nl.framework.execute.core.TaskFactory;
|
||||||
|
import cn.code.nl.framework.execute.core.dto.TaskExecuteDTO;
|
||||||
|
import cn.code.nl.module.task.message.TaskEventMessage;
|
||||||
|
import jakarta.annotation.Resource;
|
||||||
import lombok.extern.slf4j.Slf4j;
|
import lombok.extern.slf4j.Slf4j;
|
||||||
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
import org.apache.rocketmq.spring.annotation.RocketMQMessageListener;
|
||||||
import org.apache.rocketmq.spring.core.RocketMQListener;
|
import org.apache.rocketmq.spring.core.RocketMQListener;
|
||||||
import org.springframework.stereotype.Component;
|
import org.springframework.stereotype.Component;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 监听任务状态变更
|
* 监听任务状态变更,根据 handleCode 定位任务子类并执行完成/取消逻辑
|
||||||
|
*
|
||||||
* @Author: liyongde
|
* @Author: liyongde
|
||||||
* @Date: 2026/7/20 10:28
|
* @Date: 2026/7/20 10:28
|
||||||
*/
|
*/
|
||||||
@Slf4j
|
@Slf4j
|
||||||
@Component
|
@Component
|
||||||
@RocketMQMessageListener(topic = "${rocketmq.topic.wms-task-status-change}",
|
@RocketMQMessageListener(
|
||||||
consumerGroup = "${rocketmq.push-consumer.group.wms-task-status-change}",
|
topic = "${rocketmq.consumer.wms-task-operate.topic}",
|
||||||
namespace = "${rocketmq.push-consumer.namespace}")
|
consumerGroup = "${rocketmq.consumer.wms-task-operate.group}"
|
||||||
public class TaskStatusChangeConsumer implements RocketMQListener {
|
)
|
||||||
|
public class TaskStatusChangeConsumer implements RocketMQListener<TaskEventMessage> {
|
||||||
|
|
||||||
|
@Resource
|
||||||
|
private TaskFactory taskFactory;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onMessage(Object o) {
|
public void onMessage(TaskEventMessage message) {
|
||||||
|
String handleCode = message.getHandleCode();
|
||||||
|
String eventType = message.getEventType();
|
||||||
|
log.info("收到任务状态变更消息, handleCode={}, eventType={}, taskId={}", handleCode, eventType, message.getTaskId());
|
||||||
|
|
||||||
|
AbstractTask task = taskFactory.getTask(handleCode);
|
||||||
|
if (task == null) {
|
||||||
|
log.warn("未找到对应任务处理器, handleCode={}", handleCode);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
TaskExecuteDTO dto = new TaskExecuteDTO();
|
||||||
|
dto.setTaskId(message.getTaskId());
|
||||||
|
dto.setPayload(message.getPayload());
|
||||||
|
|
||||||
|
if (TaskEventMessage.EVENT_TYPE_FINISHED.equals(eventType)) {
|
||||||
|
task.doHandleFinish(dto);
|
||||||
|
} else if (TaskEventMessage.EVENT_TYPE_CANCELLED.equals(eventType)) {
|
||||||
|
task.doHandleCancel(dto);
|
||||||
|
} else {
|
||||||
|
log.warn("未知事件类型, eventType={}, handleCode={}", eventType, handleCode);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -75,11 +75,6 @@ spring:
|
|||||||
# password: 123456 # 密码,建议生产环境开启
|
# password: 123456 # 密码,建议生产环境开启
|
||||||
|
|
||||||
--- #################### MQ 消息队列相关配置 ####################
|
--- #################### MQ 消息队列相关配置 ####################
|
||||||
|
|
||||||
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
|
||||||
rocketmq:
|
|
||||||
name-server: 127.0.0.1:9876 # RocketMQ Namesrv
|
|
||||||
|
|
||||||
spring:
|
spring:
|
||||||
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
# RabbitMQ 配置项,对应 RabbitProperties 配置类
|
||||||
rabbitmq:
|
rabbitmq:
|
||||||
|
|||||||
@@ -0,0 +1,8 @@
|
|||||||
|
--- #################### MQ 消息队列相关配置 ####################
|
||||||
|
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||||
|
rocketmq:
|
||||||
|
name-server: 127.0.0.1:9876
|
||||||
|
consumer:
|
||||||
|
wms-task-operate:
|
||||||
|
group: wms-task-status-change-dev-group
|
||||||
|
topic: wms-task-status-change-dev-topic
|
||||||
@@ -0,0 +1,8 @@
|
|||||||
|
--- #################### MQ 消息队列相关配置 ####################
|
||||||
|
# rocketmq 配置项,对应 RocketMQProperties 配置类
|
||||||
|
rocketmq:
|
||||||
|
name-server: 127.0.0.1:9876
|
||||||
|
consumer:
|
||||||
|
wms-task-operate:
|
||||||
|
group: wms-task-status-change-test-group
|
||||||
|
topic: wms-task-status-change-test-topic
|
||||||
@@ -13,6 +13,7 @@ spring:
|
|||||||
import:
|
import:
|
||||||
- optional:classpath:application-${spring.profiles.active}.yaml # 加载【本地】配置
|
- optional:classpath:application-${spring.profiles.active}.yaml # 加载【本地】配置
|
||||||
- optional:nacos:${spring.application.name}-${spring.profiles.active}.yaml # 加载【Nacos】的配置
|
- optional:nacos:${spring.application.name}-${spring.profiles.active}.yaml # 加载【Nacos】的配置
|
||||||
|
- optional:classpath:application-mq-${spring.profiles.active}.yaml # 加载 MQ 配置
|
||||||
|
|
||||||
# Servlet 配置
|
# Servlet 配置
|
||||||
servlet:
|
servlet:
|
||||||
|
|||||||
Reference in New Issue
Block a user