JMS Connector in MuleSoft
- March 20, 2023
Java Messaging Service (JMS) makes communication between various components of a distributed application loosely coupled, reliable and asynchronous, in addition to enabling message-based communication between the apps. Messages can be sent to and received from queues and topics using the JMS Connector.
JMS supports two models for messaging:
- Queue (point to point)
- Topic (publish and subscribe)
Queue (point to point)
The point-to-point queuing model involves a sender posting messages to a specific queue and a receiver reading those messages from that same queue. The message is only consumed by one receiver. The sender doesn’t need to be running when the receiver receives the message. In the same way, the receiver doesn’t need to be running when the sender sends the message. The receiver acknowledges receipt of every message that successfully processes. In this model, the sender posts the message directly to the destination, because the sender is aware of it.
Topic (publish and subscribe)
When using the publish and subscribe model, a sender can publish messages to a specific message topic rather than directly to a specific queue. It enables one-to-many communication. The publisher sends a message to a topic and all subscribers actively listening to the topic will receive it. If a subscriber isn’t actively listening to the topic, they’ll miss the published message unless messages are made durable.
Set up ActiveMQ JMS Server
-
Download the activeMQ zip file from
https://activemq.apache.org/components/classic/download/
-
Extract the downloaded zip file and navigate to the following path:
C:\softwares\apache-activemq-5.16.5-bin\apache-activemq-5.16.5\bin\win64
- Click the activemq.bat file; the MQ server will start
ActiveMQ has a default web application that can be used to interact with the console application of ActiveMQ. Web applications can be accessed through the link in the console. You can see the highlighted log in the following screenshot:
- A pop-up window will appear asking for a username and password; by default, both the username and the password are set to “admin”
- On the ActiveMQ home page, click Manage ActiveMQ broker; you’ll see a dashboard with Queues and Topics tabs, as shown in the screenshot below:
- To create a queue, click the Queues tab
- Enter a name in the Queue name input field — for example, Q.TEST — then click Create; you’ll see a new queue in the Queues list
- To create a topic, click the Topics tab
- Enter a name in the Topic name input field — for example, T.TEST — then click Create; you’ll see a new topic in the Topics list
- To connect to the ActiveMQ, you’ll need to add a broker URL in JMS Config in Anypoint Studio; to find the broker URL, navigate to the path below:
C:\softwares\apache-activemq-5.16.5-bin\apache-activemq-5.16.5\conf - Click and open the activemq.xml document with a notepad; you’ll see the broker URL — tcp://0.0.0.0:61616 — under the transportConnectors tag
Configure JMS Connector in Anypoint Studio
- Add JMS Connector to your Mule Project:
- Go on your Mule Palette and select Search in Exchange
- In the Add Dependencies to Project window, type JMS in the search field
- Click JMS Connector in Available Modules
- Click Add
- Click Finish
- Configure a global element for the Connector
- In Studio, navigate to the Global Elements tab
- Click Create
- In the filter box, type JMS and select JMS Config
- Click OK
- In the JMS Config window, for Connection, select one of the following connection types for this configuration:
- Active MQ Connection
- Active MQ Connection — No Connectivity Test – (DEPRECATED)
- Generic Connection
- In the Required Libraries section that shows the ActiveMQ KahaDB, ActiveMQ Broker and ActiveMQ Client libraries, click Configure to install the dependency
- Select any of the following options:
- Add recommended library and install it
- Use a local file and then browse to a local file for the required engine library and install it
- Add Maven dependency and then browse to the dependency and install it
- In the Factory configuration field, select Edit Inline
- Set the Broker URL field value to the address of the broker to connect to, for example, tcp://localhost:61616
- Set the Initial redelivery delay field to 1,000 — the amount of time to wait before the first message redelivery can be configured in milliseconds using the Initial redelivery delay field
- Set the Redelivery delay field to 100 — you can specify how long to wait after the initial redelivery of the message using the Redelivery delay field, which allows you to specify the time in milliseconds
- Set the Max redelivery field to 10 — configure the max redelivery field to stop the endless redelivery of a message
Example to publish and consume a message using queue
- Create a flow in the Mule project to publish messages to the Q.TEST queue of the ActiveMQ server
- In your Studio flow, select Publish Operation
- Set the Connector configuration to JMS_Config, created earlier
- Set the Destination field to the queue name — for example, Q.TEST
- Execute jms-publish-testFlow; a message will be published to queue Q.TEST.
One message will be enqueued into the queue of the ActiveMQ server as shown in the below screenshot.
- Create a flow in the Mule project to consume messages from the Q.TEST queue of the ActiveMQ server
- In your Studio flow, select the Consume Operation
- Set the Connector configuration to JMS_Config, created earlier
- On the Consume configuration screen, in Destination, specify the name of the destination from which to consume the message — for example, Q.TEST
- Execute jms-consume-testFlow; a message will be consumed from queue Q.TEST and logged in the console
One message will be dequeued from the queue of the ActiveMQ server, as shown in the screenshot below:
In the same way, by changing the Destination type to Topic in the Publish operation and the consumer type to Topic in the Consume operation, you can use the topic for one-to-many communication.
— By Ambar Jain