The Publish/Subscribe Message Domain

So far, you have seen how to create a simple point-to-point application with a single sender and receiver. Now you will build a simple bulletin board application to demonstrate the features of the publish/subscribe model.

For this example, a TopicPublisher will be produced that creates messages and publishes them to a bulletin board. A single TopicSubscriber will asynchronously listen for messages. The messages will be printed to the screen—one at time—in the order they were received. The program then exits.

Remember that, unlike messages in a queue, messages in a topic are immediately distributed to all subscribers. As a result, the timing of the publisher and the subscriber becomes important. Apart from this, the publisher/subscriber code is very similar to the sender/receiver code. In fact, the publisher code is a copy of the previous sender code with all references to Queue<object> changed to Topic<object>.

Table 9.5 lists the JMS objects in the publish/subscribe domain.

Table 9.5. Components in the JMS Publish/Subscribe Message Domain
Component Description
TopicConnectionFactory Administered object used to create an object that implements the TopicConnection interface
TopicConnection Active connection to a JMS provider
TopicSession Provides methods for creating objects that implement the TopicPublisher and TopicSubscriber interfaces
TopicPublisher Used by a client to publish a message to a topic
TopicSubscriber Used to receive messages sent to a topic
Durable TopicSubscriber Used to receive messages published when the subscriber is inactive
Topic Encapsulates the JMS provider topic name

Because subscribers only receive messages when they are active, it would be nice to be able to test for active subscribers before publishing to a topic. Unfortunately, this is not possible.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.191.233.43