This explains how you can set up delayed delivery in the broker.
Setting up delayed delivery for topics
When a topic has one or more subscriptions and when you need to apply a global delay to all the subscriptions of it, you can set it up in the broker by defining global policies.
Setting up delayed delivery for subscribers
The client who connects can override the global policies you set up for topics. When configuring the
java.naming.provider.url parameter in the JMS Inbound Endpoint, you can pass the re-delivery policy property as a query String. For a detailed description on the policy properties, go to Apache ActiveMQ Documentation.
For example, when configuring a Script mediator, if you need to rollback the message four times upon an error with a one second delay, set the
java.naming.provider.url property as follows.
redeliveryDelay property specifies the re-delivery delay in milliseconds and the
maximumRedeliveries property defines the number of times the retry should occur. For more information on these properties, go to Apache ActiveMQ Documentation.
In ActiveMQ 5.9.0, although the broker properly identifies the re-delivery times, the broker does not delay the messages as expected. Thus, it will not enforce even the global level delay.
The final Inbound Endpoint configuration with the re-delivery delay and number of times you defined will be as follows:
Enforcing a delay per message
There are not an out of the box feature available in ActiveMQ to enforce delay per message. The re-delivery policies are defined by the Initial Context Factory when it establishes the connection and it does not happen on a per message basis.
However, before rolling back, you can enforce a delay using the Script mediator in the error sequence as follows.
Then, the sample sequence is as follows.
The Script mediator holds the end of the sequence for the defined number of times before it reaches the end. The
rollback() operation occurs once the sequence ends. Hence, this holds onto the rollback operation for a specified period of time. Further, this re-delivers the message only after the specified duration.
Therefore, in the above example, the
java.lang.Thread.sleep(10000) property schedules the message back after 10 seconds.
There is no grantee that the re-scheduling will occur as soon as the defined time period in the sleep(..) operation ends. Also, it considers the delay of any re-deliveries. For example, if the re-delivery delay is x seconds and the sleep time is y seconds, then the overall time for the message to get re-delivered is x+y seconds.
Also you can use the filter mediator and switch mediator to do the filtering as shown in the example below, when you need to define re-delivery intervals based on the message error condition dynamically.