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
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.
First, open the Amazon MQ console by clicking on ‘Get Started.’
Select the type of engine you want. We are setting Apache ActiveMQ
Setup Deployment Mode and Storage Type. We are not selecting Blueprints for a network for this example.
Give Broker a Friendly Name, Select Instance type, and authentication methods. Here, we are using Simple Authentication. It usually takes around 20 mins to create a Broker.
Once the Broker is created, go to connections details and copy the Openwire endpoint.
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:
System.out.println("Received Message is : "+consumerTextMesg.getText());
Convert Java application into Maven project. Update pom.xml file with the following code. This will allow the application to work with ActiveMQ.
Run Producer class to send a message and Run Consumer class to receive the message sent by the Producer.
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.
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.
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.