SETTING UP A TOPIC QUEUE IN RABBITMQ

What is a topic queue?

A topic queue uses pattern matching to select what messages should be received. It works much like a Fanout queue with filtering. The publisher sends a message to the server which is coded with a specific topic.

The topic is actually the Routing Key but formatted using the pattern {word}.{word}…{word}.

The subscribers only receive the messages they are interested in, by subscribing to these words. It can also use wildcards to say that no matter what this specific word or words are, I want them all.

Confusing? Here are a few examples:

Publisher routing key
homeelectronics.music.salesHome electronics division, music department, sales.
homeelectronics.appliances.serviceHome electronics division, appliances department, service errands.
office.supplies.salesOffice division, supplies department, sales.
Subscriber filter
homeelectronics.*.salesReceives sales from the home electronics division, no matter what department.
#.salesReceives all sales, no matter the division or department.
homeelectronics.music.sales    Receives only sales for the music department in the home electronics division

For more info about the different queues, look here: All RabbitMQ Articles ##### Länka till ett ankare istället.

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;

private const string ExchangeName = "TopicExchange";

    
public void CreateQueue()
{
    _factory =
        new ConnectionFactory
        {
            HostName = "localhost",
            UserName = "guest",
            Password = "guest",
        };

    _connection = _factory.CreateConnection();
    _channel = _connection.CreateModel();
    _channel.ExchangeDeclare(ExchangeName, ExchangeType.Topic);
}

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();

This is basically the TCP connection to the server. This is 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(ExchangeName, ExchangeType.Topic);

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 “topic”, which means that the publisher will send messages with topic coded routing keys (see example above), and the consumer will use the routing key to filter what to receive.

— Cheers!

Like it? Share it!

Leave a Reply

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