JMS Connector with apache activemq
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
Prerequisite
We would need to download apache ActiveMQ from http://activemq.apache.org/download.html
Steps
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
- Add the JMS config
- Select “ActiveMQ Connection” in the connection parameter.
- Now, you will need to add client jars to connect to Active MQ. Click on Configure to add the Client jars.
Dependency below would be added in POM.
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-client</artifactId> <version>5.14.5</version> </dependency>
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.
<dependency> <groupId>org.apache.activemq</groupId> <artifactId>activemq-broker</artifactId> <version>5.15.4</version> </dependency>
- Enter the broker URL as tcp://localhost:61616 and test the connection. Test connection should be successful as below. if not then, check if your activeMQ is started properly or if the dependencies are loaded correctly.
Basic JMS operations
You can use JMS in your application to
- Send a message to queue or a topic using “Publish” JMS message processor
- You can verify the same in ActiveMQ admin portal also.
- Receive a message from queue or a topic using “Listener” JMS message processor
- Send message to queue or a topic and wait for a response queue using “Publish Consume” JMS message processor
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).
- Application decoupling
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.
- Reliable/Guaranteed delivery
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.
-
- Auto: The message is not acknowledged if and only if the flow execution completes successfully. If there is any exception in the flow, the message will be redelivered till the Max redelivery (configured as below and defined by the JMS header JMSXDeliveryCount) is exhausted.
-
- DUPS_OK: It is similar to Auto acknowledgement mode. However, the acknowledgement is done at later automatically by the session till it has received a fixed number of messages, or when a fixed time interval has elapsed since the last acknowledgment was sent. So, your message could be redelivered by the server before the acknowledgement.
- Immediate: The message is acknowledged as soon as it is received by the consumer. The message is immediately removed from the broker also. Hence there is no way the message will be redelivered if there is any exception in the flow
- Manual: This mode lets the acknowledgement to be done by consumer. You can use Ack JMS message processor (shown in above picture) to achieve this. The message would be deleted from the server only when it has been acknowledged by the Ack JMS message Processor in the flow
- Pub Sub Architecture
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.
- Asynchronous processing.
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.
- Processing messages in groups
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.
- Maintaining the sequence of the messages
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
Is their any size limit to store the data in queues?
i am trying to add jars but its not working