AWS, Cloud Computing

5 Mins Read

Setup Amazon MQ with Java Application

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.

  • Cloud Migration
  • Devops
  • AIML & IoT
Know More

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.

     

  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.

Get your new hires billable within 1-60 days. Experience our Capability Development Framework today.

  • Cloud Training
  • Customized Training
  • Experiential Learning
Read More

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

WRITTEN BY Nisarg Desai

Nisarg Desai is a certified Lead Full Stack Developer and is heading the Consulting- Development vertical at CloudThat. With over 5 years of industry experience, Nisarg has led many successful development projects for both internal and external clients. He has led the team for development of Intelligent Quarterly Remuneration System (iQRS), Intelligent Training Execution and Analytics System (iTEAs), and Cloud Cleaner projects among many others.

Share

Comments

    Click to Comment

Get The Most Out Of Us

Our support doesn't end here. We have monthly newsletters, study guides, practice questions, and more to assist you in upgrading your cloud career. Subscribe to get them all!