Spring JMS and ActiveMQ Integration – publish/subscribe domain

This tutorial will show you how we can send a message to Topic using publish/subscribe messaging system in Apache ActiveMQ. For more information on publish/subscribe messaging system please read tutorial http://www.roytuts.com/configure-jms-client-using-glassfish-3/

Before you moving forward please read the tutorial http://www.roytuts.com/apache-activemq-configuration-in-windows/ for configuring ActiveMQ but do not create any Topic.

Now we will look into the following steps in order to implement publish/subscribe messaging system.

1. Create a class called MessagePublisher that will produce message or send message to the destination – Topic.

 2. Create a class called MessageSubscriber1 that will receive message from the destination – Topic.

3. Create a class called MessageSubscriber2 that will receive message from the destination – Topic.

 4. Create an XML property file that will hold all key/value pairs for the application. Put this file under src/main/resources

5. Create an XML configuration file that will hold all JMS related configuration for the application. Put this file under src/main/resources/jms

 6. Create an XML file that will load all other resources and configure support for annotation in the application. Put this file under src/main/resources/spring

 7. Create a test class under src/test/java that will run the MessagePublisher.

8. Run the MessagePublisher.


9. Now again click on Topics in ActiveMQ Web Console. You will not see any message in the IN_TOPIC because as soon as you ran the MessagePublisher the message has been consumed by MessageSubscriber1 and MessageSubscriber2 and now you will see one message was enqueued but two messages has been dequeued because there were two message subscribers. We do not need to run the MessageSubscriber1 or MessageSubscriber2 because it is an asynchronous messaging system and MessageSubscriber1 or MessageSubscriber2 is already registered to the Spring’s DefaultMessageListenerContainer. So when a message arrives to the Topic, the message gets automatically consumed by the subscriber’s onMessage() method.

ActiveMQ Spring JMS publish subscribe

Here is the pom.xml


That’s all. Thank you for reading.

Leave a Reply

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.