O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
Create successful ePaper yourself
Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
Each of the accessor methods (getProperty( )) can throw the<br />
<strong>Message</strong>FormatException. The <strong>Message</strong>FormatException is thrown by the accessor methods<br />
in order to indicate that the original type could not be converted to the type requested. The<br />
<strong>Message</strong>FormatException might be thrown if, for example, a JMS client attempted to read a<br />
float property as an int.<br />
String values can be converted to any primitive type, provided the String is formatted<br />
correctly:<br />
<strong>Message</strong> message = topicSession.create<strong>Message</strong>( );<br />
// Set the property "Weight" as a String value<br />
message.setStringProperty("Weight","240.00");<br />
// Set the property "IsProgrammer" as a String value<br />
message.setStringProperty("IsProgrammer", "true");<br />
...<br />
// Read the property "Weight" as a flaot type<br />
float weight = message.getFloatProperty("Weight");<br />
// Read the property "IsProgrammer" as a boolean type<br />
boolean isProgrammer = message.getBooleanProperty("IsProgrammer");<br />
If the String value cannot be converted to the primitive type requested, a<br />
java.lang.NumberFormatException is thrown. Any property can be accessed as a String<br />
using the getStringProperty( ) method; all the primitive types can be converted to a<br />
String value.<br />
The getObjectProperty( ) returns the appropriate object wrapper for that property. For<br />
example, an int can be retrieved by the message consumer as a java.lang.Integer object.<br />
Any property that is set using the setObjectProperty( ) method can also be accessed<br />
using the primitive property accessors; the conversion rules outlined in Table C.1 apply.<br />
The following code shows two properties (Age and Weight) that are set using primitive and<br />
Object property methods. The properties are later accessed using the Object, primitive, and<br />
String accessors:<br />
<strong>Message</strong> message = topicSession.create<strong>Message</strong>( );<br />
// Set the property "Weight" as a float value<br />
message.setFloatProperty("Weight",240.00);<br />
// Set the property "Age" as an Integer value<br />
Integer age = new Integer(72);<br />
message.setObjectProperty("Age", age);<br />
...<br />
// Read the property "Weight" as a java.lang.Float type<br />
Float weight1 = (Float)message.getObjectProperty("Weight");<br />
// Read the property "Weight" as a flaot type<br />
float weight2 = message.getFloatProperty( );<br />
// Read the property "Age" as an Object type<br />
Integer age1 = (Integer)message.getObjectProperty("Age");<br />
// Read the property "Age" as a long is legal<br />
long age2 = message.getLongProperty("Age");<br />
170