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.

Notification of certain bids on inventory<br />

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

A supplier wants notification of requests for bids on specific inventory items at<br />

specific quantities:<br />

String selector =<br />

"InventoryID = 'S93740283-02' AND Quantity BETWEEN 1000 AND 13000";<br />

TopicSubscriber subscriber =<br />

session.createSubscriber(topic, selector,false);<br />

3.4 <strong>Message</strong> Types<br />

The <strong>Java</strong> <strong>Message</strong> <strong>Service</strong> defines six <strong>Message</strong> interface types that must be supported by<br />

JMS providers. Although JMS defines the <strong>Message</strong> interfaces, it doesn't define their<br />

implementation. This allows vendors to implement and transport messages in their own<br />

way, while maintaining a consistent and standard interface for the JMS application<br />

developer. The six message interfaces are <strong>Message</strong> and its five sub-interfaces: Text<strong>Message</strong>,<br />

Stream<strong>Message</strong>, Map<strong>Message</strong>, Object<strong>Message</strong>, and Bytes<strong>Message</strong>.<br />

The <strong>Message</strong> interfaces are defined according to the kind of payload they are designed to<br />

carry. In some cases, <strong>Message</strong> types were included in JMS to support legacy payloads that<br />

are common and useful, which is the case with the text, bytes, and stream message types.<br />

In other cases, the <strong>Message</strong> types were defined to facilitate emerging needs; for example,<br />

Object<strong>Message</strong> can transport serializable <strong>Java</strong> objects. Some vendors may provide other<br />

proprietary message types. Progress' SonicMQ and SoftWired's iBus, for example, provide<br />

an XML<strong>Message</strong> type that extends the Text<strong>Message</strong>, allowing developers to deal with the<br />

message directly through DOM or SAX interfaces. The XML<strong>Message</strong> type may become a<br />

standard message type in a future version of the specification. At the time of this writing,<br />

Sun Microsystems was starting discussions about adding an XML<strong>Message</strong> type.<br />

3.4.1 <strong>Message</strong><br />

The simplest type of message is the javax.jms.<strong>Message</strong>, which serves as the base interface<br />

to the other message types. As shown below, the <strong>Message</strong> type can be created and used as a<br />

JMS message with no payload:<br />

// Create and deliver a <strong>Message</strong><br />

<strong>Message</strong> message = session.create<strong>Message</strong>( );<br />

publisher.publish(message);<br />

...<br />

// Receive a message on the consumer<br />

public void on<strong>Message</strong>(<strong>Message</strong> message){<br />

// No payload, process event notification<br />

}<br />

This type of message contains only JMS headers and properties, and is used in event<br />

notification. An event notification is a broadcast, warning, or notice of some occurrence. If<br />

the business scenario requires a simple notification without a payload, then the lightweight<br />

<strong>Message</strong> type is the most efficient way to implement it.<br />

42

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

Saved successfully!

Ooh no, something went wrong!