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

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 */
  }