How do I go about setting up a Mule Publisher Subscriber model using ActiveMQ? I’ve been looking around the web for a complete example that is simple and easy to follow but was not able to. All i want to do is sent a string to a MQ topic (using a publisher) and then read the message from the topic (using a subscribeer). Nothing too complex
-
1Need more info on what you are up to. Publishing data to AMQ from Mule is straight forward but can be done in many ways depending on the complexity of your scenario – Petter Nordlander Jan 19 '16 at 15:21
1 Answers
The following step describe how to use Apache MQ as part of a Mule Pub/Sub Model
Setting up Active MQ
• Download ActiveMQ by Apache. I’m currently using version “apache-activemq-5.2.0”
• Extract download into a known location (eg C:\MQ\apache-activemq-5.2.0)
• Setup Environment Variable for ACTIVEMQ_HOME (C:\MQ\apache-activemq-5.2.0\bin\win32)
• Run C:\MQ\apache-activemq-5.2.0\bin\win32\InstallService.bat (to uninstall run unInstallService.bat)
• To verify, go to control panel -> administrative tools -> services and look for the ActiveMQ service
• Start the Service (Can set it to start automatically)
• Browse to http://localhost:8161/admin/ to see MQ dashboard
===========
Setting up a Mule Publisher
- Create a Mule Project
- Right click on the Project properties and select “Java Build Path”
Select the “Libraries” tab and click on “Add External Jars”
Browse to the location of the MQ jar file (eg C:\MQ\apache-activemq-5.2.0\ activemq-all-5.2.0.jar) and select OK to accept
Newly added Jar file should be in the list
- Select OK to close the properties window
Create a Global Element for the project
Select “Create”
Select Connector Configuration --> JMS --> ActiveMQ and OK to accept (MQ will have default settings which is OK for this example)
Drag a HTTP Connector and Configure
- Set port number to an port that is free (eg 8075)
- Set “Path” to “/pub”
Drag a JMS Connector to the Right of the HTTP connector and Configure
- Set Exchange Pattern to “OneWay”
- Select “Topic” and give it a name (eg MyLocalTopic)
- From the Connector Configuration drop down – Select the Global Active-MQ element created earlier
- Set Transaction (eg “XATransaction”)
- Set Action (eg “ None”)
The Flow should be like the below
<flow name="pubsubFlow1">
<http:listener config-ref="HTTP_Listener_Configuration_8075" path="/pub" doc:name="HTTP_8075"/>
<jms:outbound-endpoint connector-ref="Active_MQ" doc:name="JMS" topic="MyLocalTopic">
<xa-transaction action="NONE"/>
</jms:outbound-endpoint>
Setting up a Mule Subscriber
- Create a Mule Project
- Right click on the Project properties and select “Java Build Path”
Select the “Libraries” tab and click on “Add External Jars”
Browse to the location of the MQ jar file (eg C:\MQ\apache-activemq-5.2.0\ activemq-all-5.2.0.jar) and select OK to accept
- Newly added Jar file should be in the list
- Select OK to close the properties window Create a Global Element for the project
- Select “Create”
- Select Connector Configuration --> JMS --> ActiveMQ and OK to accept (MQ will have default settings which is OK for this example)
Drag a JMS Connector onto the design pallet and configure
- Set Exchange Pattern to “OneWay”
- Select “Topic” and give it a name (eg MyLocalTopic)
- From the Connector Configuration drop down – Select the Global Active-MQ element created earlier
- Set Transaction Type (eg “NoTransaction ”)
- Set Action (eg “ None”)
- Drag a “Logger” connector to the right of the JMS connector and configure
- Set message eg “FIRST SUBSCRIBER - PAYLOAD FROM MQ #[payload]”
Create a Second Mule subscriber project as described above, but this time set the Logger message to “SECOND SUBSCRIBER - PAYLOAD FROM MQ #[payload]”. This will allow you to see two subscribers subscribing to the same topic
<flow name="pubsub_readqueueFlow">
<jms:inbound-endpoint topic="MyLocalTopic" connector-ref="Active_MQ" doc:name="JMS">
</jms:inbound-endpoint>
<logger message="FIRST SUBSCRIBER - PAYLOAD FROM MQ #[payload]" level="INFO" doc:name="Logger"/>
</flow>
Applications in Action Publishing a message to the Queue
- Run the publishing application
- Use a Soap Client (eg SoapUI) to post a message to http://localhost:8075/pub
- From the Active-MQ dashboard browse to topics(http://localhost:8161/admin/topics.jsp) to see the newly added message
Consuming the message
- Run both subscribing application
- Refresh the dash board to see that consumers added. (see example of my Dashboard below)
You are also able to send messages to the topic my Clicking on the “MyLocalTopic” topic. Enter a message in the “Message Body” tex filed and sent. The message count in the Topic should increment.
Subscriber Output

- 702
- 1
- 6
- 28