Section 11.5. Publish-Subscribe Messaging


11.5. Publish-Subscribe Messaging

Publish-subscribe messaging involves one or more MessageProducers "publishing" messages to a particular topic and one or more MessageConsumers "subscribing" to the topic and receiving any messages published to it. The JMS provider is responsible for delivering a copy of any message sent to a topic to all subscribers of the topic at the time that the message is received. Unlike point-to-point messaging, where messages are kept on the queue until a receiver reads them, any messages received at a topic while a subscriber is not active (e.g., hasn't subscribed to the topic yet or subscribed and then went out of scope or exited) are lost with respect to that subscriber.

Publish-subscribe messaging is performed in JMS using the topic-related interfaces and classes in the javax.jms package. Topics are represented by Topic objects, which are looked up in JNDI from the JMS provider. TopicConnectionFactory objects are looked up in JNDI as well and used to create TopicConnections. TopicConnections and Topics are used to create TopicSessions, which are in turn used to create TopicPublishers and TopicSubscribers.

11.5.1. Sample Client

The downloadable source code for this book includes a publish-subscribe client, PubSubMessagingClient, that mirrors the PTPMessagingClient in Example 11-2. The structure and function of the client is virtually identical to that described for the PTPMessagingClient, except that topics, subscribers, and publishers are used instead of queues, receivers, and senders. The only significant difference with this client is it doesn't have a "browse" option, since browsing a topic is not possible. As they arrive, topics deliver their messages to any subscribers currently attached to the topic, otherwise they are dropped, so browsing a topic's contents doesn't make much sense.

11.5.2. Durable Subscriptions

If a client needs to guarantee delivery of messages from a Topic beyond the lifetime of a single subscriber, it can register a durable subscription with the JMS provider for the target Topic. A durable subscription to a Topic is made using the createDurableSubscriber( ) methods on a TopicSession. In its simplest form, a durable subscriber is created by specifying a Topic and a subscriber name:

     TopicConnection tConn = ...;     tConn.setClientID("client-1");     TopicSession tSession =       tConn.createTopicSession(false, Session.AUTO_ACKNOWLEDGE);     TopicSubscriber durableSub =       tSession.createDurableSubscriber(topic, "subscriber-1"); 

This registers a durable subscription to the Topic under the name "subscriber-1." Durable subscriptions and their names are associated by the JMS provider with the client ID of the client that created them (see "Client identifiers" earlier in this chapter for details on client IDs). Here, we're setting our client ID to "client-1" by calling setClientID( ) on the TopicConnection.

As long as this TopicSubscriber is live, it will receive any messages published to the Topic, as it would if it were a nondurable subscriber. But if the subscriber dies (goes out of scope or the client dies), the JMS provider will retain messages on behalf of the named subscriber (based on its client identifier), until another durable subscriber attaches to the topic from the same connection using the same client ID and specifying the same subscriber name. Any pending messages will be delivered to the new subscriber when it attaches.

It's important to remember that durable subscriptions can be a costly resource on the JMS provider. The provider will have to create database records, or otherwise allocate server resources, in order to preserve the subscription information and any pending messages for the client. If there are many durable subscriptions or if the number of pending messages being held on the server for subscribers becomes large, this can eventually have a significant impact on the performance of the JMS provider. So durable subscriptions should be used with discretion.



Java Enterprise in a Nutshell
Java Enterprise in a Nutshell (In a Nutshell (OReilly))
ISBN: 0596101422
EAN: 2147483647
Year: 2004
Pages: 269

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net