Author: Ambar Jain
Introduction
The Java Messaging Service (JMS) is used to make communication between the various components of a distributed application loosely coupled, reliable, and asynchronous as well as to enable message-based communication between the apps. Messages can be sent and received to and from queues and topics using the JMS connector.
JMS supports two models for messaging:
- Queue (Point to Point)
- Topic (Publish-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. It is not necessary for the sender to be running at the time the receiver receives the message and in the same way, the receiver does not need to be running while the sender sends the message. The receiver acknowledges receipt of every message that is successfully processed. In this model, the sender posts the message directly to the destination because the sender is aware of it.
Topic (Publish-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 message will be sent to a topic by the publisher, and all subscribers who are actively listening to the topic will receive it. If a subscriber is not actively listening to the topic, they will miss the published message unless messages are made durable.
Setup ActiveMQ JMS Server:
1. Download the activeMQ zip file from the following link:
https://activemq.apache.org/components/classic/download/

2. Extract the downloaded zip file and navigate to the below path:
C:\softwares\apache-activemq-5.16.5-bin\apache-activemq-5.16.5\bin\win64
3. Click on the activemq.bat file. It will start the MQ server.
4. ActiveMQ has a default web application that can be used to interact with the console application of ActiveMQ. Web applications can be accessed by the link appearing in the console. You can see the highlighted log in the below screenshot.

5. A pop-up window will appear asking username and password. By default username=”admin” and password=”admin”.
6. On the ActiveMQ home page click “Manage ActiveMQ broker”.You will see a dashboard with Queues, Topics tab as shown in the below screenshot.

7. To create a queue, click on the Queues tab.
8. Enter a name in the Queue name input field for example: Q.TEST then click Create. You will see a new queue in the Queues list.

9. To create a Topic, click on the Topics tab.
10. Enter a name in the Topic name input field for example: T.TEST then click Create. You will see a new topic in the Topics list.

11. To connect to the ActiveMQ, we will need a broker URL that needs to be added 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
12. Click on activemq.xml document and open it with a notepad. You will see the broker URL under the transportConnectors tag. Please see highlighted link the below screenshot.
Broker url: tcp://0.0.0.0:61616

Configure JMS Connector in Anypoint Studio
1. Add JMS Connector to your Mule Project:
- First, you have to go on your ‘Mule Palette’. Then you have to 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.


2. 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 either one of the connection types to provide to 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 the Configure button to install the dependency.
- Select any of the following options:
- Add recommended library Install the recommended library.
- Use a local file Browse to a local file for the required engine library and install it.
- Add Maven dependency 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 1000. 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 endless redelivery of a message.

Example to Publish and Consume Message Using Queue
1. Create a flow in the Mule project to publish messages to the Q.TEST queue of the ActiveMQ server.

2. In your studio flow, select Publish Operation.
3. Set the connector configuration to JMS_Config which we have created.
4. Set the Destination field to the queue name, for example, Q.TEST.

5. 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.

6. Create a flow in the Mule project to consume messages from the Q.TEST queue of the ActiveMQ server.

7. In your studio flow, select the Consume Operation.
8. Set the connector configuration to JMS_Config which we have created.
9. In the Consume configuration screen, in Destination, specify the name of the destination from which to consume the message, for example, Q.TEST.

10. Execute jms-consume-testFlow, a message will be consumed from queue Q.TEST and will be logged in the console. One message will be dequeued from the queue of the ActiveMQ server as shown below as shown in the below screenshot.

In the same way, by changing the destination type to Topic in Publish operation and consumer type to Topic in Consume Operation, we can use the topic for one-to-many communication.