Praveen Ramanayake
3 min readJul 25, 2020

--

RabitMQ 2.Work Queue

RabbitMQ is an open source message-queuing software also known as message broker or queue manager. Basically it is a software where queues are predefined in order to transfer messages of connected applications.

RabbitMQ

As an example if we take a web application, message broker acts as the middleman to that application. RabbitMQ architecture basically has 3 parts of client application which is the sender called as producer, broker(message queue) and the receiver called as consumer. Producer is creating the messages and publishing it to the message queue. Consumer connects to the message queue in order to subscribe to the messages that are in process.

RabbitMQ Workflow

Now let’s create a work queue that will be used to distribute time-consuming tasks among multiple workers using java client.

First we create two files called NewTask.java and Worker.java as in the example https://www.rabbitmq.com/getstarted.html.

NewTask.java

Worker.java

Then it needs to compile two files by following commands.

javac -cp $CP NewTask.javajavac -cp $CP Worker.java

Then it needs to open three consoles, two consoles for run worker program and other console to publish tasks. After started the consumers we can publish messages using the console.

Followings are delivered for two workers.

1st Worker console

2nd Worker Console

Basically messages are distributed in the round-robin method. RabbitMQ will send each message, in sequence, to the next consumer. Each user gets on average the same number of messages.

The key concept behind Work Queues is to stop quickly completing a resource-intensive task, and to wait for it to be done. Then we schedule the task to be carried out later. As a post, we encapsulate a task, and send it to a queue. A worker cycle that runs in the background will pop up the tasks and finally carry out the job. If you have a lot of workers, they’ll share the tasks.

--

--