Before you can understand what RabbitMQ (message queue) is, it's helpful to see how data is transmitted without RabbitMQ. In this extremely simplified diagram, the sending system (producer) transmits data to the receiving system (consumer). In this scenario, the data would most probably be in XML format, and be something like an order placed at an online store.
While this is all fine and dandy, this type of design can produce problems. Most notably, if the receiving system is unable to receive the data (eg. the receving system is down or overwhelmed with requests), the sending system may not be able to move onto the next transmission or may continually try to send the data over and over again (which is a waste of the sending systems resources). RabbitMQ resolves these issues.
RabbitMQ sit's between the sending system (producer) and receiving system (consumer), as a sort of broker or proxy. In this way, if the receiving system is unavailable, the sending system does not get trapped in a situation where it cannot move onto it's next transmission or is bogged down trying to send the transmission over and over again. The sending system simply sends the data to RabbitMQ and then moves on (fire and forget). In this way, RabbitMQ makes it so that data tranmission is asynchronous.
Before we get into how a message gets from the producer to the consumer, the first step is for the sending system to make a connection to RabbitMQ. RabbitMQ will need to be configured with a user, a virtual host, and an AMQP port.
- Create a virtual host using the rabbitmqctl command
- Create user using the rabbitmqctl command
- List AMQP port using the rabbitmqctl command
Here is an example of how to make a connection to RabbitMQ in a Java application.
Next you'll probably want to put a messages on a queue, thus you'll need to create a queue.
You can the publish a message onto the queue.
Once the message is on the queue, the Consumer should be able to get the message from the queue.
The issue with the above scenario is there is no way for a producer to put a message onto the queue, because a producer will put a message onto what is known as a Exchange. For this reason, for a message to transmit from a producer to a consumer, you will need to:
- Create an exchange using the rabbitmqadmin command
- Bind a queue to an exchange using the rabbitmqadmin command
Let's say you have one exchange, but you want certain messages to go to queue "A" and certain messages to go to queue "B". The exchange can be setup with routing keys to route certain messages to certain queues.