How to integrate Java Application with Amazon MQ?

June 22, 2018 | Comments(1) |

Messaging mediates communication among different parts of distributed system or applications, also eliminating the need for understanding application’s design in order to exchange messages. There are numerous open source message broker tools available in the market, which can be used for messaging applications.

In this article I will walk you through one of the AWS’s Managed Message Broker service i.e, Amazon MQ. We will learn about Amazon MQ, its benefits and advantages over Amazon SQS. Also, we will see how to use it in an application.

Introduction

Amazon MQ is a new service that was launched during AWS re:Invent 2017 which is a managed message brokering service for Apache ActiveMQ. ActiveMQ is open source service which provides reliable communication between two or more processes and can be used for communication within large critical applications. This supports various programming languages, operating systems and messaging protocols by eliminating the need for managing and maintaining our own messaging system. It also provides direct console access to ActiveMQ.

Amazon MQ Broker

We can create message broker according to our requirements. A broker can be a single-instance broker or active/standby broker.

  • Single Instance Broker contains one broker in one availability It communicates with the application and AWS storage location. It can be used for development and testing.
  • Active/Standby Broker contains two brokers in two different availability zones for high availability. It will communicate with the application and shared storage location. It can be used for automatic failover. For both modes, Amazon MQ provides data replication across AZs.

Why ActiveMQ, Why not database to store messages?

Yes, we can use the database to store messages and process it. But, whenever communication happens between two applications, once the message is received it should be processed and deleted i.e. for each message entry there should be insert and delete operation. This will work smoothly for few messages without affecting the performance of the database. If we want to handle thousands of messages between these applications, then it may affect database performance and it may tend to fail.

Amazon MQ acts as message broker service for Apache ActiveMQ which can be used to handle this use case. This will handle maintenance of ActiveMQ brokers and perform optimizations to avoid overhead. It provides a mechanism of pushing messages to consumers instead of consumer polling for a new message. Therefore, this reduces the latency involved in processing new messages.

Benefits of Amazon MQ:

  1. Application Integration:

    Provides a way to integrate applications with different languages and different operating systems to communicate with each other.

  2. Reliability:

    No need for availability of producers or consumers at the same time. Provides queue to manage any number of requests at any point in time.

  3. Asynchronous:

    Allows one application to fire a message and continue with other tasks instead of waiting for a response.

  4. Decoupled:

    Since queue lies between the application and communication is carried through the queue, failure of one system will not affect other application.

  5. Cost Reduction:

    Need to pay only for storage and broker instance as you use.

Amazon MQ vs Amazon SQS

Amazon MQ

Amazon SQS

Best suitable for the existing applications which are already using open source message brokers Best suitable to build new cloud-based messaging applications
In Amazon MQ, there is no need for availability of producers or consumers at the same time. It provides queue to manage any number of requests at any point in time Amazon SQS relaxes some of the queueing system properties in order to achieve high redundancy and to ensure no messages are lost. i.e. in some rare situation, messages arrived may be out of order and some messages can be delivered more than once
Need to pay only for storage and broker instance as you use i.e. pay per hour and per GB Need to pay per request

 

Is API compatible Provides simple APIs
Is feature rich Provides unlimited throughput

 

Creating Broker and connecting it to Java Application

In this application, we will create two components i.e. a Producer and a Consumer. Here, the Producer will connect to the broker, create a queue and send a message and the Consumer will connect to the broker, create a queue and receive a message sent by the Producer.

If we want to create multiple producers with a large number of connections always use JMS connection pooling factory i.e. use PooledConnectionFactory class. But message consumers should use JMS connection factory i.e., ActiveMQConnectionFactory class.

1. To start with, first open Amazon MQ console with the desired Enter Broker name and Click on Next step.
Introduction Amazon MQ

2. In this example we are creating single broker instance type, so select Single-broker deployment.
Single Broker Deployment

3. Enter Username and Password which you want to use in future to access ActiveMQ console of the broker created. Click on Create broker.ActiveMq Web Console AccessBroker creation will take up to 10 – 15 minutes.

4. Click on the broker after the status turns to running. You will get the view of urls and endpoints that can be used to access your broker.

5. Once you are done with broker creation. Copy the OpenWire endpoint URL from the console, which will be used while creating Java application.
ActivemQ Web Console

6. Create Java application named AmazonMQConnection. Create two classes which will act as two components in this application. One is the Producer class and another one is the Consumer.

Producer Class

Consumer Class

7. Convert Java application into Maven project. Update pom.xml file with the following code. This will allow the application to work with ActiveMQ.

8. Run Producer class to send a message and Run Consumer class to receive the message sent by the Producer.

Conclusion:

This blog helps you to get an overview of Amazon MQ with working example to connect two components in an application. Watch out for my next blog article on How to migrate existing applications with On-premises Broker to Amazon MQ.

Message queuing services like SQS and Amazon MQ are extensively used by developers to decouple the application components.

In case you want to learn about how to create applications for AWS, please check out our developer course, AWS Developer Certification BootCamp – Level 3 (Associate).

Please feel free to post your comments in the comment section below.


One Response to “How to integrate Java Application with Amazon MQ?”

Leave a Reply