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.
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.
3.149.236.27