Java JMS Oversimplified - International Burch University
AP x
Facebook rejects talks with Australia publisher, testing world’s toughest online law
June 25, 2021
IoT Security: What Your Business Needs to Know
June 25, 2021

Java JMS Oversimplified

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: XML1

<dependency>

2

  <groupId>org.apache.activemq</groupId>

3

  <artifactId>activemq-core</artifactId>

4

  <version>5.7.0</version>

5

</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

Source: https://dzone.com/articles/java-jms-oversimplified

Department of Information Technologies: https://www.ibu.edu.ba/department-of-information-technologies/