欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 科技 > 能源 > RabbitMQ中CorrelationData 与DeliveryTag的区别

RabbitMQ中CorrelationData 与DeliveryTag的区别

2024/10/24 4:49:44 来源:https://blog.csdn.net/qq_39666711/article/details/140014300  浏览:    关键词: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的消息确认机制相结合,为消息的可靠传递和处理提供了重要支持。

例子:

@Service
public class TestServiceImpl implements TestService {@Autowiredprivate MsgLogMapper msgLogMapper;@Autowiredprivate RabbitTemplate rabbitTemplate;@Overridepublic 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的一个消息标识(这个仅仅是消息的标识,和业务没关系)
使用如下:

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();}}}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com