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 小时前
Kafka 生产者与消费者分区策略全解析:从原理到实践
分布式·kafka
会飞的架狗师4 小时前
【Kafka系列】第二篇| Kafka 的核心概念、架构设计、底层原理
分布式·kafka
lifallen5 小时前
Hadoop MapReduce过程
大数据·数据结构·hadoop·分布式·apache
婷儿z12 小时前
部署 Zabbix 企业级分布式监控
分布式·zabbix
abigalexy14 小时前
百万并发QPS-分布式架构设计
分布式·架构
@Jackasher16 小时前
Redis如何实现一个分布式锁?
redis·分布式·wpf
hqxstudying1 天前
java分布式定时任务
java·开发语言·分布式
前端世界1 天前
鸿蒙分布式任务调度深度剖析:跨设备并行计算的最佳实践
分布式·华为·harmonyos
在未来等你1 天前
RabbitMQ面试精讲 Day 16:生产者优化策略与实践
中间件·面试·消息队列·rabbitmq
巴里巴气1 天前
kafka架构原理快速入门
分布式·kafka