rabbitmq的confirm模式获取correlationData为null解决办法

回调函数confirm中的correlationData=null

复制代码
// 实现confirm回调,发送到和没发送到exchange,都触发
@Override
public void confirm(CorrelationData correlationData, boolean ack, String cause) {
    // 参数说明:
    // correlationData: 相关数据,可以在发送消息时,进行设置该参数
    // ack: 结果
    // cause: 原因

    if (ack) {
        log.info("【ConfirmCallback】消息已经送达Exchange,ack已发");

    } else {
        ReturnedMessage message = correlationData.getReturned();

        if (message != null) {
            String msgData = new String(message.getMessage().getBody());
            log.error("消息发送到 exchange {} 失败,原因: {},id: {}, routingKey: {},body: {}", message.getExchange(), cause, correlationData.getId(), message.getRoutingKey(), msgData);
        } else {
            log.error("消息发送 exchange 失败,原因: {},id: {}", correlationData.getId(),cause);
        }

    }
}

解决办法

在convertAndSend方法中传入correlationData数据

复制代码
@SpringBootTest
class RabbitmqDemoApplicationTests {

    @Test
    void contextLoads() {
    	// 模拟消息
        BattleSubmitMqVo msg = new BattleSubmitMqVo().setUserId(1L).setRoomId("123").setTimes(300L);
        // 工具类发送消息到mq
        MqUtil.sendMsgToMq(RabbitConfig.BATTLE_PAPER_EXCHANGE,RabbitConfig.BATTLE_PAPER_ROUTING_KEY, msg);

    }

}

工具类

复制代码
package com.example.rabbitmqdemo.util;

import cn.hutool.json.JSONUtil;
import com.sun.istack.internal.NotNull;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.core.MessagePostProcessor;
import org.springframework.amqp.core.ReturnedMessage;
import org.springframework.amqp.rabbit.connection.CorrelationData;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

/**
 * desc:
 *
 * @author qts
 * @date 2023/11/3 0003
 */
@Component
public class MqUtil {

    private static RabbitTemplate rabbitTemplate;

    @Autowired
    private RabbitTemplate rabbitTemplate2;

    @PostConstruct
    public void init(){
        rabbitTemplate = rabbitTemplate2;
    }

    /**
     * 发送消息并
     * 添加 CorrelationData数据
     * @param exchange
     * @param routingKey
     * @param msg
     */
    public static void sendMsgToMq(String exchange, String routingKey, Object msg){
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(new ReturnedMessage(new Message(JSONUtil.toJsonStr(msg).getBytes()),1,"1",exchange,routingKey));

        rabbitTemplate.convertAndSend(exchange,routingKey,msg,correlationData);
    }

    /**
     * 发送消息
     * 添加 CorrelationData数据, 消息后处理回调
     * @param exchange
     * @param routingKey
     * @param msg
     * @param messagePostProcessor 消息后处理回调
     */
    public static void sendMsgToMq(String exchange, String routingKey, Object msg,MessagePostProcessor messagePostProcessor){
        CorrelationData correlationData = new CorrelationData();
        correlationData.setReturned(new ReturnedMessage(new Message(JSONUtil.toJsonStr(msg).getBytes()),1,"1",exchange,routingKey));

        rabbitTemplate.convertAndSend(exchange,routingKey,msg,messagePostProcessor,correlationData);
    }
}

效果

得到了值

springboot集成rabbitmq

相关推荐
程序猿阿伟3 小时前
《分布式追踪Span-业务标识融合:端到端业务可观测手册》
分布式
消失的旧时光-19435 小时前
第十六课实战:分布式锁与限流设计 —— 从原理到可跑 Demo
redis·分布式·缓存
若水不如远方5 小时前
分布式一致性(三):共识的黎明——Quorum 机制与 Basic Paxos
分布式·后端·算法
会算数的⑨7 小时前
Kafka知识点问题驱动式的回顾与复习——(一)
分布式·后端·中间件·kafka
张小凡vip7 小时前
Kafka--使用 Kafka Connect 导入/导出数据
分布式·kafka
回忆是昨天里的海7 小时前
kafka概述
分布式·kafka
知识即是力量ol7 小时前
初识 Kafka(一):分布式流平台的定义、核心优势与架构全景
java·分布式·kafka·消息队列
nbsaas-boot7 小时前
Pipeline + Saga 分布式扩展规范
分布式
creator_Li7 小时前
分布式IM聊天系统的消息可靠性
分布式·im
一条闲鱼_mytube7 小时前
《分布式事务实战完全指南》:从理论到实践
分布式