23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

}<br />

public void setByteProperty(String name, byte value)<br />

throws JMSException, <strong>Message</strong>NotWriteableException;<br />

public long getLongProperty(String name)<br />

throws JMSException, <strong>Message</strong>FormatException;<br />

public void setLongPreperty(String name, long value)<br />

throws JMSException, <strong>Message</strong>NotWriteableException;<br />

public short getShortProperty(String name)<br />

throws JMSException, <strong>Message</strong>FormatException;<br />

public void setShortProperty(String name, short value)<br />

throws JMSException, <strong>Message</strong>NotWriteableException;<br />

public Object getObjectProperty(String name)<br />

throws JMSException, <strong>Message</strong>FormatException;<br />

public void setObjectProperty(String name, Object value)<br />

throws JMSException, <strong>Message</strong>NotWriteableException;<br />

public void clearProperties( )<br />

throws JMSException;<br />

public Enumeration getPropertyNames( )<br />

throws JMSException;<br />

public boolean propertyExists(String name)<br />

throws JMSException;<br />

...<br />

<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />

The following code shows how a JMS client might produce and consume messages with<br />

properties that have primitive values:<br />

// A message producer writes the properties<br />

message.setStringProperty("username","William");<br />

message.setDoubleProperty("Limit", 33456.72);<br />

message.setBooleanProperty("IsApproved",true);<br />

publisher.publish(message);<br />

...<br />

// A message consumer reads the properties<br />

String name = message.getStringProperty("username");<br />

double limit = message.getDoubleProperty("Limit");<br />

boolean isApproved = message.getBooleanProperty("IsApproved");<br />

The Object property methods that are defined in the <strong>Message</strong> interface<br />

(setObjectProperty( ) and getObjectProperty( )) are also used for properties, but they<br />

don't give you as much functionality as their names suggest. Only the primitive wrappers<br />

that correspond to the allowed primitive types and the String type can be used by the<br />

Object property methods. Attempting to use any other Object type will result in a<br />

javax.jms.<strong>Message</strong>FormatException.<br />

Given that the Object methods don't really let you do anything new, why do they exist?<br />

The Object property methods provide more flexibility, letting you write clients that don't<br />

hard-code the property types into the application. JMS publishers can decide at runtime<br />

what form properties should take, and JMS consumers can read the properties and use<br />

reflection to determine the value types at runtime. Here is an example of how the Object<br />

property methods are used to set and access properties in a message:<br />

// A message producer writes the properties<br />

String username = "William";<br />

Double limit = new Double(33456.72);<br />

Boolean isApproved = new Boolean(true);<br />

...<br />

message.setObjectProperty("username",username);<br />

message.setObjectProperty("Limit", limit);<br />

message.setObjectProperty("IsApproved",isApproved);<br />

168

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!