Sun GlassFish Message Queue 4.4 Developer's Guide for Java Clients

Processing Messages

Processing a message after you have received it may entail examining its header fields, properties, and body. The following sections describe how this is done.

Retrieving Message Header Fields

The standard JMS message header fields are described in Table 2–4. Table 2–18 shows the methods provided by the JMS Message interface for retrieving the values of these fields: for instance, you can obtain a message’s reply destination with the statement

Destination  replyDest = inMsg.getJMSReplyTo();
Table 2–18 Message Header Retrieval Methods

Name 

Description 

getJMSMessageID

Get message identifier 

getJMSDestination

Get destination 

getJMSReplyTo

Get reply destination 

getJMSCorrelationID

Get correlation identifier as string 

getJMSCorrelationIDAsBytes

Get correlation identifier as byte array 

getJMSDeliveryMode

Get delivery mode 

getJMSPriority

Get priority level 

getJMSTimestamp

Get time stamp 

getJMSExpiration

Get expiration time 

getJMSType

Get message type 

getJMSRedelivered

Get redelivered flag 

Retrieving Message Properties

Table 2–19 lists the methods defined in the JMS Message interface for retrieving the values of a message’s properties (see Message Properties). There is a retrieval method for each of the possible primitive types that a property value can assume: for instance, you can obtain a message’s time stamp with the statement

long  timeStamp = inMsg.getLongProperty("JMSXRcvTimestamp");
Table 2–19 Message Property Retrieval Methods

Name 

Description 

getIntProperty

Get integer property 

getByteProperty

Get byte property 

getShortProperty

Get short integer property 

getLongProperty

Get long integer property 

getFloatProperty

Get floating-point property 

getDoubleProperty

Get double-precision property 

getBooleanProperty

Get boolean property 

getStringProperty

Get string property 

getObjectProperty

Get property as object 

getPropertyNames

Get property names 

propertyExists

Does property exist? 

There is also a generic getObjectProperty method that returns a property value in objectified form, as a Java object of class Integer, Byte, Short, Long, Float , Double, Boolean, or String . For example, another way to obtain a message’s time stamp, equivalent to that shown above, would be

Long  timeStampObject = (Long)inMsg.getObjectProperty("JMSXRcvTimestamp");
long  timeStamp = timeStampObject.longValue();

If the message has no property with the requested name, getObjectProperty will return null; the message method propertyExists tests whether this is the case.

The getPropertyNames method returns a Java enumeration object for iterating through all of the property names associated with a given message; you can then use the retrieval methods shown in the table to retrieve each of the properties by name, as shown in Example 2–5.


Example 2–5 Enumerating Message Properties


Enumeration  propNames = inMsg.getPropertyNames();
String       eachName;
Object       eachValue;

while ( propNames.hasMoreElements() )
  { eachName  = propNames.nextElement();
    eachValue = inMsg.getObjectProperty(eachName);
    /* Do something with the value */
  }
               

Processing the Message Body

The methods for retrieving the contents of a message’s body essentially parallel those for composing the body, as described earlier under Composing Messages. The following sections describe these methods for each of the possible message types (text, stream, map, object, and bytes).

Processing Text Messages

The text message method getText (Table 2–20 ) retrieves the contents of a text message’s body in the form of a string:

String  textBody = inMsg.getText();
Table 2–20 Text Message Access Method

Name 

Description 

getText

Get content string 

Processing Stream Messages

Reading the contents of a stream message is similar to reading from a data stream, using the access methods shown in Table 2–21: for example, the statement

int  intVal = inMsg.readInt();

retrieves an integer value from the message stream.

Table 2–21 Stream Message Access Methods

Name 

Description 

readInt

Read integer from message stream 

readByte

Read byte value from message stream 

readBytes

Read byte array from message stream 

readShort

Read short integer from message stream 

readLong

Read long integer from message stream 

readFloat

Read floating-point value from message stream 

readDouble

Read double-precision value from message stream 

readBoolean

Read boolean value from message stream 

readChar

Read character from message stream 

readString

Read string from message stream 

readObject

Read value from message stream as object 

The readObject method returns the next value from the message stream in objectified form, as a Java object of the class corresponding to the value’s primitive data type: for instance, if the value is of type int, readObject returns an object of class Integer. The following statements are equivalent to the one shown above:

Integer  intObject = (Integer) inMsg.readObject();
int      intVal    = intObject.intValue();

Processing Map Messages

The MapMessage interface provides the methods shown in Table 2–22 for reading the body of a map message. Each access method takes a name string as an argument and returns the value to which that name is mapped: for instance, under the example shown in Composing Map Messages, the statement

int  meaningOfLife = inMsg.getInt("The Meaning of Life");

would set the variable meaningOfLife to the value 42.

Table 2–22 Map Message Access Methods

Name 

Description 

getInt

Get integer from message map by name 

getByte

Get byte value from message map by name 

getBytes

Get byte array from message map by name 

getShort

Get short integer from message map by name 

getLong

Get long integer from message map by name 

getFloat

Get floating-point value from message map by name 

getDouble

Get double-precision value from message map by name 

getBoolean

Get boolean value from message map by name 

getChar

Get character from message map by name 

getString

Get string from message map by name 

getObject

Get object from message map by name 

itemExists

Does map contain an item with specified name? 

getMapNames

Get enumeration of all names in map 

Like stream messages, map messages provide an access method, getObject, that returns a value from the map in objectified form, as a Java object of the class corresponding to the value’s primitive data type: for instance, if the value is of type int, getObject returns an object of class Integer. The following statements are equivalent to the one shown above:

Integer  meaningObject = (Integer) inMsg.getObject("The Meaning of Life");
int      meaningOfLife = meaningObject.intValue();

The itemExists method returns a boolean value indicating whether the message map contains an association for a given name string:

if ( inMsg.itemExists("The Meaning of Life") )
  { /* Life is meaningful */
  }
else
  { /* Life is meaningless */
  }

The getMapNames method returns a Java enumeration object for iterating through all of the names defined in the map; you can then use getObject to retrieve the corresponding values, as shown in Example 2–6.


Example 2–6 Enumerating Map Message Values


Enumeration  mapNames = inMsg.getMapNames();
String       eachName;
Object       eachValue;

while ( mapNames.hasMoreElements() )
  { eachName  = mapNames.nextElement();
    eachValue = inMsg.getObject(eachName);
    /* Do something with the value */
  }
                  

Processing Object Messages

The ObjectMessage interface provides just one method, getObject (Table 2–23 ), for retrieving the serialized object that is the body of an object message:

Object  messageBody = inMsg.getObject();

You can then typecast the result to a more specific class and process it in whatever way is appropriate.

Table 2–23 Object Message Access Method

Name 

Description 

getObject

Get serialized object from message body 

Processing Bytes Messages

The body of a bytes message simply consists of a stream of uninterpreted bytes; its interpretation is entirely a matter of agreement between sender and receiver. This type of message is intended primarily for decoding message formats used by other existing message systems; Message Queue clients should generally use one of the other, more specific message types instead.

Reading the body of a bytes message is similar to reading a stream message (see Processing Stream Messages): you use the methods shown in Table 2–24 to decode primitive values from the message’s byte stream. For example, the statement

int  intVal = inMsg.readInt();

retrieves an integer value from the byte stream. The getBodyLength method returns the length of the entire message body in bytes:

int  bodyLength = inMsg.getBodyLength();
Table 2–24 Bytes Message Access Methods

Name 

Description 

getBodyLength

Get length of message body in bytes 

readInt

Read integer from message stream 

readByte

Read signed byte value from message stream 

readUnsignedByte

Read unsigned byte value from message stream 

readBytes

Read byte array from message stream 

readShort

Read signed short integer from message stream 

readUnsignedShort

Read unsigned short integer from message stream 

readLong

Read long integer from message stream 

readFloat

Read floating-point value from message stream 

readDouble

Read double-precision value from message stream 

readBoolean

Read boolean value from message stream 

readChar

Read character from message stream 

readUTF

Read UTF-8 string from message stream