This document discusses using Java Message Service (JMS) with MuleSoft's messaging transport to integrate applications. It provides an example of configuring JMS with ActiveMQ to send and receive messages from queues. The key steps are:
1. Import the ActiveMQ JAR library
2. Create an ActiveMQ connector in Mule
3. Use a JMS endpoint to enqueue messages to a queue via an HTTP request
4. Use another JMS endpoint and logger to receive and log messages from the queue
2. JMS
• 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.
3. Queues
• In the point-to-point or queuing model, a sender posts messages to a
particular queue and a receiver reads messages from the queue. Here,
the sender knows the destination of the message and posts the message
directly to the receiver's queue. It is characterized by the following:
• Only one consumer gets the message
• The producer does not have to be running at the time the
consumer consumes the message, nor does the consumer
need to be running at the time the message is sent
• Every message successfully processed is acknowledged by the
consumer
4. Configuration with Example:
• Anypoint Studio offers easy-to-use components to connect to JMS Queues
and Topics. In the below example, we will use ActiveMQ, a leading open
source JMS implementation from Apache that supports JMS.
• The outline of the simple steps required to implement this example is as
follows:
• Import ActiveMQ libraries
• Create an ActiveMQ connector
• Enqueue to JMS Queue
• Run the sample
5. 1. Import ActiveMQ Library:
• Download Apache ActiveMQ binaries (ActiveMQ 5.11) from
http://activemq.apache.org/download.html. Unzip the compressed
file to any folder on your file system. Let’s call it <ACTIVEMQ_HOME>.
• Create a new flow in Mule Studio and name it “jms”. To configure the
project to use ActiveMQ libraries, right-click on the project ->
Properties -> Java Build Path -> Add External JARs. Select “activemq-
all-5.11-SNAPSHOT.jar” from <ACTIVEMQ_HOME>.
6.
7. 2. Create an ActiveMQ Connector:
• Double-click on “jms” flow to bring up the message flow. Click on the
“Global Elements” tab. Click on “Create” and locate ActiveMQ under
“Connectors -> JMS” and click OK.
8. 3. Enqueue to JMS Queue:
• We will use the request payload received from an HTTP inbound
endpoint to seed the ActiveMQ Queue. Open the “jms” message flow
and drag and drop an HTTP endpoint on to the flow.
• Double-click on the HTTP endpoint to bring up the properties dialog.
Specify “jms_queue” for Path. This will make the HTTP endpoint
accessible using URL http://localhost:7777/jms_queue.
• Set a payload that you want to add to the queue.
• Drag and drop a JMS endpoint next to the HTTP inbound endpoint.
• Double-click the JMS endpoint to bring up the properties dialog. Specify
“queue” for Queue name.
9. Select “Active_MQ” for Connection Reference in the Connector Configuration
that we created in Step 2.
10. 4. Create a Jms_receiver :
• Use a Jms endpoint to receive the messages in the queue. Its configuration
is as follows:
11. • Use a Logger to log the Received Message.
• Use Set-payload to send a response back to the Jms Service.
• The final JMS Receiver Message Flow is as below:
5. Run the sample:
• First, run ActiveMQ using executable “activemq-admin” from
<ACTIVEMQ_HOME>/bin folder. Make sure you can access the ActiveMQ admin
page using http://localhost:8161/.
12. • Run the JMS flow.
• Open http://localhost:7777/jms_queue.This will enqueue the request to
the ActiveMQ JMS Queue “queue”.
• Verify this by examining the ActiveMQ administration page at
http://localhost:8161/admin/queues.jsp. We can see the messages under
the “Messages Enqueued” column.
13. • The Output you receive after the execution is the Payload Set by the JMS-
Client.
• Note: Only one client can read from a queue at one time and the messages
read from the queue are removed from the queue. If you want to perform any
transactions on top of JMS then the Transaction settings come in handy.