RabbitMQ — Work Queue

Hirushka Nirmani
4 min readJul 22, 2020

Introduction

First of all, I would like to give a brief introduction about RabbitMQ before. RabbitMQ is an open-source message broker. It is originally implemented the Advanced Message Queuing Protocol (AMQP). It has been expanded with a plug-in architecture to support Streaming Text Oriented Messaging Protocol (STOMP), Message Queuing Telemetry Transport (MQTT), and other protocols.

Actually, when we talk about RabbitMQ, it is written in Erlang programming language. According to expertise this language Erlang programming language has the ability of supporting distributed, fault-tolerant systems. That concept made it the best match for implementing a platform for message queuing and handling enterprise level message handling as well.

RabbitMQ server is act as the message coordinator that can be integrated with many other applications as per the requirement. RabbitMQ is consist with many features that makes it so special and very easy for system integration.

· Reliability

· Routing

· Clustering

· High availability

· UI Management

· Command line interface

As I say RabbitMQ server acts like a message coordinator. As an example, we can get a post office that manage all the posts from people which guarantees to be send to its correct destination (required receiver).

This whole message coordinating process consists with three main components. The sender (Producer), the message queue that holds the messages before it sends to their destination and the receiver (consumer). The queues follow the FIFO concept (First come first out).

Consumers/ receivers get the stored message in the queues. Consumers have the ability to pull the message from the queue as well as queue has the ability to push the message from the queue to consumers. Queues defines rules named bindings during the period of establishing the connection with an exchange. Using binding the messages will be routed to the queues.

Now we can go to the example project for the RabbitMQ. This project consists with 2 classes Worker.java and NewTask.java. in this example that I am going to explain, Worker class is the consumer or the receiver and the NewTask class in the one who send the messages which is the producer according to the explanation. Think there are 2 workers/ consumers and one producer/sender. When producer sends messages, all the messages will be kept in a message queue and then the messages will be distributed through the workers accordingly.

During the distribution process, we can use the Round Robbin Dispatching. RabbitMQ the message broker will coordinate with both producer and the consumer and manage to send all the messages within the queue efficiently. Using the Task queue is very important here because it facilitate the task parallelization.

In this code the tasks have been managed to be executed according to the time that we specified within the input (number of dots). So, it pretends to be busy.

Message acknowledgement

Then what happen one of the worker / consumers goes down? Actually, we do not loose any of message/ tasks that proceed through that particular worker because of RabbitMQ. RabbitMQ facilitate Message acknowledgement which is it acts as coordinator as we discussed before and the worker / consumer send an acknowledgement response to the RabbitMQ that the task is completed executing and it is free to delete it. Even if RabbitMQ dosen’t receive the acknowledgement response, it will understand the situation which is the execution proceed has not been fulfilled with the consumer. It will requeue (put that task into the queue again) and push to another listening consumer. In order to facilitate that procedure we have to use “autoAck” as false.

Message Durability

RabbitMQ server goes down? Actually, RabbitMQ have the ability to remember tasks queue as per the client request (Otherwise it will forget it and we lost). In order to proceed that we make both queue as well as message/tasks as durable.

boolean durable = true;

channel.queueDeclare(“hello”, durable, false, false, null);

this concept need to be applied for both consumer and producer.

Fair Dispatch

As we discussed earlier, for dispatching Round Robbin will not be the best solution in terms of efficiency. Without having a proper knowledge about the message size RabbitMQ will distribute messages evenly. In order to prevent this happening, we can use “basicQos” method along with the prefechCount=1. This make sure that the queue gives messages to a consumer one at time and only after receiving the acknowledge response of the previous one.

Both Worker.java class and the NewTask.java has been given below. Go through the code to get a fine idea.

Worker.java
NewTask.java

To compile the files…

As you can see, I have sent 11 messages to the workers and we can how messages have been distributed among workers.

Producer

Msg 1,3,7,9,10 has been sent to worker 1

Worker 1/Consumer 1

Msg 2,4,5,6,8,11 has been sent to worker 2.

Worker 2/Consumer 2

For more details please keep in touch with my blog.

--

--