[RabbitMQ] 6. Confirm the message of the rabbitmq producer and the rabbitmq producer.

Source: Internet
Author: User

[RabbitMQ] 6. Confirm the message of the rabbitmq producer and the rabbitmq producer.

Through the Publisher Confirms and Returns mechanism, the producer can determine whether the message is sent to exchange and queue. Through the consumer confirmation mechanism, Rabbitmq can decide whether to resend the message to the consumer to ensure that the message is processed.

1. What is 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.
Address: http://www.rabbitmq.com/confirms.html

According to the official website of RabbitMq, the confirmation of the rabbitmq broker to the publisher (publishers) is called the publisher confirmation (publisher confirms). This mechanism is the extension of Rabbitmq to the standard Amqp protocol. Therefore, you can confirm whether the message is sent to the target.

2. How to Use the Publisher Confirms and Returns mechanism through Spring amqp?

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

The Spring amqp document shows that to use this mechanism, you must set the attribute publisherConfirms or publisherReturns of the Template to true, and set ConnectionFactory to 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 use and trigger of 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 login /10/31. * @ description: inherits RabbitTemplate. confirmCallback, message confirmation listener */@ 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 ("received callback, successfully sent to broker ");}}

Use Cases:

2.2 Use and trigger of 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 login /10/31. * @ description: inherits RabbitTemplate. returnCallback: The Listener */@ Servicepublic class ReturnCallBackListener implements RabbitTemplate is returned if the message fails to be sent. returnCallback {private Logger log = LoggerFactory. getLogger (CommonProducer. class); @ Override public void returnedMessage (Message message, int replyCode, String replyText, String exchange, String routingKey) {log.info ("receive callback"); log.info ("return -- message: "+ new String (message. getBody () + ", replyCode:" + replyCode + ", replyText:" + replyText + ", exchange:" + exchange + ", routingKey:" + routingKey );}}

Use Cases:

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.