|
@@ -1,12 +1,15 @@
|
|
|
package com.fdkankan.contro.mq.listener;
|
|
|
|
|
|
import com.alibaba.fastjson.JSONObject;
|
|
|
+import com.fdkankan.contro.constant.RedisConstants;
|
|
|
import com.fdkankan.contro.mq.service.IBuildSceneService;
|
|
|
import com.fdkankan.rabbitmq.bean.BuildSceneCallMessage;
|
|
|
import com.fdkankan.rabbitmq.bean.BuildSceneResultMqMessage;
|
|
|
+import com.fdkankan.redis.util.RedisLockUtil;
|
|
|
import com.rabbitmq.client.Channel;
|
|
|
import lombok.extern.slf4j.Slf4j;
|
|
|
import org.springframework.amqp.core.Message;
|
|
|
+import org.springframework.beans.factory.annotation.Autowired;
|
|
|
import org.springframework.util.ObjectUtils;
|
|
|
|
|
|
import java.io.IOException;
|
|
@@ -15,9 +18,23 @@ import java.util.HashMap;
|
|
|
|
|
|
@Slf4j
|
|
|
public class AbstrackBuildSceneListener implements IBuildSceneListener {
|
|
|
+
|
|
|
+ @Autowired
|
|
|
+ private RedisLockUtil redisLockUtil;
|
|
|
+
|
|
|
@Override
|
|
|
public void preHandle(Channel channel, String queueName, Message message, IBuildSceneService buildSceneService) throws IOException {
|
|
|
+ // 添加消息幂等处理
|
|
|
String messageId = message.getMessageProperties().getMessageId();
|
|
|
+ if(!ObjectUtils.isEmpty(messageId)){
|
|
|
+ // 设置消息id幂等性,防止消息重复消费
|
|
|
+ boolean lock = redisLockUtil.lock(RedisConstants.SCENE_PREPARE_BUILDING + messageId, 24 * 3600);
|
|
|
+ if (!lock) {
|
|
|
+ log.error("服务:{},消息重复消费:{}", "常驻服务", messageId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
|
|
|
log.info("开始准备场景计算资源,队列名:{},id:{},消息体:{}", queueName, messageId, msg);
|
|
|
BuildSceneCallMessage buildSceneMessage = JSONObject.parseObject(msg, BuildSceneCallMessage.class);
|
|
@@ -34,7 +51,17 @@ public class AbstrackBuildSceneListener implements IBuildSceneListener {
|
|
|
|
|
|
@Override
|
|
|
public void postHandle(Channel channel, String queueName, Message message, IBuildSceneService buildSceneService) throws Exception {
|
|
|
+ // 添加消息幂等处理
|
|
|
String messageId = message.getMessageProperties().getMessageId();
|
|
|
+ if(!ObjectUtils.isEmpty(messageId)){
|
|
|
+ // 设置消息id幂等性,防止消息重复消费
|
|
|
+ boolean lock = redisLockUtil.lock(RedisConstants.SCENE_POST_BUILDING + messageId, 24 * 3600);
|
|
|
+ if (!lock) {
|
|
|
+ log.error("服务:{},消息重复消费:{}", "常驻服务", messageId);
|
|
|
+ return;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
String msg = new String(message.getBody(), StandardCharsets.UTF_8);
|
|
|
log.info("场景计算完成,开始处理场景计算结果,队列名:{},id:{},消息体:{}", queueName, messageId, msg);
|
|
|
BuildSceneResultMqMessage buildSceneMessage = JSONObject.parseObject(msg, BuildSceneResultMqMessage.class);
|