DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Improving Java Application Reliability with Dynatrace AI Engine
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Trending

  • Working With Cowork: Don’t Be Confused
  • Has AI-Generated SQL Impacted Data Quality? We Reviewed 1,000 Incidents
  • Can Claude Skills Replace Playwright Agents? A Practical View for QA Engineers
  • Why Pass/Fail CI Pipelines Are Insufficient for Enterprise Release Decisions
  1. DZone
  2. Software Design and Architecture
  3. Integration
  4. Java JMS Oversimplified

Java JMS Oversimplified

What JMS is and how it works in two words.

By 
Dmitry Egorov user avatar
Dmitry Egorov
DZone Core CORE ·
Jun. 18, 21 · Tutorial
Likes (27)
Comment
Save
Tweet
Share
43.4K Views

Join the DZone community and get the full member experience.

Join For Free

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

Problem Explanation

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.

API 1 and 2 Example

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. 

Main JMS Patterns: Queue and Topic

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.

ActiveMQ UI

6. Add dependency in your Java application: 

XML
 
<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).

Designing Application Using Active MQ Broker

Implementing Designed Application Using ActiveMQ

Initializing connectivity with ActiveMQ:

Initializing Connectivity With ActiveMQ

Pushing new user to ActiveMQ Queue:

Pushing New User To ActiveMQ Queue

Polling new user from ActiveMQ Queue and processing it:

Polling New User From ActiveMQ Queue and Processing It

Exposing API to be polled from the client-side:

Exposing API to be Polled from The Client-Side

Checking Active MQ Activity

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:

Change Acknowledgment Mode While Creating Session

Adding our successful message processing: 

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
Java (programming language) application Message broker

Opinions expressed by DZone contributors are their own.

Related

  • Improving Java Application Reliability with Dynatrace AI Engine
  • A Systematic Approach for Java Software Upgrades
  • Building a Simple RAG Application With Java and Quarkus
  • Dust Actors and Large Language Models: An Application

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook