RabbitMQ Exchange deliver a message to one or multiple consumers (queues). This pattern is known as "publish/subscribe".
(The queue name is not the catch here, you can use even aqueue with unknown name).
There are 4 types of exchange: fanout, direct, topic and header.
The fanout exchange is very simple. it just broadcasts all the messages it receives to all the queues that is bounded to it (no routing key need).
The relationship between exchange and a queue is called a binding.
so add a binding to all queues in the consumers.
For example:
channel.QueueBind(queue: queueName,
exchange: "hello.fanout",
routingKey: "");
Sample of code:
Publisher
//declare an exchange
channel.ExchangeDeclare(exchange: "hello.fanout", type: ExchangeType.Fanout);
//Send a message to the exchange
var message = GetMessage(args);
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish(exchange: "hello.fanout",
routingKey: "",
basicProperties: null,
body: body);
Subscriber:
//declare an exchange
channel.ExchangeDeclare(exchange: "hello.fanout", type: ExchangeType.Fanout);
//bind the queue to the exchange
var queueName = channel.QueueDeclare().QueueName;
channel.QueueBind(queue: queueName,
exchange: "logs",
routingKey: "");
//declare a consumer event:
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body.ToArray();
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] {0}", message);
};
//add a consumer the a queue
channel.BasicConsume(queue: queueName,
autoAck: true,
consumer: consumer);
So if you want to deliver a message to multiple consumers.
You should publish a message to your exchange, and bind all your consumers to the same exchange as the publisher.
More information about exchanges and pub/sub in rabbitMQ website