What is this all about
This site shows 2 simple examples of how to use JMS messaging. One is for the Send and Receive with synchronous messaging and the other is for the Publish and Subscribe paradigm. Of course there are many ways messaging systems can be setup and there are many variations on each scheme. Here we attempt to demonstrate the most used scenarios with the simplest examples possible. Think Quick Start.
This example assumes you have some basic understanding of JMS. Minimally, you need to understand that you need "Broker" to handle the messaging. These examples use ActiveMQ as an imbedded broker. To run, you'll need to download the latest ActiveMQ and make all the jars available on the classpath for this application. And by all, I mean you also need to include the optional jars as well. Also, note that ActiveMQ requires jdk1.5 to use the embedded broker (contrary to some previously published information).
Publish & Subscribe
For the Publish and Subscribe example I also have setup 3 classes: a Publisher that will publish a message about a topic, a Subscriber that wants to be informed of messages on the same topic, and a controlling class ExamplePublishAndSubscribe with a main() that runs a simple scenario. Below is the
ExamplePublishAndSubscribe.javacode where you will see that this defines a TOPIC that the messages are set to. The startBroker() and getJmsConnectionFactory() methods to are the same as in the first example. The main method does the following:
- Starts the embedded broker. (Normally, you'd be using an existing broker that would be running under WebSphere or other.)
- Starts a Publisher on a separate thread (code below). This guy actually starts publishing to the topic, but there are no subscribers initially. The main thread waits 3 seconds before continuing.
- A Subscriber is started. The JMS subsystem actually puts the listener on a separate thread so we can start it from the main thread. In this case the Subscriber class actually implements the MessageListener interface and will be notified of the messages.
- After allowing messages to run for 10 seconds the publisher and subscriber are stopped.
The Publisher class performs the following key steps in a loop to continually send messages until it is stopped.
- Creates the JMS Connection, Session, and Topic - The are basics of JMS Messaging for this scenario.
- Creates a MessageProducer on the Topic with limit lifetime of the messages (of course many other options would apply here, but are beyond the scope of this example).
- Creates a MessageConsumer on a temporary receiving Queue - This is where the response message will be returned.
- Creates a messsage.
- Sends the message.
- Closes the connection.
In the main method shown above, the Subscriber object was created and startListening() method was called (see above and below). On startListening, a connection to the topic was created and then the object registered itself as the MessageListener. When a message arives on that topic, in this case sent by the Publisher object, the onMessage method gets called with the message (see below). This will happen for each message. Running this code you should get output like the following: