2. We can filter JMS messages on based on JMS properties like JMS priority, JMS Type and
Headers etc. We will first look into how we can filter JMS messages on based on JMS
priority and then with Header.
3. So, first we will create a Mule flow that will send messages to the queue of ActiveMQ. Consider the
following flow :-
<jms:activemq-connector name=“Active_MQ” numberOfConcurrentTransactedReceivers=“20”
brokerURL=“tcp://localhost:61616″/>
<flow name=“JMSSender” doc:name=“JMSSender”>
<http:inbound-endpoint exchange-pattern=“request-
response” host=“localhost”port=“8081” path=“jms” doc:name=“HTTP”/>
<logger message=“Payload :- #[message.payload]” level=“INFO” doc:name=“Logger”/>
<jms:outbound-endpoint queue=“MyQueue” connector-ref=“Active_MQ” doc:name=“JMS”>
</jms:outbound-endpoint>
</flow>
4. Now, if we hit the url :- http://localhost:8081/jms we will be sending message payload
to the queue MyQueue . Since we haven’t set any priority to the message, it will be
send to the queue with default priority 4 as follows:-
5. So, here we will be setting priority in our message payload. To set priority in our payload
we will be configuring it as following:-
<message-properties-transformer>
<add-message-property key=“Priority” value=“9”/>
</message-properties-transformer>
As you can see, we are trying to set the message priority to 9.
7. So, if you now again hit the url :- http://localhost:8081/jms the message will now again
pushed into the queue MyQueue but this time with priority 9 as follows:-
8. Since we can define our own JMS priority to the messages that we push into the queue,
we can now consume the message from the queue based on the JMS priority. That
means we can now filter the message from queue based on JMS priority.
9. So, here will be now creating a flow that will consume messages from JMS queue based on
priority. That means we will be consuming messages from 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 connector-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>
10. Here, the flow is configured to consume only the JMS messages from the
queue MyQueue whose JMS priority is 9 and remaining messages will be ignored as
follows:-
Here you can see in the above that the messages with JMS priority 9 are consumed while
the remaining messages are left in the queue MyQueue .
11. In the similar way we can also configure the JMS messages by setting JMS header as
follows:-
<message-properties-transformer>
<add-message-property key=“Header” value=“Custom-Header” />
</message-properties-transformer>
And then consume it based on the Header:-
<jms:inbound-endpoint connector-ref=“Active_MQ” doc:name=“JMS”exchange-
pattern=“request-response” address=“jms://tcp:MyQueue”>
<jms:selector expression=“Header = ‘Custom-Header'”/>
</jms:inbound-endpoint>
So here messages from queue will be consumed only if its header is ‘Custom-Header’
12. I hope I am clear enough to demonstrate the way to configure JMS messages
with JMS properties and consuming the messages based on the properties
by applying filters.
Now, you can experiment your own way and configure JMS messages and
implement the example .