Monday, October 17, 2011

Weblogic and JMS Queue Message Filtering


Recently, one of my customers presented me the following scenario:



On a JMS Queue he had a Producer and several JMS consumers. The producer sends messages to the Queue that are to be consumed by a specific Client.

My customer wanted to know how to implement their JMS Queue Sender and Receiver to only post/get the messages they are supposed to.

WLS provides a set of predefined message headers that can be set to implement such logic or you can define your own set of message properties:

Setting and Browsing Message Header and Property Fields

Once the producer sets a message property, you can instantiate a consumer with a selector to filter only the messages that matches the filter expression:

Filtering Messages

So, in the above example, client C1 would only receive messages tagged with property "M1" and client C2 would only get messages tagged "M2".

Keep reading to see how the sample classes work....





The Filtering expressions follow a SQL standard similar to the 'WHERE' clause. So, Filters could include expressions like:

"salary > 64000 and dept in ('eng','qa')"

"product like 'WebLogic%' or product like '%T3') and version > 3.0"

"hireyear between 1990 and 1992 or fireyear is not null"

and so on...

I have provided a small example of a QueueSender.java and a QueueReceiver.java that implements filtering

Download Examples Here

To test them, on a WLS domain, create a Connection Factory with JNDI name as "QCF" and a Queue with JNDI name as "TestQ". (Sources are attached in case you want to change anything).

Open a command prompt and run setDomainEnv.cmd(.sh) script provided with WLS.

Go to the place where you extracted the provided classes and run the QueueReceive.class with "java examples.samples.QueueReceive".

Then run the QueueSender with "java examples.samples.QueueSend"



Note that the QueueSend, sends 4 messages: two to CONSUMER_1 and other two to CONSUMER_2, but only messages tagged to CONSUMER_1 are retrieved from the Queue.

You can check this by monitoring the Queue in the WLS Admin Console.



Checking the Message properties we can see that only the ones with property "RECIPIENT_ID = CONSUMER_2" are lef tin the Queue.



Not only the Message Filtering can enable a more sofisticated logic to your JMS applications but it can improve the system performance by only retrieving the messages that are addressed for that specific client.

Reference:

Programing Weblogic JMS
Message API

0 comments:

Post a Comment