Sun Java System Message Queue 4.1 Developer's Guide for Java Clients

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