Property Value Conversion

The JMS specification defines rules for conversion of property values, so that, for example, a property value of type int can be read as a long:

Message message = topicSession.createMessage( );
// Set the property "Age" as an int value
message.setIntProperty("Age", 72);
...
// Read the property "Age" as a long is legal
long age = message.getLongProperty("Age");

The conversion rules are fairly simple, as shown in Table 3.1. A property value can be set as one primitive type or String, and read as one of the other value types.

Table C.1. Property Type Conversions

Message.set<TYPE>Property( )

Message.get<TYPE>Property( )

boolean
boolean, String
byte
byte, short, int, long, String
short
short, int, long, String
int
int, long, String
long
long, String
float
float, double, String
double
double, String
String
String, boolean, byte, short, int, long, float, double

Each of the accessor methods (get<TYPE>Property( )) can throw the MessageFormatException . The MessageFormatException is thrown by the accessor methods in order to indicate that the original type could not be converted to the type requested. The MessageFormatException might be thrown if, for example, a JMS client attempted to read a float property as an int.

String values can be converted to any primitive type, provided the String is formatted correctly:

Message message = topicSession.createMessage( );

// Set the property "Weight" as a String value
message.setStringProperty("Weight","240.00");

// Set the property "IsProgrammer" as a String value
message.setStringProperty("IsProgrammer", "true");
...

// Read the property "Weight" as a flaot type
float weight = message.getFloatProperty("Weight");
// Read the property "IsProgrammer" as a boolean type
boolean isProgrammer = message.getBooleanProperty("IsProgrammer");

If the String value cannot be converted to the primitive type requested, a java.lang.NumberFormatException is thrown. Any property can be accessed as a String using the getStringProperty( ) method; all the primitive types can be converted to a String value.

The getObjectProperty( ) returns the appropriate object wrapper for that property. For example, an int can be retrieved by the message consumer as a java.lang.Integer object. Any property that is set using the setObjectProperty( ) method can also be accessed using the primitive property accessors; the conversion rules outlined in Table 3.1 apply. The following code shows two properties (Age and Weight) that are set using primitive and Object property methods. The properties are later accessed using the Object, primitive, and String accessors:

Message message = topicSession.createMessage( );

// Set the property "Weight" as a float value
message.setFloatProperty("Weight",240.00);

// Set the property "Age" as an Integer value
Integer age = new Integer(72);
message.setObjectProperty("Age", age);
...

// Read the property "Weight" as a java.lang.Float type
Float weight1 = (Float)message.getObjectProperty("Weight");
// Read the property "Weight" as a flaot type
float weight2 = message.getFloatProperty( );

// Read the property "Age" as an Object type
Integer age1 = (Integer)message.getObjectProperty("Age");
// Read the property "Age" as a long is legal
long age2 = message.getLongProperty("Age");
..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset