I have a few questions on coding in RabbitMQ... I'm not new to this world and have got questions based on a design provided to me for implementation...
- If I send BasicAck or BasicAack from Consumer, does it only remove the corresponding message from the queue or will it deliver Ack to the Publisher?
- How do I ensure that Publisher sends message to Server only when Consumer is ready for processing?
The design says Publisher needs to wait and know when the processing of Consumer is completed to do certain task on client side (depending on success / failure).
I have tried below code but dequeue immediately removed message from queue without I send any Ack or Nack. I'm confused
publisher code:
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("test", durable, false, false, null);
channel.TxSelect();
var properties = channel.CreateBasicProperties();
properties.SetPersistent(true);
string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);
channel.BasicPublish("", "test", properties, body);
channel.TxCommit();
Console.WriteLine(" [x] Sent {0}", message);
}
}
Consumer code
using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.QueueDeclare("test", durable, false, false, null);
var consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("test", true, consumer);
Console.WriteLine(" [*] Waiting for messages." +
"To exit press CTRL+C");
while (true)
{
var ea = (BasicDeliverEventArgs)consumer.Queue.Dequeue();
var body = ea.Body;
var message = Encoding.UTF8.GetString(body);
Console.WriteLine(" [x] Received {0}", message);
}
}
}
Note: I realized that channel.BasicConsume("test", true, consumer);
has noAck to true. I changed this to channel.BasicConsume("test", false, consumer);
I can see the message is removed from the queue when I used channel.BasicAck(deliveryTag: ea.DeliveryTag, multiple: false);
But, how does Publisher know that the Consumer processed it successfully?