Java JMS Oversimplified
What JMS is and how it works in two words.
Join the DZone community and get the full member experience.
Join For FreeJava Messaging Service
JMS or Java Messaging service is a solution for asynchronous communication using messages (objects). Initially, it was part of JSR (specification used in Java EE).
Simple Problem as Example to Explain: User Creation Service
Let's assume we have any service that can process only 100 requests per second. Any higher load will freeze service or put it down.
Solving it with Java Messaging Service
Such problems are very known and can be easily sorted by splitting API into two parts:
- The first part creates a task and returns its id immediately. Once the system has an available worker it will process that task.
- The second part of API is constantly pooled by the user until worked complete task and return.
So the key thing in our problem is how to share information between 2 API? We might use a database or some Java collection, but JMS provides 2 convenient asynchronous solutions.
Main JMS Patterns: Queue and Topic
In Jms there are two main patterns: Queue and Topic. The key difference is that in Topic we can have many subscribers. Both structures can receive data from many producers.
Configuring ActiveMQ Message Broker
There are many known Message Brokers like RabbitMq, Kafka, etc. In our example, we use a free open-source Active MQ broker. In order to install it do the next steps:
1. Download ActiveMQ Classic.
2. Install Java and add JAVA_HOME to PATH.
3. Execute wrapper.exe in apache-activemq-5.16.2\bin\win64.
4. Open http://localhost:8161/admin/index.jsp.
5. Create Queue in tab Queues by pressing the Create button.
6. Add dependency in your Java application:
<dependency>
<groupId>org.apache.activemq</groupId>
<artifactId>activemq-core</artifactId>
<version>5.7.0</version>
</dependency>
Designing Application Using Active MQ Broker
The key profit we gain from Queue - load balancing in our application. Application won't have more load than we can handle. We create threads - consumers that poll new users to be created and execute slow creation process (slow initialization isn't necessary for this example but it emphasizes that the consuming process can easily break the application if expose outside). Once a user is created we store them in User Map. The diagram below describes design only with two consumers but can be easily scaled and for any given requirements (i.e., 200 user requests) and 2 seconds delay, we would create 100 consumers (producers can be also numbered but it's not critical as far as pushing process cost nothing).
Implementing Designed Application Using ActiveMQ
Initializing connectivity with ActiveMQ:
Pushing new user to ActiveMQ Queue:
Polling new user from ActiveMQ Queue and processing it:
Exposing API to be polled from the client-side:
Checking Active MQ Activity
Delivery Acknowledgment
In this implementation, we poll data from the queue without considering its processing status on the application side. So if we fail to create a user then the queue won't be notified about it. To avoid it we add delivery acknowledgment at the end of the user creation process. Having it ActiveMQ will release the message only if the message was delivered + processed. Otherwise, if we fail to process it, the message will stay in the queue unless someone processes it successfully.
Change acknowledgment mode while creating session:
Adding our successful message processing:
Conclusion
Used source code is located here.
I hope this article was clear enough and explained the main idea of the JMS pattern. Right now Message Broker is an almost mandatory part of each enterprise application. If for you it's the first introduction about JMS I would recommend investigating the next subjects:
- Spring JMS Integration, e.g., JMSTemplate
- RabbitMQ Message Broker
- Kafka Message Broker
- XA Transaction Support in JMS
Opinions expressed by DZone contributors are their own.
Comments