当前位置: 首页 > news >正文

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();}}}
http://www.lryc.cn/news/385882.html

相关文章:

  • 数据恢复篇:如何在Android上恢复删除的短信
  • 花了大几万的踩坑经验!宠物空气净化器哪个牌子好:希喂、小米、有哈PK
  • 查普曼大学团队使用惯性动捕系统制作动画短片
  • vue 代理
  • [leetcode]24-game
  • 网络爬虫的原理
  • 游戏AI的创造思路-技术基础-机器学习(2)
  • 【深度学习】记录为什么没有调用GPU
  • vite 创建vue3项目 集成 ESLint、Prettier、Sass等
  • 计算机系统基础知识(上)
  • [深度学习]循环神经网络RNN
  • 【C++:list】
  • 解锁 Apple M1/M2 上的深度学习力量:安装 TensorFlow 完全指南
  • Apache Iceberg:现代数据湖存储格式的未来
  • 【离散数学·图论】(复习)
  • 【ONLYOFFICE震撼8.1】ONLYOFFICE8.1版本桌面编辑器测评
  • Shell 脚本编程保姆级教程(上)
  • 凸优化相关文章汇总
  • Java鲜花下单预约系统源码小程序源码
  • 网络变压器和RJ45接线的方法
  • Matlab/simulink三段式电流保护
  • OOXML入门学习
  • k8s集群node节点加入失败
  • layui+jsp项目中实现table单元格嵌入下拉选择框功能,下拉选择框可手动输入内容或选择默认值,修改后数据正常回显。
  • 2024年客户体验的几个预测
  • 【C++】动态内存管理new和delete
  • Java面向对象特性
  • odoo17 tree视图添加按钮
  • PreparedStatement 与Statement 的区别,以及为什么推荐使用 PreparedStatement ?
  • wsl ubuntu 安装Anaconda3步骤