Hello world Using RabbitMQ
Distributed and Cloud Computing
First of all, Let’s see what is RabbitMQ….
RabbitMQ is known as a message broker or queue manager that means a message-queueing software. More over; it is software where queues are defined to which applications connect in order to transfer a messages.
A message can include any kind of information. For example, It have information about a process or task that should start on another application or it could be just a simple 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 even if they run on different machines.
When send messages to multiple queues, we would have multiple queues by having a more complex application. So the messages will send it in the multiple queues.
Sending messages to multiple queues exchange is connected to the queues by the binding and the routing key. A Binding is a “link” that you set up to connect a queue to an exchange. The Routing key is a message attribute. The exchange might look at this key when deciding how to route the message to queues (depending on exchange type).
Using above knowledge,
This is a simple hello world example using RabbitMQ.
First you have to setup rabbitmq server in local. For that please visit https://www.rabbitmq.com/
Then you have to create a maven project and add the dependency according figure 2: dependency
First you have to create a maven project. Here the file structure;
You have add following dependency to the pom.xml.
Here the sender.java
Receiver.java
First run the receiver.java. then you will get following
Then run sender.java. here the result
Then go to the receiver console and you can see receiver got the message.