4.3. A Simple Publish/Subscribe Example

This section describes the publishing and subscribing programs in a pub/sub example that uses a message listener to consume messages asynchronously. This section then explains how to compile and run the programs, using the J2EE SDK 1.3.

4.3.1. Writing the Pub/Sub Client Programs

The publishing program, SimpleTopicPublisher.java, performs the following steps:

1.
Performs a JNDI API lookup of the TopicConnectionFactory and topic
2.
Creates a connection and a session
3.
Creates a TopicPublisher
4.
Creates a TextMessage
5.
Publishes one or more messages to the topic
6.
Closes the connection, which automatically closes the session and TopicPublisher

The receiving program, SimpleTopicSubscriber.java

Get Java™ Message Service API Tutorial and Reference: Messaging for the J2EE™ Platform now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.