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.

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

line is passed to the Chat class's write<strong>Message</strong>( ) method. This method uses the<br />

TopicPublisher to deliver a message to the topic:<br />

protected void write<strong>Message</strong>(String text) throws JMSException{<br />

Text<strong>Message</strong> message = pubSession.createText<strong>Message</strong>( );<br />

message.setText(username+" : "+text);<br />

publisher.publish(message);<br />

}<br />

The TopicPublisher objects deliver messages to the topic asynchronously. Asynchronous<br />

delivery and consumption of messages is a key characteristic of <strong>Message</strong>-Oriented<br />

Middleware; the TopicPublisher doesn't block or wait until all the subscribers receive the<br />

message. Instead, it returns from the publish( ) method as soon as the message server<br />

receives the message. It's up to the message server to deliver the message to all the<br />

subscribers for that topic.<br />

2.1.2.8 The TopicSubscriber<br />

The TopicSubscriber is created using the subSession and the chatTopic:<br />

// Look up a JMS topic<br />

Topic chatTopic = (Topic)jndi.lookup(topicName);<br />

// Create a JMS publisher and subscriber<br />

TopicPublisher publisher =<br />

pubSession.createPublisher(chatTopic);<br />

TopicSubscriber subscriber =<br />

subSession.createSubscriber(chatTopic);<br />

A TopicSubscriber receives messages from a specific topic. The Topic object argument<br />

used in the createSubscriber( ) method identifies the topic from which the<br />

TopicSubscriber will receive messages.<br />

The TopicSubscriber receives messages from the message server one at a time (serially).<br />

These messages are pushed from the message server to the TopicSubscriber<br />

asynchronously, which means that the TopicSubscriber does not have to poll the message<br />

server for messages. In our example, each chat client will receive any message published<br />

by any of the other chat clients. When a user enters text at the command line, the text<br />

message is delivered to all other chat clients that subscribe to the same topic.<br />

The pub/sub messaging model in JMS includes an in-process <strong>Java</strong> event model for<br />

handling incoming messages. This is similar to the event-driven model used by <strong>Java</strong><br />

beans. [3] An object simply implements the listener interface, in this case the<br />

<strong>Message</strong>Listener, and then is registered with the TopicSubscriber. A TopicSubscriber may<br />

have only one <strong>Message</strong>Listener object. Here is the definition of the <strong>Message</strong>Listener<br />

interface used in JMS:<br />

[3] Although the in-process event model used by TopicSubscriber is similar to the one used in<br />

<strong>Java</strong> beans, JMS itself is an API and the interfaces it defines are not <strong>Java</strong> beans.<br />

package javax.jms;<br />

public interface <strong>Message</strong>Listener {<br />

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

}<br />

29

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

Saved successfully!

Ooh no, something went wrong!