How To Create Exchanges And Queues In RabbitMQ Server

dimitrilc 1 Tallied Votes 322 Views Share

Introduction

In the previous tutorial, we learned how to set up our own RabbitMQ server. In this tutorial, we will learn how to create our own exchange and queue.

Goals

At the end of the tutorial, you would have learned:

  1. How to create your own exchanges and queues.

Tools Required

  1. A RabbitMQ server.

Prerequisite Knowledge

  1. Basic Docker.

Project Setup

To follow along with the tutorial, perform the steps below:

  1. Log into RabbitMQ’s management interface.

Direct Exchange Routing

Although an AMQP server is capable of many different methods of routing messages, we will only learn about direct exchanges in this tutorial. The diagram below illustrates the direct exchange model.

amqp_direct_exchange.jpg

If you are new to AMQP, the diagram above might look confusing, so here is my best attempt to break it down to you.

  1. Outside the RabbitMQ Server, there is the Publisher. The publisher sends messages to exchanges. It is up to the exchanges to decide which queue to route the messages to.
  2. The Exchange routes the messages to queues that you have configured it to. We will discuss how this configuration works later in the tutorial.
  3. Queues are objects that store messages and send them out to consumers. RabbitMQ queues are FIFO (first in, first out). After the messages are sent to the queues, the messages stay there until they are consumed. By default, the messages are automatically removed after the client consumes them AND sends back an ack. This also means that messages will stay in the queue even after they were consumed, but no ack has been received from the clients.
  4. The consumers are other client programs that consume the messages from the queues. Consumers are also sometimes called clients. There are many client applications available, such as the RabbitMQ Java Client, RabbitMQ .NET client, Bunny (Ruby), Pika (Python), or amqp-client (NodeJS).

Setting Up An Exchange

RabbitMQ already came with some default exchanges readily for use. You can see them by clicking on the Exchanges tab in the management interface.

Screen_Shot_2022-09-08_at_12.37.12_PM.png

Highlighted in red, pink, green, and yellow are different types of exchange (we will only work with a direct exchange in this tutorial). We are going to set up our own exchange using the interface highlighted in orange.

Let us add an exchange called daniweb-exchange by providing the exchange Name, leave everything as default, and select Add exchange.

Screen_Shot_2022-09-08_at_3.15.36_PM.png

You should now see the daniweb-exchange under the exchange list. To view daniweb-exchange’s configuration, click on its name.

Screen_Shot_2022-09-08_at_3.29.28_PM.png

In the exchange configuration screen, you can see that the daniweb-exchange is currently not bound to any queue. In the next section, we will need to declare some queues, so we can bind daniweb-exchange to it.

Screen_Shot_2022-09-08_at_3.39.04_PM.png

Declaring Queues

To declare a queue, select the Queue tab, provide it a name, leave everything else as default, and then click on Add queue.

Screen_Shot_2022-09-08_at_4.01.38_PM.png

Now, click on the queue name to see its statistics and create a binding key.

Screen_Shot_2022-09-08_at_4.09.28_PM.png

The area in green shows message statistics for this queue. We have not sent any message, so there is no activity shown up here.
Screen_Shot_2022-09-08_at_4.13.54_PM.png

To create a binding key, you need to supply a valid exchange name (it must exists). You can name the routing key however you want. This routing key describes the relationship:

The queue my-queue is interested in messages from the daniweb-exchange.

After creating the binding key, you should see it show up under Bindings.

Screen_Shot_2022-09-08_at_4.21.07_PM.png

If we go back into Exchanges/daniweb-exchange, then we can see that it is already bound to my-queue, so we do not have to manually bind it ourselves in the exchange configuration anymore.

Screen_Shot_2022-09-08_at_4.32.03_PM.png

Summary

Congratulations, you have learned how to set up your own exchanges and queue in RabbitMQ in this tutorial. In the next tutorial, we will learn how to send messages via the queue created in this tutorial.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.