Delay With JMS: From Configuration to Flow Testing
Mule ESB has powerful connectivity with message queues. Here's a quick tutorial for sending and receiving messages, message scheduling, and testing a flow.
Join the DZone community and get the full member experience.Join For Free
Mule ESB has an amazing capacity to connect with message queues and send/receive message between them. We all know how to send/receive messages in 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.
<message-properties-transformer doc:name="Message Properties"> <add-message-property key="AMQ_SCHEDULED_DELAY" value="10000"/> </message-properties-transformer>
What Does That Mean???
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:
<http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/> <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ"/> <flow name="JMSDelayFlow"> <http:listener config-ref="HTTP_Listener_Configuration" path="/jms" doc:name="HTTP"/> <json:json-to-object-transformer returnClass="java.lang.Object" doc:name="JSON to Object" /> <set-variable variableName="delay" value="#[message.payload.jmsDelay]" doc:name="Variable"/> <message-properties-transformer doc:name="Message Properties"> <add-message-property key="AMQ_SCHEDULED_DELAY" value="#[flowVars.delay]"/> </message-properties-transformer> <json:object-to-json-transformer doc:name="Object to JSON"/> <jms:outbound-endpoint queue="testqueue" connector-ref="Active_MQ" doc:name="JMS"/> </flow>
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...
Published at DZone with permission of Anirban Sen Chowdhary, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.