J2EE Messaging
895
A client can get and set message properties through methods on the Message interface. The following
code fragment first sets, then gets the status property of a message containing a short integer:
message.setShortProperty("status", 5);
short status = message.getShortProperty("status");
The property accessor and mutator methods are set[type]Property(String name, [type]
value) and get[type]Property(String name), where [type] is the type of the property (in this
case, short). The set() methods create the property if it doesn't exist before set is called.
A client can clear all property values by calling the message's clearProperties() method and can
check if a property exists by using the message's propertyExists(String name) method.
Message Filtering
A message consumer can be configured to receive only messages with certain message properties. For
example, consider a message consumer that is listening to receive stock quotes. The message consumer
only wishes to receive certain stock quotes so it checks the stock symbol property of each message to
determine whether or not it wishes to receive that stock quote.
The consumer specifies which messages it wishes to receive with a message selector. The following code
fragment creates a selector that filters messages, looking for only messages whose stockSymbol
property is MSFT or SUNW, and creates a subscriber, which will listen only for those stock symbols:
String selector = new String("(stockSymbol = SUNW) OR (stockSymbol = MSFT)");
selectiveListener = new TopicSubscriber(stockTopic,selector);
The message selector is a String. Its syntax is based on a subset of the SQL-92 conditional expression
syntax. The consumer sends the selector as a parameter to the creation of the message consumer (either
a TopicSubscriber or a QueueReceiver).
The complete syntax of message selectors can be found in the JMS specification, available at
http://www.java.sun.com/products/jms/docs.html.
Message Body
A JMS message body can contain messages of five different types:
q
The StreamMessage type contains a stream of Java primitive values. It is both filled
and read sequentially.
q
The MapMessage type contains a set of name-value pairs where names are of type String
and values are of Java primitive types. The entries can be accessed sequentially by enumerator
or randomly by name. The order of the entries is undefined.
q
The TextMessage type contains a java.lang.String object. The text may contain
XML data.
q
The ObjectMessage type contains a serializable Java object. If you want to send a group of
objects in one message you can send a collection object, such as a List or Set, containing
several serializable objects.
q
The BytesMessage type contains a stream of uninterpreted bytes.