Setup Amazon MQ with Java Application

June 7, 2022 | Comments(0) |

TABLE OF CONTENT

1. Overview
2. Introduction to Amazon MQ
3. What is Amazon MQ Broker
4. Why ActiveMQ?
5. Benefits of Amazon MQ
6. Amazon MQ vs Amazon SQS
7. Creating Broker and connecting it to Java Application
8. Conclusion
9. About CloudThat
10. FAQs

 

Overview

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

Today, we will walk through one of AWS’s Managed Message Broker services, i.e., Amazon MQ. Explore Amazon MQ, its benefits, and its advantages over Amazon SQS. Also, we will see how to use it in an application.

Introduction to Amazon MQ

Amazon MQ is one of the managed services offered by AWS. It is a message broker for RabbitMQ and Apache ActiveMQ. As Amazon MQ is a managed service, it reduces the burden of operational responsibilities, which involve setting up, maintaining, and provisioning message brokers. With Amazon MQ, one can quickly move to AWS without worrying about rewriting the code as it can connect to existing applications with industry-standard APIs and protocols. Amazon MQ can be set up with a few clicks and supports multiple languages and platforms.

What is Amazon MQ Broker

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

  • Single Instance Broker contains one Broker in one availability. It communicates with the application and AWS storage location. It is 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 a database to store messages?

Yes, we can use the database to store messages and process them. 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 an insert and delete operation. It will work smoothly for a few messages without affecting the performance of the database. If we want to handle thousands of messages between these applications, it may affect database performance and tend to fail.

Amazon MQ acts as a message broker service for Apache ActiveMQ, which can be used to handle this use case. It will run the maintenance of ActiveMQ brokers and perform optimizations to avoid overhead. It offers a mechanism of pushing messages to consumers rather than that consumer polling for a new message. Messaging mediates intercommunication amongst distinct parts of distributed systems or applications, eliminating the want the notion of the application’s construct to swap messages. Therefore, this reduces the latency involved in processing new messages.

Benefits of Amazon MQ:

  • Application Integration: It provides a way to integrate applications with different languages and different operating systems to communicate with each other.
  • 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.
  • Asynchronous allows one application to send a message and continue with other tasks instead of waiting for a response.
  • Decoupled: Since the queue lies between the application and communication is carried through the queue, the failure of one system will not affect other applications.
  • Cost Reduction: You need to pay only for the storage and broker instance you use.

Amazon MQ vs Amazon SQS

Amazon MQ vs Amazon SQS Table

Creating Broker and connecting it to Java Application

We will create two components i.e., a Producer and a Consumer. Here, the Producer will click 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.

Our goal is to create multiple producers with many connections that always use the JMS connection pooling factory, i.e., the PooledConnectionFactory class. However, message consumers should use the JMS connection factory, i.e., ActiveMQConnectionFactory class.

  1. First, open the Amazon MQ console by clicking on ‘Get Started.’
    Amazon MQ
  2. Select the type of engine you want. We are setting Apache ActiveMQ
    Amazon MQ
  3. Setup Deployment Mode and Storage Type. We are not selecting Blueprints for a network for this example.
    Amazon MQ
  4. Give Broker a Friendly Name, Select Instance type, and authentication methods. Here, we are using Simple Authentication.
    Amazon MQ
    It usually takes around 20 mins to create a Broker.
  5. Once the Broker is created, go to connections details and copy the Openwire endpoint.
    Amazon MQ
  6. Create a Java application named AmazonMQConnection. Create two classes that 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:

In this blog, we saw how easy it is to set up Amazon MQ. We have connected two components in an application using the Amazon MQ service. Message Queuing Services are used extensively to decouple applications or communication between two applications.

About CloudThat:

CloudThat is the official AWS Advanced Consulting Partner, Microsoft Gold Partner, and Training partner helping people develop knowledge on the cloud and help their businesses aim for higher goals using best in industry cloud computing practices and expertise. We are on a mission to build a robust cloud computing ecosystem by disseminating knowledge on technological intricacies within the cloud space. Our blogs, webinars, case studies, and white papers enable all the stakeholders in the cloud computing sphere.

If you have any queries about Amazon MQ setup with Java Application, Amazon Messaging Broker, or any other AWS services, feel free to drop in a comment and we will get back to you quickly.

FAQS:

1. Who can use Amazon MQ?

Ans: If your application uses RabbitMQ or ActiveMQ, you want to migrate to the cloud without revamping messaging code.

2. What if the existing application is not using ActiveMQ or RabbitMQ?

Ans: Amazon MQ is compatible with many standard message broker APIs like .NET Message Service/Java Message Service and protocols like MQTT and WebSocket. Mostly, updating the endpoints of Amazon MQ is takes to start sending messages.

3. Pricing of Amazon MQ?

Ans: Amazon MQ is eligible for AWS free one-year tier. Amazon MQ charged for storage usage, data transfer, and type of broker instance.


Leave a Reply