techspider techspider - 3 months ago 37
C# Question

RabbitMQ - Know which message is Acked

I'm trying to publish two messages and receiv Ack/Nack. Below is my code:

using (var connection = factory.CreateConnection())
{
using (var channel = connection.CreateModel())
{
channel.BasicAcks += channel_BasicAcks;
channel.BasicNacks += channel_BasicNacks;

channel.QueueDeclare("test", durable, false, false, null);
channel.ConfirmSelect();

var properties = channel.CreateBasicProperties();
properties.SetPersistent(true);

string message = "Hello World!";
var body = Encoding.UTF8.GetBytes(message);

channel.BasicPublish("", "test", properties, body);
channel.BasicPublish("", "test", properties, body);
Console.WriteLine(" [x] Sent {0}", message);

channel.WaitForConfirmsOrDie();
}
}


My Ack/Nack events

static void channel_BasicNacks(object sender, RabbitMQ.Client.Events.BasicNackEventArgs e)
{

}

static void channel_BasicAcks(object sender, RabbitMQ.Client.Events.BasicAckEventArgs e)
{

}


For those two messages, I perfectly received two Ack event calls. I'm even able to e.DeliveryTag as 1 and 2 (I hope those are sequence numbers of the messages I processed).

I want to know which message is acked / nacked more precisely rather than depending on Sequence number to do certain actions.

Is there a way that I can append a unique identifier to message and get the same id back in Ack / Nack event args to verify that a particular message is acked / nacked. Thanks for your pointers.

Let us say for example, I have been polling a folder and reading list of files from that folder and publishing the content to a queue. A file might be acked; another might be nacked; I want to know which file is acked or nacked so that I can move file to respective success / failed folders.

Answer

AMQP's Basic.Ack method does not have room for user-specified content, so no, you can't use an arbitrary ID.

However, you can query the next sequence ID which will be used for the next publish, and maintain a map locally to associate messages to their sequence ID.

Sequence IDs start at 1, so NextPublishSeqNo() will return 1 before any publish, 2 after the first publish and so on.