n a previous article
I covered some of the basics of JMS using OpenJMS as the JMS provider. In this article, you'll see some more advanced OpenJMS topics such as message selectors, durable subscriptions, and message persistence along with code examples that show you how to implement these concepts programmatically.
Sometimes you don't want message consumers to receive every message sent by a sender or publisher. Instead you want to be able to filter
received messages. The JMS API provides this facility through message selectors. The message consumer specifies which messages it is interested in. The JMS provider then selectively sends just those messages to the client. Note that the onus of filtering messages is left up to the JMS provider, not the consuming application
. The provider can scrutinize the header values and message properties with message selectors, but unfortunately, OpenJMS cannot analyze the incoming message bodies. I'll show you an example message selector in just a bit, but first here are a couple of topics (no JMS pun intended) to set the stage.
Message Header Fields
When a JMS provider sends a message it generates the following header fields:
- JMSMessageIDA unique message identifier
- JMSDestinationThe queue or topic to which the message is sent.
- JMSRedeliveredStates whether the message has been resent
The client sending application can set a number of header fields. Refer to the JMS API documentation for a full breakdown of what headers the client can set. Each header field has getter and setter methods. Some of the fields the client can set include:
- JMSPriorityYou can set a message priority from 0 to 9 (9 being the highest priority and 4 being the default). However, note that the there is no guarantee that higher priority messages will be delivered before lower priority ones.
- JMSTypeString identifying contents of a message
- JMSReplyToWhere responses should be sent
- JMSExpirationAn expiration time for the message
While the preceding message header fields are predefined, clients can add more information to JMS messages via the extensibility of properties
. These properties are name/value pairs which the client application defines. The values for properties can be of type boolean, byte, short, int, long, float, double, or string (each type has a corresponding Message.setProperty
method). For example, using the syntax below, you could set a property named "Sport" to "Basketball"