Being Picky with JMS
Anirban provides an example of how ActiveMQ can integrate with Mulesoft for JMS processing.
Join the DZone community and get the full member experience.Join For Free
mule esb offers an amazing out-of-the-box integration which easily integrates with activemq . there are a plethora of examples on the internet that will show how to use activemq with mule . but here we will explore how to use a filter with activemq and mule that will help us picking up the right jms messages we need.
consider an environment where there is a jms queue and there are multiple consumers listening to that queue; how can a mule flow filter and pick up a particular jms message of choice from all the messages available in the queue?
let’s assume we have a jms queue named myqueue which many consumers listening to it:
we can see the queue contains few messages in it. now all these messages can be consumed by any of the consumers out there which are listening to this queue. but how can our jms consumer in our mule flow select particular messages and consume only a selection of messages out of it?
we can filter jms messages based on jms properties like jms priority, jms type and headers, etc. we will first look into how we can filter jms messages based on jms priority and then with header.
to consume a jms message based on jms priority, we need to insert the message into the queue with that priority. let’s consider first that we will only consume jms messages whose priority will be, say, 9.
so, let’s first insert a couple of jms messages in the queue myqueue with priority 9.
so, now we have our queue with total 5 messages out of which 2 messages have the priority 9 and remaining to have default priority of 0.
with this in place, we will now consume the messages in the queue myqueue with 2 consumers listening to the same queue; one that will consume only those messages with priority=9 and the other will consume the remaining messages with default priority.
we will now create a flow that will filter and consume messages from the jms queue based on priority. that means we will be consuming only those messages from the queue myqueue whose jms priority is 9. we will be using jms:selector here to filter jms messages as follows:
<flow name="jmsreceiver" doc:name="jmsreceiver"> <jms:inbound-endpoint <a href="https://www.mulesoft.com/exchange#!/?types=connector"target="_blank" title="cloud connectors" >connector-</a>ref="active_mq" doc:name="jms" exchange-pattern="request-response" address="jms://tcp:myqueue"> <jms:selector expression="jmspriority = 9" /> </jms:inbound-endpoint> <logger level="info" message="received payload :-#[message.payload]" doc:name="logger"/> </flow>
so, now if we start our flow, we will find that our jms consumer from our mule flow has selectively consumed the messages from the queue myqueue whose jms priority is 9 leaving other messages in that queue.
you can see here that it has consumed only those messages of priority 9 and remaining messages will be ignored.
so we can now configure jms messages with a particular message priority and later we can consume the messages based on that priority.
now let’s have another mule flow, with another jms consumer, and this time, without a filter listening to the same queue myqueue:
<flow name="jmsreceiver2" doc:name="jmsreceiver"> <jms:inbound-endpoint connector-ref="active_mq" doc:name="jms" exchange-pattern="request-response" address="jms://tcp:myqueue"/> <logger level="info" message="received payload :-#[message.payload]" doc:name="logger"/> </flow>
this time, we haven’t given any filter and now we will find all the other messages are consumed by the consumer leaving the queue empty:
using a dynamic jms selector:-
now the question arises, can we do this dynamically? can we select the messages by its jms properties which are set dynamically?
yes, we can. we use this selector option in an expression to use a dynamic jms selector. to select dynamically we can use the select option of mule requester and can call it in the middle of a mule flow, selecting and consuming the messages with dynamic selection:-
<flow name="jmsreceiverdynamic" > <http:listener config-ref="http_listener_configuration" path="/dynamic" doc:name="http"/> <set-variable variablename="priority" value="9" doc:name="variable"/> <mulerequester:request config-ref="mule_requester" resource="jms://myqueue?selector=priority%3d'#[flowvars.priority]'" doc:name="mule requester" timeout="120000"/> <logger level="info" message="received payload with selecting dynamically:-#[message.payload]" doc:name="logger"/> </flow>
here we have selected the messages with priority=9 dynamically from a variable with an expression. we can use values from variables, inbound properties, properties file or from a message payload directly to make it dynamic.
pushing a message to jms queue with jms priority from mule :-
next we will see how to insert a message in our jms queue, directly from our mule flow, setting jms priority of the message to 9.
here is the mule flow, which can directly set the message into jms queue with a priority 9:-
<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" numberofconcurrenttransactedreceivers="20" brokerurl="tcp://localhost:61616"/> <flow name="jmssender" > <http:listener config-ref="http_listener_configuration" path="/jms" doc:name="http"/> <set-payload value="this is a test jms message with priority 9" doc:name="set payload"/> <logger message="payload :- #[message.payload]" level="info" doc:name="logger" /> <jms:outbound-endpoint queue="myqueue" connector-ref="active_mq" doc:name="jms"> <message-properties-transformer> <add-message-property key="priority" value="9" /> </message-properties-transformer> </jms:outbound-endpoint> </flow>
here we will set the priority in our message payload. to set priority in our payload we will be configuring it as the following:-
<message-properties-transformer> <add-message-property key="priority" value="9"/> </message-properties-transformer>
so, if we invoke the mule flow by hitting the url:- http://localhost:8081/jms the message will now be pushed into the queue myqueue with priority 9 as follows:-
in a similar way we can also configure the jms messages by setting other jms properties like jms header in our mule flow as follows:
<message-properties-transformer> <add-message-property key="header" value="mycustom-header" /> </message-properties-transformer>
each message will be sent to the queue myqueue with a header attached to it. so, if we check the property of the message in the queue, we can see our header is attached to it:
and then consume it based on the header:
<jms:inbound-endpoint connector-ref="active_mq" doc:name="jmsheader" exchange-pattern="request-response" address="jms://tcp:myqueue"> <jms:selector expression="header = 'mycustom-header'"/> </jms:inbound-endpoint>
so here messages from queue myqueue will be consumed only if its header is ‘mycustom-header’ in the same way it filtered and consumed jms messages based on priority.
using dynamic jms selector again:-
again, if we want to consume it dynamic selection, we can use the expression in mule requester and select the message based on header while fetching the header from a variable:-
<set-variable variablename="custom-header" value="mycustom-header" doc:name="variable"/> <mulerequester:request config-ref="mule_requester" resource="jms://myqueue?selector=header%3d'#[flowvars.custom-header]'" doc:name="mule requester" timeout="120000"/>
at this point, you should have a good idea on the way to configure jms messages with jms properties and consuming the messages from a queue in a multi-consumer environment based on the properties by applying filters both statically and dynamically.
now, you can experiment in your own way and configure jms messages and implement the example.
please do share your feedback and experiences in the below section for comments.
this article was written by anirban
Published at DZone with permission of Ross Mason, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.
Integration Testing Tutorial: A Comprehensive Guide With Examples And Best Practices
Which Is Better for IoT: Azure RTOS or FreeRTOS?
How To Scan and Validate Image Uploads in Java
Simplifying SAP Data Integration With Google Cloud