How to integrate Java Application with Amazon MQ?

June 22, 2018 | Comments(7) |

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.


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.


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.

7 Responses to “How to integrate Java Application with Amazon MQ?”

  1. Soheb Shaikh

    I have added my public ip in security group inbound rule, but still when running code, connection timeout error is coming when creating connection. Any help please?

  2. Monika Mehta

    I am implementing the same i have pasted the web console link its working.But when i am trying to connect with java application it’s not working.I have added the ip of local machine with port 8162.but still not able to connect from java application.

  3. Jitendra

    Really Nice Explanation!!!

    I am Python developer and just start using Amazon MQ..I didn’t get official document with python for using AMazon MQ services..So my question is ? Is it possible with Python or only java?

    Please Reply..I am awaiting your response.


  4. Arvind Kumar Avinash

    It will be more helpful if you can provide a simple integration diagram.

  5. Prashant Gupta

    Hi, Is there any way we can use queue as without consumer, means queue will maintain the consumer URL only and that will be hit to that address?

  6. Mia

    Great article. PLease let me know if the article to connect from on premise broker to Amazon MQ is published ?


Leave a Reply to Mia

Click here to cancel reply.