RabbitMQ – Brain Mentors Skip to content

RabbitMQ

What is RabbitMQ?

RabbitMQ is an open-source message-broker software.

It has implemented the Advanced Message Queuing Protocol.

RabbitMQ works very similar to a post box.

The producer i.e., the originator of a message will send the message to post box and than the message will reside inside post box until the message is handed over to the consumer/receiver.

 

In rabbitMq instead of a postBox we will use a queue.

This queue will retain the messages until taken by the consumer.

 

The main benefit of using this message broker is that the producer and the consumer don’t need to understand each other language.

that is a consumer can be written in javascript and a consumer could be a java program.

They just should know how to connect with RabbitMQ queue.

 

For example : here is a simple program to show a producer, consumer and a queue.

 

This is a sender program.

Here amqp protocol is used and a connection to rabbitMq is established.

Connection= TCP connection to the broker.

Channels are the threads that will signify a virtual/logical connection that will actually put data or take the data out from the queue.

 

Then a queue is attached to this channel using assertqueue() method.

Now later on the receiver which is attached to the same queue using the other channel of course will receive the message.

The receiver program

The consume() method in receiver program and the sendToQueue() method in sender program are used to take data out and put data onto the queue respectively.

Exchanges in RabbitMq

Now usually the messages are not sent to a queue directly and instead a exchange has this role of routing the messages to a specified queue.

An attribute can be attached with the incoming message and based on this the message can be redirected to a specified queue.

Now suppose if a message is sent with green binding so this message will be routed to the second queue.

Types of exchanges:

Direct Exchange:

Here message is routed based on the routing key.

Producer adds this routing key with the message.

Any queue that will have exact matching key will receive the message.

This is how an exchange can be created.

The type of the exchange can be mentioned.

And a key is bind with the message during the delivery of the message.

At the consumer’s side we are using the same exchange as used by producer.

Here also “direct” being the type of exchange is specified.

And then an anonymous queue is attached with the bindings.

These are the bindings later on which will be used by exchange to route the messages.

Default Exchange

It is a no-name predefined exchange.

It is a type of direct exchange.

Now if a message is sent with a key and there is a queue with same name as binding key then the data will be delivered to this queue only.

Topic Exchange

Whenever we need more than one attribute to be used while routing, this exchange technique should be used.

*=>match of exactly one word.

#=> match of zero or more words.

 

Producer’s code

Here multiple attributes are attached to the queue.

Now rose.# means if a message is arriving with starting attribute as rose and later with any combination of words receive it inside this queue.

Fanout exchange

This type of queue will send the incoming message to all the queues attached to the exchange.

Now even if a key will be provided with the message it will simply be ignored.

Sign Up and Start Learning