Contents
Let’s start by creating a docker image from a rabbitmq image:
docker run -it --rm --name rabbitmq -p 5672:5672 -p 15672:15672 rabbitmq:3.12-management
Add the -d flag if you want to run the container in detached mode. We are binding two ports: one for the rabbitmq service (5672), and one for a web-ui (15672). In this section, we will play a bit only with the web-ui, to better understand how the system works.
The RabbitMQ web-UI
Open your browser at localhost:15672. You should see something like this:
Insert guest as username and guest as password, and log in.
From the web interface, we can monitor the rabbitmq instance and see, for example, how many messages are present, the memory footprint of the instance, some statistics, and the messages in a queue. We can also create exchanges, queues, and bindings: let’s create a direct exchange to see how it works.
Create an exchange
Go to the exchange tab. From here, you can see a list of exchanges (rabbitmq adds some pre-defined exchanges). We can create a new exchange:
Set type = direct, name = test_exchange and click Add exchange.
Create a queue
We need also the create a queue: for our case, we create two durable queues: test_queue_1 and test_queue_2. To do that, go to the tab Queues and Streams and fill out the form:
Binding queues to exchanges
What remains to do is to bind the queues to the exchanges. Let’s go on Exchanges and click on our exchange from the list. Then, create the bindings:
These bindings say that all the messages published to test_exchange with routing key yellow go on test_queue_1, and all the messages published to test_exchange with routing key blue go on test_queue_2. Remember that if the routing key of a message does not match any of the bindings, that message will be discarded!
Publish some messages
Now we publish some messages to the exchange to see the behavior. Create and publish two messages: one with routing key yellow and one with routing key blue.
What happens if we publish a message with routing key red?
Lost forever!
Get messages
To see messages in a queue, go to the queue and click on Get Message(s): on queue test_queue_1, you should see only the yellow message. On queue test_queue_2, you should see only the blue message.
What happens if we publish a message with routing key red?
About ACK
Messages can be acknowledged.
ACK is a mechanism to ensure that a message is successfully sent. Acknowledgment can be positive (ACK) or negative (NACK). Positive acknowledgments tell the RabbitMQ instance to consider the message as delivered, while a negative acknowledgment says that the message had some problems during consuming and requires specific action (for example, it can be requested or sent to a Dead Letter Exchange).