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 />

multiple consumers can use the same queue, but each message delivered to the queue can<br />

only be received by one of the queue's consumers. How messages delivered to a queue are<br />

distributed to the queue's consumers depends on the policies of the JMS provider. Some<br />

JMS providers use load-balancing techniques to distribute messages evenly among<br />

consumers, while others will use more arbitrary policies.<br />

<strong>Message</strong>s intended for a p2p queue can be either persistent or nonpersistent. Persistent<br />

messages survive JMS provider failures, while nonpersistent messages do not. <strong>Message</strong>s<br />

may have a priority and an expiration time. One important difference between point-topoint<br />

and publish/subscribe messaging is that p2p messages are always delivered,<br />

regardless of the current connection status of the receiver. Once a message is delivered to a<br />

queue, it stays there even if there is no consumer currently connected. More details on<br />

failure scenarios can be found in Chapter 6.<br />

The interfaces for connecting, creating, sending and receiving are similar to the interfaces<br />

for topics, as shown in Table 5.1.<br />

Table 5.1. Interfaces for Topics and Queues<br />

Topic Queue<br />

TopicConnectionFactory QueueConnectionFactory<br />

TopicSession QueueSession<br />

TopicPublisher QueueSender<br />

TopicSubscriber QueueReceiver<br />

createTopicConnection( ) createQueueConnection( )<br />

createTopicSession( ) createQueueSession( )<br />

createTemporaryTopic( ) createTemporaryQueue( )<br />

... ...<br />

5.1.1 When to Use Point-to-Point Messaging<br />

First, let's talk about why two distinct models exist. The rationale behind the two models<br />

lies in the origin of the JMS specification. JMS started out as a way of providing a<br />

common API for accessing existing messaging systems. At the time of its conception,<br />

some messaging vendors had a p2p model, and some had a pub/sub model. Hence JMS<br />

needed to provide an API for both models to gain wide industry support. The JMS 1.0.2<br />

specification does not require a JMS provider to support both models, although most JMS<br />

vendors do.<br />

Almost anything that can be done with the pub/sub model can be done with point-to-point,<br />

and vice versa. An analogy can be drawn to developers' programming language<br />

preferences. In theory, any application that can be written with Pascal can also be written<br />

with C. Anything that can be written in C++ can also be written in <strong>Java</strong>. In some cases it<br />

comes down to a matter of preference, or which model you are already familiar with.<br />

In most cases, the decision about which model to use depends on the distinct merits of<br />

each model. With pub/sub, any number of subscribers can be listening on a topic, all<br />

receiving copies of the same message. The publisher may not care if everybody is<br />

70

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

Saved successfully!

Ooh no, something went wrong!