ActiveMQ publish/subscribe messaging 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 MessagePublisherOne that will produce message or send message to the destination – Topic.

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

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

4. Create a class called JmsConstants that will hold all constants related to JMS.

6. Create a main class that will run the MessagePublisherOne.

7. Create a main class that will run the MessageSubscriberOne.

8. Create a main class that will run the MessageSubscriberTwo.

9. Go to the ActiveMQ Web Console and click on Topics. You will see there are two consumers in the IN_TOPIC. In order to receive message by message subscriber(s), the message subscriber(s) must run before the message publisher.

Apache ActiveMQ publish subscribe messaging domain

10. Run the MessageSubscriberOne. The below output in the console you will receive as soon as you run the MessagePublisherOne.

Output 

11. Run the MessageSubscriberOne. The below output in the console you will receive as soon as you run the MessagePublisherOne.

Output 

12. Run the MessagePublisherOne.

Output 

13. 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 MessagePublisherOne the message has been consumed by MessageSubscriberOne and MessageSubscriberTwo and now you will see one message was enqueued but two messages has been dequeued because there were two message subscribers.

Apache ActiveMQ publish subscribe messaging domain

Here is the pom.xml

 

That’s all. Thank you for reading.

Soumitra

Software Professional, I am passionate to work on web/enterprise application. For more information please go to about me. You can follow on Twitter. You can be a friend on Facebook or Google Plus or Linkedin

Leave a Reply

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

Time limit is exhausted. Please reload CAPTCHA.