Using JMS synchronously in Mule ESB

This tutorial will show you how to use Mule JMS Transport synchronously in Mule based application. As JMS is inherently asynchronous in nature, you will usually use JMS inbound endpoints with one-way message-exchange patterns—sending messages and not waiting around for a response. Sometimes, however, you will want to wait for a response from a message you are sending. You can accomplish this by setting the exchange pattern on a JMS inbound endpoint to request-response.

Prerequisites

Mule Studio 3.x(Anypoint Studio) (Download from https://www.mulesoft.com/platform/studio)
Maven 3.2.1 (Download from https://maven.apache.org/download.cgi?Preferred=ftp://mirror.reverse.net/pub/apache/)
JDK 1.7 (Download from http://www.oracle.com/technetwork/java/javase/downloads/index.html)
Configure JDK, Maven and Mule Studio

Step 1. First install JDK
Step 2. Add the Java_Home/bin directory to your system’s PATH.
Step 3. After downloading Maven, extract it to a drive
Step 4. Add the M2_Home/bin directory to your system’s PATH.
Step 5. Download and extract Mule Studio to a drive
Step 6. Now start Mule Studio by clicking on AnypointStudio exe icon in the folder <physical drive>/AnypointStudio
Step 7. Once started, close the startup page
Step 8. In Mule Studio, go to Window -> Preferences. Expand Java, then click on Installed JREs. Add JDK 1.7 and select it. In expanded Java, click on Compiler and select the compiler level as 1.7
Step 9. Now expand Anypoint Studio and click on Maven Settings. Then select appropriate Maven installation home directory using Browse button.
Step 10. If you want you can input Default groupId for new projects, it will save your time every time when you want to create a new project.

Create Mule project in Mule Studio
Now we will see how to create a new project in Mule Studio(Anypoint Studio).

Step 1. In Anypoint Studio, go to File -> New -> Mule Project
Step 2. Input Project Name: mule, Runtime is by default selected, tick on Use Maven; here the artifactId is automatically picked up from the Project Name:, the Group Id is picked up from the Default groupId for new projects and version is also a default value.
Step 3. Click Next and verify the JDK, mainly select Use default JRE(currently ‘jdk1.7.0_x’)
Step 4. Click on Next and click on Finish.

Example

Step 1. Create the synchronous-jms.xml file under src/main/app directory and put the below file connector. While you create the xml file you will see on red mark on each file connector. Do not worry, red mark will be disappeared once you modify the xml file as given in Step 3.


synchronous jms mule

Step 2. Open the synchronous-jms.xml file and click on Configuration XML view in the Editor.
Step 3. Modify the synchronous-jms.xml file as shown below

<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:jms="http://www.mulesoft.org/schema/mule/jms"
	xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns="http://www.mulesoft.org/schema/mule/core"
	xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
	xmlns:spring="http://www.springframework.org/schema/beans" version="EE-3.5.1"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">
	<flow name="synchronous-jms-flow" doc:name="synchronous-jms-flow">
		<jms:inbound-endpoint exchange-pattern="request-response"
			queue="in.queue" connector-ref="Active_MQ" doc:name="JMS" />
        <component class="com.roytuts.mule.jms.queue.MessageConsumer" doc:name="Java"/>
	</flow>
</mule>

The request-response exchange pattern on the JMS inbound endpoint indicates that a response is expected. To facilitate this, the JMS transport will create a temporary queue for the response data and set the queue name as the Reply-To property of the responding JMS message.

Step 4. Create a Java class under src/main/java directory

package com.roytuts.mule.jms.queue;
public class MessageConsumer {
	public String getMessage(String msg) {
		System.out.println("Message received : " + msg);
		return msg;
	}
}

Step 5. Add Active MQ dependency to the pom.xml file

<properties>
    ...
    <activemq.version>5.11.1</activemq.version>
    ...
</properties>
...
<dependencies>
    ...
    <!-- activemq -->
    <dependency>
        <groupId>org.apache.activemq</groupId>
        <artifactId>activemq-all</artifactId>
        <version>${activemq.version}</version>
    </dependency>
    ...
</dependencies>

Running the Active MQ

Extract the downloaded zip activemq in physical drive. Then open a command prompt and navigate to the <physical drive>apache-activemq-5.11.1bin and execute the command activemq start

Then open browser and hit the URL http://localhost:8161/admin and when prompted for username/password, type admin/admin and click on Queues.

Running the application

Now do a right-click on the synchronous-jms.xml file or on the mule project and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs

**********************************************************************
* Application: mule                                                  *
* OS encoding: Cp1252, Mule encoding: UTF-8                          *
*                                                                    *
* Agents Running:                                                    *
*   DevKit Extension Information                                     *
*   Batch module default engine                                      *
*   Clustering Agent                                                 *
*   JMX Agent                                                        *
**********************************************************************

Now create Queue in.queue in the ActiveMQ console


synchronous jms mule

Send message to in.queue


synchronous jms mule

Console output

Message received : Synchronous Message
INFO  2016-10-06 18:17:52,171 [ActiveMQ Session Task-2] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.jms.transformers.ObjectToJMSMessage
INFO  2016-10-06 18:17:52,259 [ActiveMQ Session Task-2] org.mule.transport.jms.JmsReplyToHandler: Reply Message sent to: queue://in.queue with correlationID:ID:moni-pc-49423-1475757756320-3:1:1:1:1
Message received : Synchronous Message

Now you will see below output under the Queues in admin console of Active MQ.

synchronous jms mule

Thanks for reading.

Leave a Reply

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