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.
<strong>Java</strong> <strong>Message</strong> <strong>Service</strong><br />
We can demonstrate durable subscriptions with the B2B example. If you still have the<br />
Retailer application up and running, try simulating an abnormal shutdown by typing Ctrl-<br />
C in the command window. Leave the Wholesaler running. In the command window for<br />
the wholesaler application, type:<br />
Surfboards, 500.00, 299.99<br />
Hockey Sticks, 20.00, 9.99<br />
Once the deals have been entered, restart the Retailer application:<br />
java chap4.B2B.Retailer localhost username password<br />
The first time you ran the Retailer application, a topic was registered as durable. When<br />
you abnormally terminated the application, the subscription information was retained by<br />
the JMS provider. When the Retailer application comes back up, the surfboards and<br />
hockey sticks messages are received, processed, and responded to. Because the Retailer<br />
had a durable subscription to the "Hot Deals" topic, the JMS server saved the messages<br />
that arrived while the Retailer was down. The messages were then delivered when the<br />
Retailer resubscribed to the topic.<br />
Here's how we set up the durable subscription. A durable subscription is created by a<br />
TopicSession object, the same as with a nondurable subscription. The Retailer class<br />
obtains a durable subscription in its constructor:<br />
public Retailer( String broker, String username, String password){<br />
try {<br />
...<br />
hotDealsTopic = (Topic)jndi.lookup("Hot Deals");<br />
javax.jms.TopicSubscriber subscriber =<br />
session.createDurableSubscriber(hotDealsTopic,<br />
"Hot Deals Subscription");<br />
subscriber.set<strong>Message</strong>Listener(this);<br />
connect.start( );<br />
....<br />
}<br />
The createDurableSubscriber( ) method takes two parameters: a topic name, and a<br />
subscription name. In our example we are using the String "Hot Deals Subscription" to<br />
identify the subscription name. While topic names are specified as being supported as JMS<br />
administered objects, subscription names are not. While not required by JMS, it is good<br />
practice for a JMS provider to provide an administration tool that monitors active<br />
subscription names, as illustrated in Figure 4.2.<br />
A durable subscription's uniqueness is defined by the client ID and the subscription name.<br />
In the event that the client disconnects without unsubscribing, a JMS provider will store<br />
these messages until they can be delivered later. Upon reconnecting and resubscribing, the<br />
JMS provider will match up the messages based on these two identifiers, and deliver them<br />
to the subscriber.<br />
You might think that the client ID and the topic would be enough for the provider to<br />
uniquely identify a durable subscription. However, a client may have multiple<br />
subscriptions on the same topic; for example, a client may want to use different message<br />
selectors to sort the incoming messages. (<strong>Message</strong> selectors are discussed in detail in<br />
62