This tutorial will show you how to use Mule JMS Transport in Mule based application.
You can watch video version of the tutorial https://www.youtube.com/watch?v=bU0Rnds3L0A
You can be interested in Sending JMS messages with the JMS outbound endpoint in Mule
Connectors provide an abstraction layer over data transport mechanisms. Connectors exist for things such as files, email messages, databases, JMS, and even Jabber messages. A connector saves you the tedium of having to implement the details of a particular communication mechanism yourself. This allows you to focus on solving your integration problem and not on the plumbing of a particular communications protocol.
JMS (Java Message Service) is a widely-used API for Message Oriented Middleware. It allows communication between different components of a distributed application to be loosely coupled, reliable, and asynchronous.
JMS supports two models for messaging:
Queues – Point-to-point
Topics – Publish and subscribe
Mule’s JMS transport lets you easily send and receive messages to queues and topics for any message service which implements the JMS specification.
In our example, we’d need an HTTP connector and a JMS connector. The HTTP connector would provide the message receiving infrastructure in the form of a dedicated listener on a particular port. The JMS connector would provide the capacity to connect to the target JMS provider (through its specific connection factory) and to handle the dispatch of messages to the desired queue.
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)
Active MQ 5.11.1 (Download from http://activemq.apache.org/activemq-5111-release.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-3, 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.
So when the project mule-3 is created in the Anypoint Studio, the project structure looks like below
JMS Transport Example
The overall flow of the application is given below
Step 1. You can rename the src/main/app/mule-3.xml file as mule-jms.xml file.
Step 2. Open the mule-jms.xml file and click on Configuration XML view in the Editor
Step 3. Create a Java class as shown here
package com.roytuts.jms; public class MessageListener { public String getMessage(String msg) { System.out.println("msg received : " + msg); return msg; } }
Step 4. Modify the mule-jms.xml file as shown below
<?xml version="1.0" encoding="UTF-8"?> <mule xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:jms="http://www.mulesoft.org/schema/mule/jms" 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/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd http://www.mulesoft.org/schema/mule/jms http://www.mulesoft.org/schema/mule/jms/current/mule-jms.xsd"> <jms:activemq-connector name="Active_MQ" brokerURL="tcp://localhost:61616" validateConnections="true" doc:name="Active MQ" /> <flow name="mule-jmsFlow1" doc:name="mule-jmsFlow1"> <http:inbound-endpoint exchange-pattern="request-response" host="localhost" port="8081" doc:name="HTTP" /> <jms:outbound-endpoint queue="IN_QUEUE" connector-ref="Active_MQ" doc:name="JMS" /> <component class="com.roytuts.jms.MessageListener" doc:name="Java" /> <logger message="#[message.payload]" level="INFO" doc:name="Logger" /> </flow> </mule>
In the above XML Configuration, we first define activemq Connector with broker URl that is required to pass message to the JMS Queue.
Here we have inbound and outbound endpoints. The message is passed from inbound endpoint to outbound endpoint. Then the message is received by Java class method getMessage() and finally logged to the logger using Logger component.
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.1\bin 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. You will see that there is no message in the queue.
Running the application
Now do a right-click on the mule-jms.xml file or on the mule-3 project and click on Run As -> Mule Application. Then you will see something like below in Console when the application runs
********************************************************************** * Application: mule-3 * * OS encoding: Cp1252, Mule encoding: UTF-8 * * * * Agents Running: * * DevKit Extension Information * * Batch module default engine * * Clustering Agent * * JMX Agent * **********************************************************************
Console output
INFO 2016-06-26 09:20:17,312 [[mule-3].connector.http.mule.default.receiver.02] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default outbound transformer: org.mule.transport.jms.transformers.ObjectToJMSMessage INFO 2016-06-26 09:20:17,313 [[mule-3].connector.http.mule.default.receiver.02] org.mule.transport.service.DefaultTransportServiceDescriptor: Loading default response transformer: org.mule.transport.jms.transformers.ObjectToJMSMessage WARN 2016-06-26 09:20:17,313 [[mule-3].connector.http.mule.default.receiver.02] com.mulesoft.mule.transport.jms.EeJmsMessageDispatcher: Starting patched JmsMessageReceiver INFO 2016-06-26 09:20:17,313 [[mule-3].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Initialising: 'Active_MQ.dispatcher.6609156'. Object is: EeJmsMessageDispatcher INFO 2016-06-26 09:20:17,315 [[mule-3].connector.http.mule.default.receiver.02] org.mule.lifecycle.AbstractLifecycleManager: Starting: 'Active_MQ.dispatcher.6609156'. Object is: EeJmsMessageDispatcher msg received : /Jms INFO 2016-06-26 09:20:17,446 [[mule-3].connector.http.mule.default.receiver.02] org.mule.api.processor.LoggerMessageProcessor: /Jms
Now hit the URL http://localhost:8081/Jms in the browser
Now again click on the Queues in Active MQ web console, now you should see one message enqueued/dequeued from the Queue.