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

相关推荐
惊讶的猫4 小时前
AMQP 与 RabbitMQ 四大模型
分布式·rabbitmq
灰子学技术4 小时前
istio从0到1:如何解决分布式配置同步问题
分布式·云原生·istio
小马爱打代码5 小时前
ZooKeeper:入门实战
分布式·zookeeper·云原生
永远都不秃头的程序员(互关)6 小时前
CANN赋能AIGC分布式训练:硬核通信,加速大模型智能生成新纪元
分布式·aigc
像少年啦飞驰点、6 小时前
从零开始学 RabbitMQ:小白也能懂的消息队列实战指南
java·spring boot·微服务·消息队列·rabbitmq·异步编程
lekami_兰6 小时前
RabbitMQ 延迟队列实现指南:两种方案手把手教你搞定
后端·rabbitmq·延迟队列
杜子不疼.7 小时前
CANN集合通信库HCCL的大规模分布式训练通信优化与拓扑感知实践
分布式
ALex_zry19 小时前
Redis Cluster 分布式缓存架构设计与实践
redis·分布式·缓存
为什么不问问神奇的海螺呢丶21 小时前
n9e categraf rabbitmq监控配置
分布式·rabbitmq·ruby
TTBIGDATA1 天前
【Atlas】Atlas Hook 消费 Kafka 报错:GroupAuthorizationException
hadoop·分布式·kafka·ambari·hdp·linq·ranger