Confirmation message producer rabbitmq

By Publisher Confirms and Returns mechanism, producers can determine whether a message is sent to the exchange and queue, and the consumer by confirming mechanism, Rabbitmq can decide whether to resend the message to consumers to ensure that messages are processed.

1. What is the 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

The official RabbitMq net definition, rabbitmq agent (Broker) acknowledgment of the publisher (Publishers) is referred to as publisher acknowledgment (publisher confirms), this mechanism is Rabbitmq Amqp extension to the standard protocol. So it can confirm whether a message is sent to the target through this mechanism.

2. How to use Publisher Confirms and Returns mechanism by 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

Spring amqp can be seen through the document, to use this mechanism needs to be set publisherConfirms Template template or publisherReturns property is set to true, moreover ConnectionFactory to configure CachingConnectionFactory.

Copy the code
    <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>
Copy the code

Use and trigger a scenario of 2.1 ConfirmCallback

Copy the code
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 public Confirm void (CorrelationData correlationData, Boolean ACK, the cause is String) {log.info ( "receives a callback, to successfully send Broker" );}}
Copy the code

Use and trigger a scenario of 2.2 ReturnCallback

Copy the code
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 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); } }
Copy the code

scenes to be used:

If the message is not to exchange, then confirm the callback, ack = false

If the message reaches the exchange, then confirm the callback, ack = true

exchange to queue successfully, no callback return

exchange to queue fails, the callback return (to be set mandatory = true, otherwise it does not return the callback, the message lost)

 

Reprinted :: http://www.cnblogs.com/wangzhongqiu/

Guess you like

Origin www.cnblogs.com/leeego-123/p/11238428.html