Chapter 23
894
The Message interface provides accessor/mutator methods for the JMSReplyTo, JMSCorrelationID,
and JMSType headers. The following code fragment sets the JMSReplyTo header:
message.setJMSReplyTo(myTopic);
The setJMSReplyTo(Destination destination) method sets the destination to which replies to
the message should be sent. The destination parameter specifies the reply destination (a queue or
topic). When the message is sent and consumed and the consumer replies to the message, it will reply to
the specified destination (myTopic, in this case).
There are also message headers that the developer cannot set, but can access using get()methods:
q
The JMSDestination header identifies the javax.jms.Destination (queue or topic) to
which the message is being sent.
q
The JMSMessageID header is a String value that uniquely identifies the message. The JMS
vendor decides whether the ID is universally unique or whether the ID is only unique for the
current installation of the message server. The message server automatically generates the
JMSMessageID when a client sends a message.
q
The JMSTimestamp header is the approximate time that the message producer sends the
message. It is approximate because the server sets it when it receives the message, which
might not be exact the same time the producer sends it.
q
The JMSRedelivered header indicates whether or not the message has been redelivered to the
consumer. It takes a value of true if the message has been redelivered.
These headers have both get()and set() methods, but the set() methods do not do anything. In the
following example we attempt to set the destination of the message:
myTopicPublisher.publish(myMessage);
myMessage.setJMSDestination(MyOtherTopic);
Topic topic = (Topic)myMessage.getJMSDestination();
myMessage is published to MyTopic then we attempt to reset the message destination to
MyOtherTopic. The setJMSDestination() method does nothing. After we call
getJMSDestination() the value of topic is MyTopic, not MyOtherTopic.
Message Properties
A message header has a set of properties that contain extra information about the message. Effectively,
message properties are optional message header fields.
There are standard JMS properties, whose property names start with JMSX_, but support for these
properties is optional and not all JMS providers implement the use of these properties. Each JMS vendor
may also define vendor-specific properties. Also, a developer can specify application-specific properties.
A message producer sets the message properties before sending the message. A property may be of type
boolean, byte, short, int, long, float, double, String, or Object. When a message consumer
receives a message, its properties are read-only.