Mule ESB has an amazing capacity to connect with Message queues and send/receive message between them.We all know how to send/receive message to a Message queue and that I am not going to demonstrate here. But yes I like to demonstrate about how to schedule and delay a JMS messages going to a Message queue.What I actually mean is, we can configure our JMS in Mule so that it dispatch the messages to the queue not immediately, but on delay with a time that we decide dynamically.
How can we configure and schedule the delay of our JMS Messages???
We will take a very simple example to demonstrate and understand how can we configure.In Mule, we can configure various properties of our messages.Here also, we need to configure a property of our JMS messages.We can set the following property dynamically in our message before dispatching it to the message queue.
What does that means???
AMQ_SCHEDULED_DELAY :- The time in milliseconds that a message will wait before being scheduled to be delivered by the broker.So, what we did here, is we set a property in our message, which will make the message to wait for the time we mentioned in milliseconds and then it will get dispatched in the queue.
Here we will configure the milliseconds as 10000 dynamically say from an input JSON request payload, so we need to wait till that time to get the message dispatched.
So, we will be designing our flow in following way :-
As we can see, we have added AMQ_SCHEDULED_DELAY before dispatching it to the message queue testqueue with a flow variable which is storing the value of delay from a JSON request.
Testing our flow
We will hit the url http://localhost:8081/jms to dispatch the message to the queue, with a JSON payload that will contains the value of the delay we want to set to our JMS message.
We will able to set the value dynamically from our message payload directly as follows:-
Now, we will check our queue testqueue and will find no message has be collected:-
We will see here there no queue has been created and no message is there in the queue.
We will wait for some time and if we refresh the page after 10000 milliseconds, we will find queue testqueue is created the message is deposited in our queue :-
So this was a very simple demonstration with a very simple use case to hold, schedule and delay a JMS Message before dispatching it into a message queue dynamically from a message payload.
Hope you will like the article and hope to see your comments down under..