Programming

Getting started with Amazon MQ

How to use Amazon MQ in .Net Core?

Emir Kılınç
May 16 · 8 min read
ActiveMQ logo from Wikiwand

This article will give brief explanation about Amazon MQ.

Amazon offers Amazon MQ, which is an implementation of ActiveMQ, integrated into its AWS cloud services. AmazonMQ is also called Amazon ActiveMQ.

Let’s get started.


What is a message queue?

A queue is a collection of entities that are maintained in a sequence and can be modified by the addition of entities at one end of the sequence and the removal of entities from the other end of the sequence. A message queue is a queue of messages sent between applications.

A message is the data transported between the sender and the receiver application.

In a message queue there are client applications called producers that create messages and deliver them to the message queue. And there are applications called consumers, which gets messages from the queue to be processed. Messages placed into the queue are stored until the consumer retrieves them.

Message queue example
Message queue example
Message Queue Schema From CloudAMQP

What are the benefits of message queuing systems?

  • They provide communication between clients.
  • Message queues remove dependencies between components and significantly simplify the coding of decoupled applications.
  • Can be used to order transactions.
  • Helps to ensure that each transaction occurs only once.
  • They provide high scalability and elasticity.
  • Can be very handy for application monitoring.

What is ActiveMQ?

ActiveMQ (Active Message Queuing) is a message queue service. It is a open source, multi-protocol Java-based messaging server (JMS). It supports many languages like C, C++, Python, .Net. ActiveMQ offers the power and flexibility to support any messaging use-case.

Some common use cases are:

  • Multi-platform applications can be integrated into ActiveMQ with AMQP (Advanced Message Queuing Protocol).
  • Establish communication between your web applications using STOMP (Streaming Text Oriented Messaging Protocol) over web sockets.
  • Manage your IoT devices using MQTT (Message Queuing Telemetry Transport).

ActiveMQ transmits messages from sender to receiver. It can connect multiple clients and servers and allows messages to be held in queue, instead of requiring both the client and server to be available simultaneously in order to communicate. Messaging can still happen even if one application is temporarily unavailable.

ActiveMQ sends messages between producers, which create messages and submit them for delivery, and consumers, which receive and process messages. The ActiveMQ Broker routes each message through one of two types of destinations:

  • A queue, where it awaits delivery to a single consumer (point-to-point).
  • A topic, to be delivered to multiple consumers that are subscribed to that topic (publish/subscribe, or “pub/sub”).

ActiveMQ gives you the flexibility to send messages through both queues and topics using a single Broker. In point-to-point messaging, the Broker acts as a load balancer by routing each message from the queue to one of the available consumers in a round-robin pattern. When you use pub/sub messaging, the Broker delivers each message to every consumer that is subscribed to the topic.

AciveMQ Diagram
AciveMQ Diagram
AciveMQ Diagram From datadoghq

Before we create our first Broker let’s go through some basic terms of queuing.

a. Failover String
If a JMS Broker goes down, ActiveMQ can automatically reconnect to an available JMS Broker using the failover: protocol. Not only does this automatically recovers connection, it will also keep all temporary destinations, sessions, producers and most importantly consumers information.

e.g. connecting to the URL
failover:tcp://host1:port1,tcp://host2:port2

b. Enqueue and Dequeue

Enqueue operation is to add/send an item to the queue. Dequeue operation is to delete/pop an item from queue.

c. Acknowledgement

Acknowledge method determines how messages received by the application will be acknowledged.

There are three acknowledgement modes in JMS. These are AUTO_ACKNOWLEDGE, CLIENT_ACKNOWLEDGE and DUPS_OK_ACKNOWLEDGE.

  • AUTO_ACKNOWLEDGE: The session automatically acknowledges each message received by the application.
  • CLIENT_ACKNOWLEDGE: The application acknowledges the messages it receives by calling the Acknowledge method of the Message class.
  • DUPS_OK_ACKNOWLEDGE: The session acknowledges the messages at times determined by the application.

ActiveMQ also has Transactional and IndividualAcknowledge modes.


Create a Broker on Amazon

Broker is a message broker environment running on Amazon MQ. AMQP, MQTT, MQTT over WebSocket, OpenWire, STOMP, STOMP over WebSocket protokols are officially supported on Broker.

Follow these steps to create a Broker:

  • Sign-in to Aws.
  • Select ‘Amazon MQ’ under the ‘Services’ section.
  • Click ‘Create Broker’ button at the top right of the page.
Create a Broker Part 1
Create a Broker Part 1
Create a Broker Part 1
Create a Broker Part 2
Create a Broker Part 2
Create a Broker Part 2
  • After you create a Broker, you will see Amazon MQ page.
  • Go to details.
  • Note down your failover string, username and password. We will use them for our .Net Core application.

More information about the Broker

Before we create an application that uses ActiveMQ, let’s learn about our Broker.

  • Go to your Broker details.
  • Look at the ‘Connections’ section.
  • Find the link below ‘ActiveMQ Web Console’ that redirects you to a web page. (Other links will redirect you to a Not Found page. These are our failover link in case our Broker goes down)
  • You will see a webpage like below.
ActiveMQ Broker Site Part 1
ActiveMQ Broker Site Part 1
ActiveMQ Broker Site Part 1
  • Click ‘Manage ActiveMQ Broker’.
  • Enter your credentials in the pop-up.
  • You will see a page like below.
ActiveMQ Broker Site Part 2
ActiveMQ Broker Site Part 2
ActiveMQ Broker Site Part 2
  • You can check you Queues/Topic here.
  • Click ‘Queues’.
  • This page shows all active queues and details.
  • Create a queue.
  • Give your queue a name by clicking ‘Queue Name’.
  • You have a queue now!
ActiveMQ Broker Site Part 3
ActiveMQ Broker Site Part 3
ActiveMQ Broker Site Part 3
  • ‘Number Of Pending Messages’ column shows messages waiting in the queue.
  • ‘Number Of Consumers’ column shows how many consumers do use that queue.
  • ‘Messages Enqueued’ column shows total enqueue count.
  • ‘Messages Dequeued’ column shows total dequeue count.
  • ‘Send To’ operation is used to send an item to the queue.
  • ‘Purge’ operation sets all column values to zero but does not deletes your queue.
  • ‘Delete’ deletes your queue.
  • Click ‘Send To’.
  • You will see a page like below.
ActiveMQ Broker Site Part 4
ActiveMQ Broker Site Part 4
ActiveMQ Broker Site Part 4
  • Enter a message to ‘Message body’ and click send.
  • ‘Number Of Pending Messages’ count and ‘Messages Enqueued’ are set to one now.
  • You can also try out Purge and Delete operations.
  • Topic operations are similar to Queue operations.

How to use ActiveMQ with .Net Core?

  • Create a .Net Core Console Application.
  • Firstly, Install Apache.NMS.ActiveMQ.NetCore package from Nuget.
  • Let’s do an enqueue operation.
How to enqueue from queue
  • BrokerUri is your failover string. Username and Passwords are your Broker credentials.
  • To make an enqueue operation, create a connection, set your credentials, start your connection, create a session , create a destination with a queueDestination (Queue Name), create a producer which you can enqueue an items with and sent an item to your queue.
  • After the enqueue operation you can check your Broker.
Image for post
Image for post
  • Let’s dequeue from our queue.
How to dequeue from queue
  • Dequeue operation is similar to the enqueue operation. To make a dequeue operation, create a connection, set your credentials, start your connection, create a session , create a destination with a queueDestination (Queue Name), create a consumer which can dequeue items from the queue and receive the message.
  • After the dequeue operation you can check your Broker.
Image for post
Image for post
  • Now let’s get pending message count. Firstly enqueue three items to the queue. Then run the GetPendingMessageCount method.
Hot to get pending message count
  • With this method you can connect to your queue and get the count of queued items without doing any enqueue operation.
Image for post
Image for post
  • So, how to get your pending messages without doing enqueue from queue?
How to get pending messages
  • This method is similar to the GetPendingMessageCount method.
Image for post
Image for post
  • How to delete your destination (queue)?
How to delete your destination
  • To perform the delete destination operation you have to declare IQueue object.
  • When you can check your Broker, you will see that your queue is gone.
  • If you need your queue back just do any queue operation (given the same queueDestination as before) and your Broker will recreate a queue automatically in the background.
  • Now check out topic operations. How to enqueue a topic?
How to enqueue to a topic
  • This method is similar to the EnqueueToQueue method. But this time your destination is specified as a topic.
  • How to dequeue from a topic?
  • This method is similar to the DequeueToQueue method. But this time your destination is specified as a topic.
Image for post
Image for post
  • If you check out my example project and run it, you will see the following output.
Image for post
Image for post

Example project:


About most popular top three Message Queuing Systems

Top 10 reasons to select a message queuing system
Top 10 reasons to select a message queuing system
About most popular top three Message Queuing Systems

ActiveMQ Alternatives

  • Kafka
  • RabbitMQ
  • Amazon SQS
  • Amazon SNS
  • Redis
  • gRPC
  • MSMQ
  • IBM MQ
  • Gearman
  • Beanstalkd
  • Azure Service Bus
  • Celery
  • NSQ
  • ZeroMQ
  • Apache NiFi
  • Confluent

Conculusion

We learned about Message Queuing Systems and gain basic knowledge of how a queue is processing. We learned about the benefits of using a Message Queue, most popular ones and alternatives. We checked out Amazon MQ (Apache ActiveMQ). Also we did a quick tutorial on how to use ActiveMQ in .Net Core and how to create/use a Broker.



For More Information


Special thanks to Ferhat Özkan

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data…

Emir Kılınç

Written by

Backend Developer @Solvoyo https://www.linkedin.com/in/emirkilinc

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Emir Kılınç

Written by

Backend Developer @Solvoyo https://www.linkedin.com/in/emirkilinc

Analytics Vidhya

Analytics Vidhya is a community of Analytics and Data Science professionals. We are building the next-gen data science ecosystem https://www.analyticsvidhya.com

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store