Is there a good example (C#) of how to do the direct reply-to in RabbitMQ? What I want to do is for X Producers to post a message ("I've got some work for somebody") and I want one of X Consumers to pick it up, do the work and send the response back. Not a basic Ack, but some data, the result of the calculation. Of course, the response has to go back to the right producer.
Producer:
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
var properties = channel.CreateBasicProperties();
properties.Persistent = true;
channel.BasicPublish(exchange: "",
routingKey: "hello",
basicProperties: properties,
body: body);
Console.WriteLine(" [x] Sent {0}", message);
}
}
Consumer:
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare(queue: "hello",
durable: true,
exclusive: false,
autoDelete: false,
arguments: null);
channel.BasicQos(prefetchSize: 0, prefetchCount: 1, global: false);
var consumer = new EventingBasicConsumer(channel);
consumer.Received += (model, ea) =>
{
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
};
channel.BasicConsume(queue: "hello",
noAck: false,
consumer: consumer);
Console.WriteLine(" Press [enter] to exit.");
Console.ReadLine();
}
It's not very clear from the minimal docs on how to set up both sides. I know somebody has to do something with the "amq.rabbitmq.reply-to" queue, but its not clear which side and what they have to do with it.
Have you seen the tutorials on the rabbitmq website? http://www.rabbitmq.com/tutorials/tutorial-six-dotnet.html
You would set up your code the same way as the RPC example, above, with only a few minor differences (noted in the docs you've referenced: https://www.rabbitmq.com/direct-reply-to.html).
When publishing a message from the original message producer, set the "replyTo" to amq.rabbitmq.reply-to
Have the original message producer also be a message consumer, consuming from the amq.rabbitmq.reply-to
queue
When the code that handles the original request is done processing, you will publish a message from that worker, through the default (empty, no-name, "") exchange, with the routing key also set to amq.rabbitmq.reply-to
So:
amq.rabbitmq.reply-to
queueamq.rabbitmq.reply-to
as the replyTo
propertyamq.rabbitmq.reply-to
as the routing keythat should be about it
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With