23.07.2013 Views

O'Reilly - Java Message Service

O'Reilly - Java Message Service

O'Reilly - Java Message Service

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Chapter 5. Point-to-Point Messaging<br />

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

This chapter focuses on the point-to-point (p2p) messaging domain. Many of the concepts<br />

of p2p messaging are similar to those we learned in Chapter 4. To avoid redundancy, this<br />

chapter highlights the areas where the two models are the same, and focuses on the areas<br />

where the two models differ.<br />

In the p2p model, the producer is called a sender and the consumer is called a receiver.<br />

The most important characteristics of the point-to-point model are:<br />

• <strong>Message</strong>s are exchanged through a virtual channel called a queue. A queue is a<br />

destination to which producers send messages, and a source from which receivers<br />

consume messages.<br />

• Each message is delivered only to one receiver. Multiple receivers may connect to<br />

a queue, but each message in the queue may only be consumed by one of the<br />

queue's receivers.<br />

• <strong>Message</strong>s are ordered. A queue delivers messages to consumers in the order they<br />

were placed in the queue by the message server. As messages are consumed they<br />

are removed from the head of the queue.<br />

• There is no coupling of the producers to the consumers. Receivers and senders can<br />

be added dynamically at runtime, allowing the system to grow or shrink in<br />

complexity over time. (This is a characteristic of messaging systems in general.)<br />

In this chapter, we introduce new versions of our Wholesaler and Retailer classes, called<br />

QWholesaler and QRetailer. QWholesaler still uses pub/sub to broadcast price quotes, while<br />

QRetailer uses a p2p queue to respond with "buy" orders instead of publishing to a<br />

temporary topic.<br />

The rest of the chapter focuses on the unique capabilities offered by p2p: examining a<br />

queue using the QueueBrowser interface, and load balancing among multiple recipients of a<br />

queue.<br />

5.1 Point-to-Point and Publish-and-Subscribe<br />

Like publish/subscribe messaging, point-to-point messaging is based on the concept of<br />

sending a message to a named destination. The actual network location of the destination is<br />

transparent to the sender, because the p2p client works with a Queue identifier obtained<br />

from a JNDI namespace, the same way that a pub/sub client uses a Topic identifier.<br />

The pub/sub model is based on a push model, which means that consumers are delivered<br />

messages without having to request them. <strong>Message</strong>s are exchanged through a virtual<br />

channel called a topic. From the viewpoint of the receiver, a p2p queue can either push or<br />

pull messages, depending on whether it uses the asynchronous on<strong>Message</strong>( ) callback, or a<br />

synchronous receive( ) method. Both of these methods are explained in more detail later.<br />

In the p2p model, as in the pub/sub model, there is no direct coupling of the producers to<br />

the consumers. The destination queue provides a virtual channel that decouples consumers<br />

from producers. In the pub/sub model, multiple consumers that subscribe to the same topic<br />

each receive their own copy of every message addressed to that topic. In the p2p model,<br />

69

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

Saved successfully!

Ooh no, something went wrong!