Overview
Messages are redelivered to a client when:
- A transacted session is used and rollback() is called.
- A transacted session is closed before commit is called.
- A session is using CLIENT_ACKNOWLEDGE and Session.recover() is called.
Behavior in the 3.x brokers
Acks/Nacks would be sent to the broker for each message that need to be redelivered. The broker would then re-deliver the messages to a consumer. The broker could implment broker side redelivery process as he was aware of when messages were being redelivered to the client. He could send the message to a DLQ after a specified number of failed deliverys.
Behavior in the 4.x brokers
Acks are not sent to the broker till the last possible moment. In the transacted case, that's before the transaction commits. A single ranged ack is sent to the broker instead of multiple acks for each message consumed. On rollback, since nothing has been acked yet, and all messages are still available in an internal consumer queue, the messages are re-dispatched from the internal consumer queue. This reduces redelivery dispatch overhead, at the cost of the broker not being aware that redeliveries are occuring. Therefore the way Redeliverys and DQL handling is done needs to be rearchitected in the 4.x brokers.
Another bonus of this approach is that re-tried messages can preserve order very easily (which is really hard to do in the 3.x version)
The broker transmits the default delivery policy that he prefers to a client connection in his BrokerInfo command packet. But the client can override the policy settings by using the ActiveMQConnection.getRedeliveryPolicy() method:
RedeliveryPolicy policy = connection.getRedeliveryPolicy();
policy.setInitialRedeliveryDelay(500);
policy.setBackOffMultiplier(2);
policy.setUseExponentialBackOff(true);
policy.setMaximumRedeliveries(2);
Once a message's redelivery attempts exceeds the maximumRedeliveries configured for the RedeliveryPolicy, a "Poison ack" is sent back to the broker letting him know that the message was considered a poison pill. The Broker then takes the message and sends it to a Dead Letter Queue so that it can be analyzed later on.