RabbitMQ中CorrelationData 与DeliveryTag的区别

在RabbitMQ中,CorrelationData是一个用于封装业务ID信息的类,它主要在消息确认机制中发挥作用。以下是关于CorrelationData在RabbitMQ中的详细作用:

封装业务ID信息:

当发送消息时,可以将业务ID信息封装在CorrelationData对象中,并作为参数传递给消息发送方法。这样,在消息处理过程中,可以方便地追踪和识别与该消息相关的业务信息。
消息确认机制:

RabbitMQ支持消息确认机制,即生产者发送消息后,可以等待消费者的确认消息,以确保消息已成功被消费者处理。

CorrelationData在这种机制中起到关键作用。生产者发送消息时,可以将CorrelationData对象与消息一起发送。当消费者处理完消息后,可以通过CorrelationData中的业务ID来确认该消息。
唯一性标识:

CorrelationData对象内部通常包含一个id属性,用于表示当前消息的唯一性。这个唯一性标识可以在整个消息处理流程中保持不变,方便进行消息追踪和确认。
获取方式:

在消费者端,可以通过消息的headers属性来获取CorrelationData中的业务ID信息。例如,在Spring AMQP中,可以使用Message.getHeaders().get("spring_returned_message_correlation")来获取CorrelationData中的业务ID。
与DeliveryTag的区别:

DeliveryTag是RabbitMQ自动为每条消息生成的唯一标识,用于消息的确认和重试等机制。而CorrelationData则是业务层面上的唯一性标识,用于标识和追踪与特定业务相关的消息。

综上所述,CorrelationData在RabbitMQ中主要用于封装和传递与消息相关的业务ID信息,以便在消息处理过程中进行追踪和确认。它通过与RabbitMQ的消息确认机制相结合,为消息的可靠传递和处理提供了重要支持。

例子:

java 复制代码
@Service
public class TestServiceImpl implements TestService {
 
    @Autowired
    private MsgLogMapper msgLogMapper;
 
    @Autowired
    private RabbitTemplate rabbitTemplate;
 
    @Override
    public ServerResponse send(Mail mail) {
        String msgId = RandomUtil.UUID32();
        mail.setMsgId(msgId);
 
        MsgLog msgLog = new MsgLog(msgId, mail, RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME);
        msgLogMapper.insert(msgLog);// 消息入库
 
        CorrelationData correlationData = new CorrelationData(msgId);
        rabbitTemplate.convertAndSend(RabbitConfig.MAIL_EXCHANGE_NAME, RabbitConfig.MAIL_ROUTING_KEY_NAME, MessageHelper.objToMsg(mail), correlationData);// 发送消息
 
        return ServerResponse.success(ResponseCode.MAIL_SEND_SUCCESS.getMsg());
    }
 
}

其中的String msgId = RandomUtil.UUID32(); 是自己随机生成的编码,作为唯一的业务ID信息

对于DeliveryTag,则是在消息手动确认的时候,需要传给MQ的一个消息标识(这个仅仅是消息的标识,和业务没关系)

使用如下:

java 复制代码
package com.atguigu.gulimall.consumertrue.listener;

import com.rabbitmq.client.Channel;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.Exchange;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.QueueBinding;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.stereotype.Component;

/**
 *
 *此处用一个类下的两个方法来模拟消费者
 *
 * @author: jd
 * @create: 2024-06-25
 */
@Component
public class MyConsumerListener {

    @RabbitListener(bindings = {
            @QueueBinding(
                    value = @Queue("consumer_queue_2"),
                    //绑定交换机
                    exchange = @Exchange(value = "muscle_fanout_exchange", type = "fanout")
            )
    })
    public void consumer2(String msg,Message message, Channel channel) throws Exception {
        long deliveryTag = message.getMessageProperties().getDeliveryTag();
        try {
            System.out.println("消费者2 => " + msg);
            channel.basicAck(deliveryTag, false);  //手动确认 设置消息唯一标识
        } catch (Exception e) {
            channel.basicReject(deliveryTag, false);
            e.printStackTrace();
        }
    }


   


}
相关推荐
翱翔-蓝天1 小时前
Spring Boot 3 集成 RabbitMQ 实践指南
spring boot·rabbitmq·java-rabbitmq
Dolphin_Home1 小时前
搭建 Hadoop 3.3.6 伪分布式
大数据·hadoop·分布式
luckilyil1 小时前
RabbitMQ学习—day6—springboot整合
spring boot·rabbitmq·java-rabbitmq
姜来可期3 小时前
【分布式数据一致性算法】Gossip协议详解
分布式
begei3 小时前
理解vllm分布式推理服务中的多节点Multi-Node部署方式
分布式
南宫文凯3 小时前
Hadoop-HA(高可用)机制
大数据·hadoop·分布式·hadoop-ha
天天向上杰6 小时前
简识MQ之Kafka、ActiveMQ、RabbitMQ、RocketMQ传递机制
kafka·rabbitmq·rocketmq·activemq
一个假的前端男6 小时前
RabbitMQ 消息队列
分布式·rabbitmq
liruiqiang056 小时前
DDD - 整洁架构
分布式·微服务·架构
刀客1237 小时前
kafka基本知识
分布式·kafka