Ask Sawal

Discussion Forum
Notification Icon1
Write Answer Icon
Add Question Icon

How to use jms in mule 4?

3 Answer(s) Available
Answer # 1 #

JMS (Java Messaging System) is mostly used to enable the communication between APIs using an exchange of messages. It provides loosely coupled, reliable and asynchronous communication.

ActiveMQ is an open-source protocol developed by Apache which functions as an implementation of message-oriented middleware (MOM). Its basic function is to send messages between different applications.

A messaging system is composed of a JMS Broker (middleware), Queues and Topics, Producers, and Consumers.

JMS Broker is an application server that sits in between the sender (producer) and receiver (consumer). JMS Broker receives messages from producers and sends messages to consumers. It means that a Message sent from the Producer is not directly received by the consumer, instead, it is first received by the Broker and after receiving a message, the broker forwards the message to consumers.

JMS producer (sender) is an entity that creates and sends messages to JMS Queue or topic.

JMS consumer (receiver) is an entity that receives messages from the JMS queue or topic.

Using JMS we can send messages to queue or topic. Now let's understand what they are and why they are used.

[5]
Edit
Query
Report
Illeana Kingsford
Secondman
Answer # 2 #
  • 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.
[4]
Edit
Query
Report
Misha Alcocer
Head Carpenter
Answer # 3 #

In this tutorial we will talk about how to use JMS connector with Apache ActiveMQ with various use cases and scenarios

JMS (java messaging API) is backbone of any Enterprise Application integration. The Java Message Service (JMS) API is a messaging standard that allows application components based on the Java Platform Enterprise Edition (Java EE) to create, send, receive, and read messages. It enables distributed communication that is loosely coupled, reliable, and asynchronous. Many providers have implemented JMS interface and created the JMS messaging products e.g. TIBCO EMS, Apache active MQ, IBM MQ etc

We would need to download apache ActiveMQ from http://activemq.apache.org/download.html

Please follow the steps below to configure JMS connector with Apache ActiveMQ

ActiveMQ Set Up

Unzip the archive downloaded. You should be able to see following.

Run the command D:\Application\apache-activemq-5.15.2\bin>activemq start to start the ActiveMQ.

Open http://localhost:8161/admin/ and you should be able to see page below. use “admin“ as username and password.

Click on queue or topic to create a new queue or topic accordingly

Integration with MuleSoft

Perform the steps below for ActiveMQ connection configuration

Dependency below would be added in POM.

You would also need to add the dependency for ActiveMQ broker (for non persistent in memory connection) or ActiveMQ Kaha DB (for persistent connection). Click on configure Broker dependency.

Dependency below would be added in POM.

Basic JMS operations

You can use JMS in your application to

Message is sent to the queue and the thread waits for response. If you use reply to queue, then the response will be sent to that queue otherwise it will be sent to a temporary queue, created when the message is being sent and attached to JMSReplyTo Header.

The request and response of that message is correlated using either correlation id or message id. the consume operation listens (using JMS selector) for the message with either the correlation id or message id sent. This way you can implement request-response pattern using messaging.

You can verify the message “correlation Id” and “reply to” queue in ActiveMQ portal also.

Use case of JMS/message-oriented middleware (MOM).

Introducing a MOM is best possible way to decouple an integration. You can use any messaging provider to decouple the application. You can decouple logging, exception handling or any non-business processing in your application thereby ensuring optimal performance to your application. You can just send message to a queue and the same can be processed in another thread in another application.

This is another part of Message provider. Message provider can ensure guaranteed delivery of messages to the desired system. There is another property in Message provider (message acknowledgement), which ensures the messages remain there in the server until the processing of the message has been completed by consumer of the message. You can use acknowledge (Ack) message processor in JMS connector to acknowledge a message. There are various types of acknowledgement you can use. E.g.

You can have pub sub style of integration in JMS. If you need to send the same message to multiple consumers. You can use Topics to achieve the same.

In Synchronous processing, instead of asking the consumer to wait, you can send the message to a broker and then process the message as an when you want to. you can Later you can send a reply to the “reply to” queue JMS header.

You can use JMSX header called JMSXgroupid if you want to process the message in groups. E.g. you have various orders of different customers and you have a task to process the customer’s order in parallel but for every customer the orders must be processed in sequence and one by one.

E.g you have messages – orderc1m1, orderc1m2, orderc1m3, orderc1m4 for customer 1, orderc2m1, orderc2m2, orderc2m3, orderc2m4 for customer 2 and orderc3m1, orderc3m2, orderc3m3, orderc3m4 for customer 3 and you want to process all the customer’s order in parallel but every order for a customer should be process one by one and in sequence.

if you have multiple JMS Sessions and consumer(JMS listener deployed in cluster mode in MuleSoft), the messages will not be processed in the order; since the messages will be processed concurrently in different threads.

You can use the exclusive consumer property, which will ensure only one consumer picks the message and the message can then be processed sequentially.

Here is the sample project for the above tutorials mulesy-jms

[1]
Edit
Query
Report
Chittaranjan Medhekar
Fraud Investigator