Divya Bandara
4 min readJul 21, 2020

--

Hello World using RabbitMQ

What is RabbitMQ?

RabbitMQ may be a message-queueing software also referred to as a message broker or queue manager. Simply said; it’s software where queues are defined, to which applications connect so as to transfer a message or messages.

A message can include any quite information. It could, for instance , have information a few process or task that ought to start on another application (which could even get on another server), or it might be just an easy text message. The queue-manager software stores the messages until a receiving application connects and takes a message off the queue. The receiving application then processes the message.

Here the components of RabbitMQ structure:

  • Producer: Application that sends the messages.
  • Consumer: Application that receives the messages.
  • Queue: Buffer that stores messages.
  • Message: Information that is sent from the producer to a consumer through RabbitMQ.
  • Connection: A TCP connection between your application and the RabbitMQ broker.
  • Channel: A virtual connection inside a connection. When publishing or consuming messages from a queue — it’s all done over a channel.
  • Exchange: Receives messages from producers and pushes them to queues depending on rules defined by the exchange type. To receive messages, a queue needs to be bound to at least one exchange.
  • Binding: A binding is a link between a queue and an exchange.
  • Routing key: A key that the exchange looks at to decide how to route the message to queues. Think of the routing key like an address for the message.
  • AMQP: Advanced Message Queuing Protocol is the protocol used by RabbitMQ for messaging.
  • Users: It is possible to connect to RabbitMQ with a given username and password. Every user can be assigned permissions such as rights to read, write and configure privileges within the instance. Users can also be assigned permissions for specific virtual hosts.
  • Vhost, virtual host: Provides a way to segregate applications using the same RabbitMQ instance. Different users can have different permissions to different vhost and queues and exchanges can be created, so they only exist in one vhost.

Producers send/publish the messages to the broker. Consumers receive the messages from the broker. RabbitMQ acts a communication middleware between both producers and consumers although they run on different machines.
When send messages to multiple queues, we might have multiple queues by having a more complex application. therefore the messages will send it within the multiple queues.

Sending messages to multiple queues exchange is connected to the queues by the binding and therefore the routing key. A Binding may be a “link” that you simply found out to attach a queue to an exchange. The Routing key’s a message attribute. The exchange might check out this key when deciding the way to route the message to queues (depending on exchange type).

Hello World Example using RabbitMQ

First you have to create a maven project. Here the structure;

File Structure

You have add following dependency to the pom.xml.

Dependency

Here the sender.java

Sender.Java

Receiver.java

Receiver.Java

First run the receiver.java. then you will get following

Receiver.Java

Then run sender.java. here the result

sender.Java

Then go to the receiver console and you can see receiver got the message.

Receiver Console.Java

--

--