Junior — Middle
How to choose a queue for sending messages in RabbitMQ?
sobes.tech AI
Answer from AI
In RabbitMQ, the queue for sending a message is selected when publishing by specifying the queue name or exchange and routing key.
If you send a message directly to a queue, you specify the queue name in the BasicPublish method.
Example in C# using RabbitMQ.Client:
var factory = new ConnectionFactory() { HostName = "localhost" };
using(var connection = factory.CreateConnection())
using(var channel = connection.CreateModel())
{
string queueName = "myQueue";
channel.QueueDeclare(queue: queueName, durable: false, exclusive: false, autoDelete: false, arguments: null);
string message = "Hello RabbitMQ!";
var body = Encoding.UTF8.GetBytes(message);
// Sending message to the specified queue
channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
}
If a direct exchange is used, the queue selection depends on the binding settings between the exchange and queues, and when publishing, the exchange and routing key are specified, through which the message will reach the desired queue.