【RabbitMQ】6、rabbitmq生产者的消息确认
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了【RabbitMQ】6、rabbitmq生产者的消息确认,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3426字,纯文字阅读大概需要5分钟。
内容图文
通过Publisher Confirms and Returns机制,生产者可以判断消息是否发送到了exchange及queue,而通过消费者确认机制,Rabbitmq可以决定是否重发消息给消费者,以保证消息被处理。
1.什么是Publisher Confirms and Returns?
Delivery processing acknowledgements from consumers to RabbitMQ are known as acknowledgements in AMQP 0-9-1 parlance; broker acknowledgements to publishers are a protocol extension called publisher confirms.
地址:http://www.rabbitmq.com/confirms.html
根据RabbitMq官网定义,rabbitmq代理(broker)对发布者(publishers)的确认被称作发布者确认(publisher confirms),这种机制是Rabbitmq对标准Amqp协议的扩展。因此通过这种机制可以确认消息是否发送给了目标。
2.如何通过Spring amqp来使用Publisher Confirms and Returns机制?
Confirmed and returned messages are supported by setting the CachingConnectionFactory’s publisherConfirms and publisherReturns properties to ‘true’ respectively.When these options are set, Channel s created by the factory are wrapped in an PublisherCallbackChannel, which is used to facilitate the callbacks. When such a channel is obtained, the client can register a PublisherCallbackChannel.Listener with the Channel. The PublisherCallbackChannel implementation contains logic to route a confirm/return to the appropriate listener. These features are explained further in the following sections.
http://docs.spring.io/spring-amqp/docs/1.6.3.RELEASE/reference/html/_reference.html#cf-pub-conf-ret
通过Spring amqp文档可以看到,要使用这种机制需要将Template模版的设publisherConfirms 或publisherReturns 属性设置为true,此外ConnectionFactory要配置为CachingConnectionFactory。
<bean id="connectionFactory"
class="org.springframework.amqp.rabbit.connection.CachingConnectionFactory">
<property name="host" value="192.168.2.133" />
<property name="port" value="5672" />
<property name="username" value="sun" />
<property name="password" value="123456" />
<property name="publisherConfirms" value="true" />
<property name="publisherReturns" value="true" />
</bean>
2.1 ConfirmCallback的使用及触发的一种场景
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.amqp.rabbit.support.CorrelationData; import org.springframework.stereotype.Service; /** * @author wangzhongqiu * Created on 2017/10/31. * @description:继承RabbitTemplate.ConfirmCallback,消息确认监听器 */ @Service public class ConfirmCallBackListener implements RabbitTemplate.ConfirmCallback { private Logger log = LoggerFactory.getLogger(CommonProducer.class); @Override publicvoid confirm(CorrelationData correlationData, boolean ack, String cause) { log.info("收到回调,成功发送到broker"); } }
使用场景:
2.2 ReturnCallback的使用及触发的一种场景
import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.springframework.amqp.core.Message; import org.springframework.amqp.rabbit.core.RabbitTemplate; import org.springframework.stereotype.Service; /** * @author wangzhongqiu * Created on 2017/10/31. * @description:继承RabbitTemplate.ReturnCallback,消息发送失败返回监听器 */ @Service public class ReturnCallBackListener implements RabbitTemplate.ReturnCallback { private Logger log = LoggerFactory.getLogger(CommonProducer.class); @Override publicvoid returnedMessage(Message message, int replyCode, String replyText, String exchange, String routingKey) { log.info("收到回调"); log.info("return--message:" + new String(message.getBody()) + ",replyCode:" + replyCode + ",replyText:" + replyText + ",exchange:" + exchange + ",routingKey:" + routingKey); } }
使用场景:
原文:http://www.cnblogs.com/wangzhongqiu/p/7815529.html
内容总结
以上是互联网集市为您收集整理的【RabbitMQ】6、rabbitmq生产者的消息确认全部内容,希望文章能够帮你解决【RabbitMQ】6、rabbitmq生产者的消息确认所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。