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.

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

listening, or even if nobody is listening. For example, consider a publisher that broadcasts<br />

stock quotes. If any particular subscriber is not currently connected and misses out on a<br />

great quote, the publisher is not concerned. Likewise, our Wholesaler class didn't care<br />

whether there were any subscribers when it sent price quotes: if a Retailer missed a great<br />

price, that wasn't the Wholesaler's problem. In contrast, a point-to-point session is likely to<br />

be intended for a one-on-one conversation with a specific application at the other end. In<br />

this scenario, every message really matters.<br />

The range and variety of the data that the messages represent can be a factor as well. Using<br />

pub/sub, messages are dispatched to the consumers based on filtering that is provided<br />

through the use of specific topics. Even when messaging is being used to establish a oneon-one<br />

conversation with another known application, it can be advantageous to use<br />

pub/sub with multiple topics to segregate different kinds of messages. Each kind of<br />

message can be dealt with separately through its own unique consumer and on<strong>Message</strong>( )<br />

handler.<br />

Point-to-point is more convenient when you want one receiver to process any given<br />

message once-and-only-once. This is perhaps the most critical difference between the two<br />

models: point-to-point guarantees that only one consumer processes a given message. This<br />

is extremely important when messages need to be processed separately but in tandem,<br />

balancing the load of message processing across many JMS clients. Another advantage is<br />

that the point-to-point model provides a QueueBrowser that allows the JMS client to peek<br />

ahead on the queue to see messages waiting to be consumed. Pub/sub does not include a<br />

browsing feature. We'll talk more about the QueueBrowser later in this chapter.<br />

5.2 The QWholesaler and QRetailer<br />

Let's rethink our wholesaler/retailer scenario in terms of the distinction between the two<br />

message models. The pub/sub model is well suited for sending price quotes, since that is<br />

naturally a one-to-many broadcast. However, when the retailer responds with a "buy"<br />

order, it is more appropriate to use a point-to-point queue. In the real world, retailers<br />

naturally deal with many wholesalers, and you would only send a purchase order to the<br />

wholesaler that offered the quote.<br />

From the user's perspective, the QWholesaler and QRetailer examples that we'll develop<br />

now are functionally equivalent to the Wholesaler and Retailer examples introduced in<br />

Chapter 4. The difference lies in the use of the point-to-point queue for responses to price<br />

quotes. If you wish to see these classes in action, start your JMS provider and execute the<br />

following commands, each in a separate command window:<br />

java chap5.B2B.Q Wholesaler localhost username password<br />

java chap5.B2B.Q Retailer localhost username password<br />

5.2.1 The QRetailer Class<br />

Here is the listing for the QRetailer class in its entirety. Later, we will examine this class<br />

in detail:<br />

import java.util.StringTokenizer;<br />

import java.util.Properties;<br />

import javax.naming.InitialContext;<br />

71

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

Saved successfully!

Ooh no, something went wrong!