Publisher (delivery) Confirms and Returns (Processing confirmation)
RMQ supports Publisher Confirms and Returns.
In the distributed architecture, the protocol method is not guaranteed that message reaches the peers. Both message broker and consumer needs a mechanism for delivery and processing confirmation
For returned messages, the template’s mandatory property must be set to true, or the mandatory-expression must evaluate to true for a particular message.
This feature requires a CachingConnectionFactory that has its publisherReturns property set to true (see the section called “Publisher Confirms and Returns”). Returns are sent to the client by it registering a RabbitTemplate.ReturnCallback by calling setReturnCallback(ReturnCallback callback). The callback must implement this method:
Publisher confirmation:
Network can fail due to many reason and detecting network failure may take time.
Messages can be lost on its way or delayed significantly.
-To guarantee the delivery, it requires to make it transactional.
-In exceptional case when broker is not able to send the message, it will send basic.nack.
-Confirms are when the broker sends an ack back to the publisher, indicating that a message was successfully routed.
Messages can be lost on its way or delayed significantly.
-To guarantee the delivery, it requires to make it transactional.
-In exceptional case when broker is not able to send the message, it will send basic.nack.
-Confirms are when the broker sends an ack back to the publisher, indicating that a message was successfully routed.
-Only one ConfirmCallback is supported by a RabbitTemplate.
-void confirm(CorrelationData correlationData, boolean ack, String cause);
-void returnedMessage(Message message, int replyCode, String replyText,
String exchange, String routingKey);
rabbitTemplate.setConfirmCallback(new RabbitTemplate.ConfirmCallback() {
@Override
public void confirm(CorrelationData correlationData, boolean b) {
System.err.println("confirmedMessage");
}
});
ReturnCallback:
Returns are when the broker returns a message because it's undeliverable (no matching bindings on the exchange to which the message was published, and the mandatory bit is set)
rabbitTemplate.setReturnCallback(new RabbitTemplate.ReturnCallback() {
@Override
public void returnedMessage(message, replyCode, replyText, exchange, routingKey) {
}
});
Positively Acknowledging Deliveries:
channel.basicAck(deliveryTag, false);
Acknowledging Multiple Deliveries at Once:
channel.basicAck(deliveryTag, true);
For example, given that there are delivery tags 5, 6, 7, and 8 unacknowledged on channel Ch, when an acknowledgement frame arrives on that channel with delivery_tag set to 8 and multiple set to true, all tags from 5 to 8 will be acknowledged. If multiple was set to false, deliveries 5, 6, and 7 would still be unacknowledged.
Negative Acknowledgement and Requeuing of Deliveries
// negatively acknowledge, the message will be discarded
channel.BasicReject(ea.DeliveryTag, false);
// requeue the delivery
channel.BasicReject(ea.DeliveryTag, true);
--requeue all unacknowledged deliveries up to this delivery tag
channel.basicNack(deliveryTag, true, true);
}
No comments:
Post a Comment