I am using RabbitMQ to get some messages in a queue. One by One. This is a snippet of my code:
var data = channel.BasicGet(queue, true);
if (data != null)
message = System.Text.Encoding.UTF8.GetString(data.Body);
else
message = "";
return message;
But i would like to know how to use the Ack property? How can i confirm/cancel the message that was read?
Thanx!
From the Docs:
Since noAck = false above, you must also call IModel.BasicAck to acknowledge that you have successfully received and processed the message:
i.e. you have two different options with BasicGet
With noAck
true
channel.BasicGet(queue, true);
This way, you don't need to ack the message - the message will be removed from the queue after the BasicGet
. This usage is typically for low-value messages - if processing fails after the BasicGet
, the message will be lost.
Or, with noAck
false:
var result = channel.BasicGet(queue, false);
// Process the message here ... e.g. save to DB
// If the processing succeeds, Ack to remove the message
channel.BasicAck(result.DeliveryTag, false);
This second option would be used for important messages, where loss of a message is not an option. If your consuming process crashes and is unable to acknowledge the message, the message is returned to the queue.