Skip to main content

RabbitMQ tutorial - Routing

Routing​

(using the AMQP 1.0 .NET client)​

info

Prerequisites​

This tutorial assumes RabbitMQ is installed and running on localhost on the standard port (5672). In case you use a different host, port or credentials, connections settings would require adjusting.

Where to get help​

If you're having trouble going through this tutorial you can contact us through GitHub Discussions or RabbitMQ community Discord.

In the previous tutorial we used a fanout exchange. Here we use a direct exchange so consumers can subscribe to a subset of messages (for example by severity).

Bindings​

The sample uses exchange logs_direct. The publisher sets the routing key when building the publisher:

IPublisher publisher = await connection.PublisherBuilder().Exchange(exchangeName).Key(severity).BuildAsync();

The consumer declares an exclusive temporary queue and binds it once per severity from the command line:

foreach (string severity in args)
{
IBindingSpecification binding = management.Binding()
.SourceExchange(exchangeSpec)
.DestinationQueue(queueName)
.Key(severity);
await binding.BindAsync();
}

The handler can read the effective routing key from message annotations (see ReceiveLogsDirect/Program.cs).

Running​

dotnet run --project ReceiveLogsDirect/ReceiveLogsDirect.csproj warning error
dotnet run --project EmitLogDirect/EmitLogDirect.csproj warning "Run. Run. Or it will explode."

Source​

Now we can move on to tutorial 5 to learn about pattern-based routing with topic exchanges.