SETTING UP A HEADER QUEUE IN RABBITMQ

What is a header queue?

A header queue, just like the Topic queue, uses pattern matching to select what messages should be received. It does not use the Routing Key to filter. Instead, it uses the header in the message which contains keywords to filter on. This can provide a different pattern of filtering that can be hard to achieve with topics.

The header consists of a dictionary with key-values. The consumer can choose to filter on some or all of them. This gives a highly flexible way to address the consumers.

NuGet

To set up a RabbitMQ client you only need one NuGet package.

RabbitMQ NuGet component.

The code

This is basically what is needed to create a direct queue. You do this in both the sending and receiving clients.

private IAsyncConnectionFactory _factory;
private IConnection? _connection;
private IModel? _channel;
    
public void CreateQueue()
{
    _factory =
        new ConnectionFactory
        {
            HostName = "localhost",
            UserName = "guest",
            Password = "guest",
        };

    _connection = _factory.CreateConnection();
    _channel = _connection.CreateModel();
    _channel.ExchangeDeclare( "HeaderExchange", ExchangeType.Header);
}

The code explained

_factory =
    new ConnectionFactory
    {
        HostName = "localhost",
        UserName = "guest",
        Password = "guest",
    };

The factory is where all entities that RabbitMQ uses are created.
It is also here you tell the client where the RabbitMQ server is, and what account to use.

HostNameThe IP address to the RabbitMQ server.
UserNameThe name of the user that is used to gain access to the server.
PasswordThe password that verifies the user.

The default username and password is guest/ guest.

_connection = _factory.CreateConnection();

where all the queues will be sending its data. Since it is created from the factory, it knows what server to create a connection to.

_channel = _connection.CreateModel();

From the RabbitMQ documentation
https://www.rabbitmq.com/channels.html

Some applications need multiple logical connections to the broker. However, it is undesirable to keep many TCP connections open at the same time because doing so consumes system resources and makes it more difficult to configure firewalls. AMQP 0-9-1 connections are multiplexed with channels that can be thought of as “lightweight connections that share a single TCP connection”.

_channel.ExchangeDeclare("HeaderExchange", ExchangeType.Header);

This is where the exchange is created.

We declare the name of the Exchange. This is the entity that “connects” the sender to the receivers queues, so that the sender only has one place to send messages to, and the receivers has their own individual queues.

The type is “header”, which means that the publisher will send messages with a header that contains information that the receivers can filter on.

Additional code for the subscriber

This code is only intended for the subscriber. The publisher does not need this, so don’t use it there.

public void BindQueueToHeader()
{
    _channel.QueueDeclare(QueueName, true, false, false, null);

    var header =
        new Dictionary<string, object>
        {
            { "stock", "update" },
            { "department", "homeelectronics" },
        };

    _channel
       .QueueBind(QueueName,
                  ExchangeName, 
                  routingKey: "", 
                  arguments: header);
}

The subscriber code explained

The receiver subscribes to messages with the correct header through a queue.

_channel.QueueDeclare(QueueName, true, false, false, null);

We start with declaring a queue.

var header =
    new Dictionary<string, object>
    {
        { "stock", "update" },
        { "department", "homeelectronics" },
    };

Then we set up the header that the queue will listen to.

_channel
   .QueueBind(QueueName,
              ExchangeName, 
              routingKey: "", 
              arguments: header);

Finally, we bind the queue to the exchange. This will connect the queue, with the header filter to the exchange.

Just make sure that the name of the exchange is the same as the sender.

Like it? Share it!

Leave a Reply

Your email address will not be published. Required fields are marked *