RabbitMQ

Shehara Grabau
4 min readJul 22, 2020

RabbitMQ is an open-source software and is very popular for message queuing. It is also called as a message-broker or queue manager because it is an intermediary for messaging.

Applications connect to RabbitMQ where queues are defined and those queues are used by the application to transfer messages.

A message could be any one of the following : a text message, information about a process or task that should start on another application etc. RabbitMQ stores these messages until an application that should receive the message connects and takes it from the queue.

Why use message queuing?

  1. Message queuing enhances the response time as web servers can respond to requests quickly. This is done by delegating tasks that would normally take up a lot of time or resources to a third party that has no other job.

2. Can be used to reduce loads

Let’s look at an example

Consider a scenario where a web application allows users to input user information to a website. The website collect this data, generate a PDF file and then email it back to the user.

The above tasks would generally take some time to be completed and this is where we see the use of message queuing.

When user enters information, the web interface creates a message for PDF Processing. This message is then places on a queue defined by RabbitMQ.

The architecture follows the Producer, Broker and Consumer structure.

Producer — Client applications that create messages and delivers them to the broker

Broker — The broker is the message queue. The messages placed here are stored until a consumer retrieves them.

Consumer — The applications that connect to the queue and subscribe to the messages to be processed.

RabbitMQ is simply like the generic flow of a letter sent by post

There are four types of exchanges in RabbitMQ

  1. Direct exchanges — routes messages with a routing key equal to the routing key declared by the binding queue
  2. Fanout exchanges — routes messages to all bound queues indiscriminately
  3. Topic exchanges — routes messages to queues whose routing key matches all, or a portion of a routing key
  4. Headers exchanges — routes messages based upon a matching of message headers to the expected headers specified by the binding queue
Direct exchange
Fanout exchange
Topic exchange
Headers exchange

Now let’s look at a simple hello world example using RabbitMQ

First of all Install RabbitMQ on your machine and open the command prompt as Administrator.

In order to use the web interface of RabbitMQ, enable the RabbitMQ plugins using the cmd : rabbitmq-plugins enable rabbitmq_management

Open up the browser window and type in localhost:15672. The default credentials are username : guest | password : guest. You will see the web interface up and running in your web browser.

Create a maven project in your project location and open it using your IDE.

Let’s look at the sender code :

When we run the code the output would look like this :

RabbitMQ message has been sent

Now let’s look at the RabbitMQ server to see visually if the message has actually been sent.

The consumer code :

The output of the code will look like this :

--

--

Shehara Grabau

Inspired by the little things in life, I aspire to be more.