In rabbitmq, Topic Exchange will perform a wildcard match between the routing key and the routing pattern specified in the binding to publish messages to the queue.
Generally, in rabbitmq when the producer creates a message that will not directly send to the queue, instead first the message will be sent to exchanges, then after that, a routing agent reads and sends it to the appropriate queue with help of header attributes, bindings, and routing keys.
In rabbitmq, we have different types of exchanges available are
To know more about exchanges in rabbitmq, check this RabbitMQ Exchanges.
In Topic Exchange, the routing key must be defined with a dot(.) delimiter like “*.bombay.*” or “#.bombay” or “Bombay.#” then only the message will be added to the queue otherwise the messages will not be stored in the queue.
For example, I have 2 queues one is Bombay and another one is Delhi then the user must send a routing key that will match the pattern of the routing key which we defined while binding the queue to exchange then only the message will be added to the queue, else the message will be lost.
Here, while defining a routing key pattern if we use a * (star) then that can be a substitute for exactly one word and # (hash) can be a substitute for zero or more words.
Following is the pictorial representation of message flow in rabbit topic exchange.
Now, we will learn how to use Topic Exchange to push and read messages from rabbitmq in c# or .net application with examples.
Let’s create a simple console application with the Name “RequestRabbitMQ” as shown below to publish messages to the rabbitmq queue using topic exchange in c#.
After creating an application, now we will add the “RabbitMQ.Client” NuGet package in our c# application to communicate with rabbitmq server to publish or consume messages from queues in rabbitmq.
In c#, we can publish or consume messages from rabbitmq by using RabbitMQ.Client NuGet package for that, right-click on your application and select Manage NuGet Packages as shown below.
Now search for RabbitMQ.Client package and install it in your application as shown below.
Following is another way to install RabbitMQ.Client package by using Package Manager Console as shown below by entering the following command.
Command to install - Install-Package RabbitMQ.Client -Version 5.1.0
Before we start writing a code in c# to publish messages to rabbitmq, we will create a topic exchange, queue and will bind a queue to exchange using the web admin console.
To create an exchange in rabbitmq web management portal, open http://localhost:15672/#/exchanges url and then go to Add a new exchange panel and enter details as shown below by selecting Type as “topic” and click on Add exchange button to create an exchange (topic.exchange).
After creating an exchange (topic.exchange), next, we will create two queues (topic.bombay.queue and topic.delhi.queue) with different routing keys and bind them to the exchange (“topic.exchange”) in the web management portal.
To create a queue in rabbitmq web management portal, open http://localhost:15672/#/queues url and then go to Add a new queue panel and enter details as shown below, and click on Add queue button to create a queue (topic.bombay.queue).
Same way, repeat the above steps to create another queue with the name “topic.delhi.queue” as shown below.
After creating the queues, if you go to the Queues panel it will show the list of available queues as shown below.
After adding queues, next we will bind defined queues to exchange (“topic.exchange”).
To bind a queue with an exchange, click on queue (topic.bombay.queue) name, then the Bindings panel will expand and enter details like exchange name as “topic.exchange” and routing key pattern as “*.Bombay.*” and click on Bind button like as shown below.
After binding a queue (topic.bombay.queue) to exchange (topic.exchange) with routing key pattern (*.Bombay.*), the binding will be as shown below.
Here, the routing pattern *.Bombay.* means it will accept a routing key which will contain any text on/before Bombay like “Order.Bombay.Pizza”, “Book.Bombay.Hotels”, “Visit.Bombay.Parks”, “List.Bombay.Colleges”, etc.
Same way bind a queue (topic.delhi.queue) with an exchange (topic.exchange) using the routing key pattern (Delhi.#) as shown below.
After binding a queue (topic.delhi.queue) to exchange (topic.exchange) with routing key pattern (Delhi.#), the binding will be as shown below.
After the completion of binding a queue to exchange, now we will publish messages from the c# application to rabbitmq queue using RabbitMQ.Client service.
To publish messages to the rabbitmq queue, add a class with the name “Topicmessages.cs” in your application as shown below.
Now, open the Topicmessages.cs class file and write the code as shown below.
If you observe the above code, to establish a connection with rabbitmq server we are passing the required credentials along with the HostName to ConnectionFactory() method. After that, we are publishing a message (Message from Topic Exchange 'Bombay') to the queue by passing required parameters like an exchange, routing key, properties, and message to the BasicPublish method.
Here, we are sending a routing key as “Message.Bombay.Email” and it will match with the routing pattern (*.Bombay.*) and publish data to the respective queue (topic.bombay.queue).
Now open Program.cs class file and write a code in the Main method to call Topicmessages.cs class to get messages from rabbitmq.
Following is the code which we need to write in Program.cs class file Main method to publish messages to rabbitmq server.
When we execute the above c# program, we will get the result as shown below.
After executing the above program, if we check the queue (topic.bombay.queue) details in the rabbitmq web management portal, the Ready column is having 1 as shown below which means the message was published successfully to the topic.bombay.queue.
After publishing a message to the queue, now will learn how to consume or get messages from rabbitmq using RabbitMQ.Client service in c# or .net application with examples
Let’s create a simple console application with the Name “RabbitMQConsumer” as shown below to consume or read messages from the rabbitmq queue.
After creating an application, now add “RabbitMQ.Client” NuGet package reference to your application as we did in publishing the application to communicate with the rabbitmq server.
After installing RabbitMQ.Client, next add a class with the name “MessageReceiver.cs” in your application as shown below to read or get messages from queues in rabbitmq.
Now, open MessageReceiver.cs class file and write the code as shown below.
If you observe the above code, we created a MessageReceiver class and it’s inheriting from DefaultBasicConsumer class of RabbitMQ.Client service and we implemented a HandleBasicDeliver method by overriding it to receive a message body.
Now open Program.cs class file and write a code in the Main method to call MessageReceiver.cs class to get messages from rabbitmq.
Following is the code which we need to write in Program.cs class file Main method to receive data from rabbitmq server.
If you observe the above example, to establish a connection with rabbitmq server we are passing the required credentials along with the HostName to ConnectionFactory() method. After that, we created a connection and channel by calling the “CreateConnection” and “CreateModel” methods and we set a prefetchCount to 1, so that it tells RabbitMQ not to give more than one message at a time to the worker.
Next, we created an instance of MessageReceiver class and passed IModel (channel) to it, in the final step we called the “BasicConsume” method and passed the queue name to it “topic.bombay.queue” along with we have set autoAck to false and passed the messageReceiver instance to it.
Here, prefetchCount is used to tell RabbitMQ not to give more than one message at a time to the worker. Or, in other words, don't dispatch a new message to a worker until it has been processed and acknowledged the previous one. Instead, it will dispatch to the next worker that is not still busy.
When we execute our application, we will get a message from the queue (topic.bombay.queue) as shown below.
In the same way, we can publish and consume messages from the “topic.delhi.queue” queue by using routing keys like “Delhi.Pizza”, “Delhi.Hotels”, “Delhi.Parks”, “Delhi.Colleges”, etc. based on our requirements.