Amazon MQ with Java

Heshani Samarasekara
Geek Culture
Published in
3 min readMar 6, 2022

In this article I will be taking you through the steps I followed to create a Amazon MQ, and to connect this queue with my java client. What Amazon MQ does is, handing over messages from one service to another.

Handing over
Photo by József Koller on Unsplash

My requirement

My requirement was a simple one. We have several micro services based applications, which are deployed in Kubernetes cluster. These application needs to be communicating with each other. Let’s say I have two microservices, where one is for a notification sending service(Service A). To send notifications to the relevant partner, service A should contact the second service (service B), to fetch the relevant partner details from the database. So that, the service A is doing notification sending only, while the partner detail fetching is done by service B.

There is a concern, when service A is always calling to service B to fetch a partner from the DB, it creates a higher latency, and the load on service B will be very high. The solution was to add a caching layer, after the first fetch of the partner, service A keeps the partner details inside it. This created another issue, when an already fetched partner is updated from the service B side, service A does not know this change, and may create some wrong notification sending.

To resolve this issue, the next suggestion was to have a queue, where the service B can publish an event once a change occurred for a partner from service B. Then the service A can subscribe to the queue, and when there is a new event, it can invalidate cache related to that partner.

Hope you are clear about the problem I’m going to solve from this implementation.

PS: This article will not go through cache implementation.

Create an Amazon MQ

  1. Login to your AWS console and search for Amazon MQ.
  2. Click on “Create brokers” button. -> Select Apache ActiveMQ -> Click Next
  3. Select Active/ Standby broker for deployment mode, and select Durability optimized as storage type -> Click Next
  4. Provide a name for your broker, and then select Simple authentication and authorization as access method. (provide a suitable username and password)
  5. From Additional settings, select the relevant VPC and subnets for the queue or create new. For security groups also, you can create a new SG or, use an existing one.
  6. Set Public Accessibility to “Yes”

The broker creation will take some time and after that we can go and check the created broker.

Connecting the Amazon MQ to Java program

Publisher

Subscriber

Maven dependencies for the publisher and subscriber.

Make sure you copy the correct URL from the Amazon MQ console. You can find it under Connections.

To access the ActiveMQ web console, you can use one of the two URLs given in this page. When you go into the web console, you can find message counts per topic.

Hope this article will guide you to connect your Java application with Amazon MQ. See you in the next one. 😀

--

--