Junior — Middle
What will be the result of delivering a message with arbitrary text when sending it to RabbitMQ using the standard exchange configuration?
sobes.tech AI
Answer from AI
When sending a message with arbitrary text to RabbitMQ using the default exchange configuration, the message will be delivered to the queue whose name matches the routing key specified during sending.
The default exchange is a direct exchange with an empty name, which automatically binds each queue to a routing key equal to the queue's name.
If, when publishing a message, you specify a routing key that matches an existing queue's name, the message will go to that queue. If no such queue exists, the message will be discarded (not delivered).
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 = System.Text.Encoding.UTF8.GetBytes(message);
// Send message to default exchange with routing key = queue name
channel.BasicPublish(exchange: "", routingKey: queueName, basicProperties: null, body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
If the routing key does not match the queue name, the message will not be delivered.