JMS Messages
While our simple examples only exchange string messages, there are several possible JMS message types. Every JMS message type is a subclass of javax.jms.Message. The different message types merely dictate the type of the message contents.
A JMS message consists of a header, a set of properties, and the message body.
JMS Header
The JMS header includes a standard set of fields defined in the JMS specification. The javax.jms.Message type includes a get and set method for each of the JMS header fields. The header fields are named metadata values used by the application to provide further information about the message.
The JMS header fields provide extra information about the JMS message. Many of the fields contain basic information about the message, and these are commonly used to filter messages. This is done in JMS with selectors and is covered in detail later in this chapter.
JMS header fields also can be used by the application code. For instance, the JMSReplyTo can be used by the application programmer for a request/response application. The sender sets the JMSReplyTo header field to a response JMS destination. When the consumer receives the message, a response is sent to the ReplyTo destination (see Table 7.2).
Table 7.2 The JMS Header Fields
Name |
Set by |
Description |
JMSDestination |
Send Method |
The JMSDestination includes the destination name where the message was sent. The WebLogic Server sets it automatically after the send() method completes. |
JMSDeliveryMode |
Send Method |
The JMSDeliveryMode specifies whether this is a persistent or nonpersistent message. A persistent message is stored on a backing store such as a file system or database so that it survives a server crash. |
JMSExpiration |
Send Method |
The JMSExpiration defines when the message has expired and will be removed from the system. This field depends on the time-to-live (TTL) specified when the message was sent. By default, messages have a TTL of 0 and a JMSExpiration of 0, which means they will never expire. |
JMSPriority |
Send Method |
The JMSPriority specifies the priority (0-9) of the message. Message priorities from 0-4 indicate normal priority, while priorities from 5-9 are expedited priority. The default priority is 4. |
JMSMessageID |
Send Method |
The JMSMessageID contains a string that uniquely identifies the message. The ID starts with ID: and is generated automatically by the WebLogic Server. |
JMSTimestamp |
Send Method |
The JMSTimestamp is a long value that represents a timestamp of when the message was accepted for delivery by WebLogic's JMS system. |
JMSCorrelationID |
Application |
The JMSCorrelationID is an arbitrary string that can be set by the application before sending the message. This can be used to correlate requests and responses by storing the JMSMessageID of the request in the JMSCorrelationID of the response. Another common use of this header field is linking several messages together in an application-specific manner. For instance, if an order consisted of multiple messages, the order number could be included in the JMSCorrelationID to allow the consumer to associate all of the messages together with a single order. |
JMSReplyTo |
Application |
The JMSReplyTo header is a JMS destination that the consumer can use to send a response. Note that this field only passes a destination to the consumer; it does not guarantee that the consumer will actually send a response to this destination. This is the responsibility of the application code. |
JMSType |
Application |
The JMSType enables the application to associate this message with a message type. This message type is an arbitrary java.lang.String, and it may be used if applications want to distinguish messages based on an application-specific type. |
JMSRedelivered |
WebLogic Server |
The JMSRedelivered flag is set when a JMS message is redelivered because the receiver failed to acknowledge the message or because the session is being recovered. Message acknowledgment is covered in detail later in this chapter. |
Message Properties
JMS also provides message properties. A JMS message producer can set application-specific property values in the message. The properties are transmitted with the messages and may be read by JMS message consumers.
Properties are name/value pairs and are set with the setObjectProperty or the type-specific setProperty calls on javax.jms.Message. The valid property types are boolean, byte, double, float, int, long, short, and java.lang.String.
For instance, a message producer can set the property named "MyProperty" to the value 4 with:
msg.setIntProperty("MyProperty", 4);
Properties are useful for associating application-specific metadata with a message. They are generally used for message filtering with JMS selectors. JMS selectors are covered in detail later in this chapter.