Message selectors allow you to filter specific messages using a selector string. The message consumer will then receive only messages whose headers and properties match the selector. There are different patterns that can be used in selector strings, and the broker (JMS provider) filters messages according to that query. It is not possible for a message selector to filter messages on the basis of the content of the message body.
Message headers
A message header contains a number of predefined fields that contain values that both clients and providers can use to identify and to route messages.
Following is a list of message header selector strings that are supported:
Message Header | Selector String Example |
---|---|
JMSDestination | JMSDestination=destqueue |
JMSMessageID | JMSMessageID=JMSMessageID |
JMSTimestamp | JMSTimestamp=1396370353826 |
JMSCorrelationID | JMSCorrelationID='srilanka' |
JMSReplyTo | |
JMSType | JMSType='AAAA' |
The following message headers will not be filtered by the broker as they are handled by the JMS provider and the default values override the selector string:
- JMSDeliveryMode
- JMSExpiration
- JMSPriority
- JMSRedelivered
Message properties
If you need values in addition to those provided by the header fields, you can create and set properties for the messages.
Following is a list of message property selector strings that are supported:
Message Properties | Selector String Example |
---|---|
ByteProperty | ByteMsgID=127 |
ShortProperty | ShortMsgID=32,767 |
IntProperty | IntMsgID=400000000 |
LongProperty | LongMsgID=1212322222 |
FloatProperty | FloatMSgID = 0.0f |
DoubleProperty | DoubleMsgID = 20011111000.12120 |
StringProperty | StrMsgID='100' |
BooleanProperty | BoolMsgID=false |
ObjectProperty |
Examples of selector patterns
Some example selector patterns are listed below:
Selector Patterns | Example |
---|---|
Header='value' OR Header='value' | JMSType='AAAA' OR JMSPriority=4 |
Property='value' AND Property='value' | Country='SL' AND ID=1 |
Header='value' OR Property='value' | JMSType='AAA' OR msgID='1' |
Header='value' AND Property='value' AND Property='value' | JMSType = 'AAA' AND color = 'red' AND weight= 3500 |
(Property='value' OR Header='value') AND Property='value' | (Country='SL' OR JMSType='AAA') AND ID=1 |
Creating a message selector subscription
The createConsumer
and createDurableSubscriber
methods allow you to specify a message selector as an argument when you create a message consumer.
You can create a message selector subscriber for a topic as in the following example:
String messgeSelector="JMSType='AAAA'"; topicSession.createSubscriber(topic,messgeSelector,true)
When creating a subscriber, set the nolocal
property to 'false' as this property is not supported by MB. However, messages are still delivered to the subscriber irrespective of the nolocal
parameter.