Yes, ActiveMQ is a message broker based on JMS, which at its core, is designed to guarantee reliable delivery of messages. To understand this better, let us consider the message path through the system, i.e. from production to consumption.
The message is usually delivered in two hops. The first hop is where the message is transmitted from the producer to a destination on the message broker, in this case, ActiveMQ. In the second hop, the message is transmitted from a physical destination on the broker to the consumer.
During this process, there are only three ways through which a message can get lost in its path. The first is as it moves from the producer to the message broker. The second possible loss-point is in the broker memory, and lastly, on its path from the broker to the consumer.
ActiveMQ uses two mechanisms to ensure messages are not lost in any of the three ways. The first is that the broker stores all messages in a highly persistent data store. If the system is interrupted or the broker memory fails before the message is successfully delivered, the broker can still retrieve the message upon recovery and retry the operation.
Secondly, ActiveMQ uses acknowledgments and transactions to ensure that both production and consumption of messages is successful. Even with these two approaches, there can be situations when a message is not delivered successfully. For instance, when the transacted session for consuming the messages is rolled back. When this occurs, the message is returned to the queue and redelivered.
ActiveMQ provides two ways to deal with messages that are returned to the queue and redelivered without success. These are delayed redelivery and dead letter address.
For delayed redelivery, messages are delayed for a while to allow the consumer to recover from transient failures and prevent network or system overload. Here’s an example showing how you can define delayed redelivery in the address-setting configuration.
<address-setting match="exampleQueue">
<redelivery-delay-multiplier>1.5</redelivery-delay-multiplier>
<redelivery-delay>5000</redelivery-delay>
<redelivery-collision-avoidance-factor>0.15</redelivery-collision-avoidance-factor>
<max-redelivery-delay>50000</max-redelivery-delay>
</address-setting>
For dead letter address, messages in the unsuccessful deliveries are removed from the queue and sent to a dead letter address. The broker does not attempt to redeliver such messages but places them in a queue where the system administrator can access and take action.
Below is a simple configuration of a dead letter address.
<address-setting match="exampleQueue">
<dead-letter-address>deadLetterAddress</dead-letter-address>
<max-delivery-attempts>3</max-delivery-attempts>
</address-setting>