O'Reilly - Java Message Service
O'Reilly - Java Message Service
O'Reilly - Java Message Service
You also want an ePaper? Increase the reach of your titles
YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.
Chapter 4. Publish-and-Subscribe Messaging<br />
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
This chapter focuses on the publish-and-subscribe (pub/sub) messaging model that was<br />
introduced in Chapter 2. The pub/sub messaging model allows a message producer (also<br />
called a publisher) to broadcast a message to one or more consumers (called subscribers).<br />
There are three important aspects of the pub/sub model:<br />
• <strong>Message</strong>s are pushed to consumers, which means that consumers are delivered<br />
messages without having to request them. <strong>Message</strong>s are exchanged through a<br />
virtual channel called a topic. A topic is a destination where producers can publish,<br />
and subscribers can consume, messages. <strong>Message</strong>s delivered to a topic are<br />
automatically pushed to all qualified consumers.<br />
• As in enterprise messaging in general, there is no coupling of the producers to the<br />
consumers. Subscribers and publishers can be added dynamically at runtime,<br />
which allows the system to grow or shrink in complexity over time.<br />
• Every client that subscribes to a topic receives its own copy of messages published<br />
to that topic. A single message produced by one publisher may be copied and<br />
distributed to hundreds, or even thousands of subscribers.<br />
In Chapter 2 you learned the basics of the pub/sub model by developing a simple chat<br />
client. In this chapter we will build on those lessons and examine more advanced features<br />
of this model, including guaranteed messaging, topic-based addressing, durable<br />
subscriptions, request-reply, and temporary topics.<br />
4.1 Getting Started with the B2B Application<br />
In this chapter we abandon the simple chat example for a more complex and real-world<br />
Business-to-Business (B2B) scenario. In our new example, a wholesaler wants to distribute<br />
price information to retailers, and the retailers want to respond by generating orders. We'll<br />
implement this scenario using the publish-and-subscribe model: the wholesaler will<br />
publish messages containing new prices and hot deals, and the retailers will respond by<br />
creating their own messages to order stock.<br />
This scenario is typical of many Business-to-Business operations. We call the clients<br />
retailers and wholesalers, but these names are really only for convenience. There's little<br />
difference between our wholesaler/retailer scenario and a stock broker broadcasting stock<br />
prices to investors, or a manufacturer broadcasting bid requests to multiple suppliers. The<br />
fact that we use a retailer and a wholesaler to illustrate our example is much less important<br />
than the way we apply JMS.<br />
Our simple trading system is implemented by two classes, both of which are JMS clients:<br />
Wholesaler and Retailer . In the interest of keeping the code simple, we won't implement<br />
a fancy user interface; our application has a rudimentary command-line user interface.<br />
53