background image

JMS Message Selectors

<< JMS Message Listeners | Message Properties >>
<< JMS Message Listeners | Message Properties >>

JMS Message Selectors

JMS Message Selectors
If your messaging application needs to filter the messages it receives, you can use a JMS API
message selector, which allows a message consumer to specify the messages it is interested in.
Message selectors assign the work of filtering messages to the JMS provider rather than to the
application. For an example of an application that uses a message selector, see
"A Java EE
Application That Uses the JMS API with a Session Bean" on page 964
.
A message selector is a String that contains an expression. The syntax of the expression is
based on a subset of the SQL92 conditional expression syntax. The message selector in the
example selects any message that has a NewsType property that is set to the value 'Sports' or
'Opinion'
:
NewsType =
'Sports' OR NewsType = 'Opinion'
The createConsumer, createDurableSubscriber methods allow you to specify a message
selector as an argument when you create a message consumer.
The message consumer then receives only messages whose headers and properties match the
selector. (See
"Message Headers" on page 907
, and
"Message Properties" on page 908
.) A
message selector cannot select messages on the basis of the content of the message body.
JMS Messages
The ultimate purpose of a JMS application is to produce and to consume messages that can then
be used by other software applications. JMS messages have a basic format that is simple but
highly flexible, allowing you to create messages that match formats used by non-JMS
applications on heterogeneous platforms.
A JMS message has three parts: a header, properties, and a body. Only the header is required.
The following sections describe these parts:
"Message Headers" on page 907
"Message Properties" on page 908
"Message Bodies" on page 908
For complete documentation of message headers, properties, and bodies, see the
documentation of the Message interface in the API documentation.
Message Headers
A JMS message header contains a number of predefined fields that contain values that both
clients and providers use to identify and to route messages.
Table 31­1
lists the JMS message
header fields and indicates how their values are set. For example, every message has a unique
identifier, which is represented in the header field JMSMessageID. The value of another header
field, JMSDestination, represents the queue or the topic to which the message is sent. Other
fields include a timestamp and a priority level.
The JMS API Programming Model
Chapter 31 · The Java Message Service API
907