博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
【RabbitMQ】6、rabbitmq生产者的消息确认
阅读量:6236 次
发布时间:2019-06-22

本文共 3071 字,大约阅读时间需要 10 分钟。

通过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. 

地址:

根据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. 

通过Spring amqp文档可以看到,要使用这种机制需要将Template模版的设publisherConfirms 或publisherReturns 属性设置为true,此外ConnectionFactory要配置为CachingConnectionFactory。

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,消息确认监听器 */@Servicepublic class ConfirmCallBackListener implements RabbitTemplate.ConfirmCallback {    private Logger log = LoggerFactory.getLogger(CommonProducer.class);    @Override    public void 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,消息发送失败返回监听器 */@Servicepublic class ReturnCallBackListener implements RabbitTemplate.ReturnCallback {    private Logger log = LoggerFactory.getLogger(CommonProducer.class);    @Override    public void 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);    }}

使用场景:

如果消息没有到exchange,则confirm回调,ack=false

如果消息到达exchange,则confirm回调,ack=true

exchange到queue成功,则不回调return

exchange到queue失败,则回调return(需设置mandatory=true,否则不回回调,消息就丢了)

转载地址:http://vxzia.baihongyu.com/

你可能感兴趣的文章
老码农教你学英语
查看>>
博客转发小工具2
查看>>
simple_html_dom使用小结
查看>>
Unity手游之路<七>角色控制器
查看>>
puma vs passenger vs rainbows! vs unicorn vs thin 适用场景 及 performance
查看>>
js中的总结汇总(以后的都收集到这篇)
查看>>
QQ左侧滑动显示
查看>>
sql server中局部变量与全局变量的 申明与赋值(转)
查看>>
从无线安全到内网渗透
查看>>
Xamarin iOS教程之申请付费开发者账号下载证书
查看>>
!+"\v1" 用来“判断浏览器类型”还是用来“IE判断版本”的问题!
查看>>
javascript之Style物
查看>>
C# 公历转农历
查看>>
LeetCode - Divide Two Integers
查看>>
去掉 “当前安全设置会使计算机有风险”提示
查看>>
sql 聚合函数
查看>>
ABP源码分析二十:ApplicationService
查看>>
学习OpenCV——BOW特征提取函数(特征点篇)
查看>>
帮你店铺日销千单的20个淘宝小技巧
查看>>
python deep copy and shallow copy
查看>>