Message selectors

 

JMS allows an application to specify that only messages that satisfy certain criteria are returned by successive receive() calls. When creating a MessageConsumer object, we can provide a string that contains an SQL (Structured Query Language) expression, which determines which messages are retrieved. This SQL expression is called a selector. The selector can refer to fields in the JMS message header as well as fields in the message properties (these are effectively application defined header fields). Details of the header field names, as well as the syntax for an SQL selector, are in JMS messages.

The following example shows how to select messages based on a user defined property called myProp:

messageConsumer = session.createConsumer(ioQueue, "myProp = 'blue'");

The JMS specification does not permit the selector associated with a message consumer to be changed. After a message consumer is created, the selector is fixed for the lifetime of that consumer. This means that, if you require different selectors, create new message consumers.

We can control whether the JMS client or the broker performs message filtering in the publish/subscribe domain by setting the MSGSELECTION property on the ConnectionFactory object. If the broker is capable of performing message selection, it is generally preferable to let the broker do it because it reduces the amount of work done by the client. However, if the broker is very heavily loaded, it might be preferable to let the client perform message selection instead.


uj25120_