I played with RabbitMq.net and message confirmations. If the consumer can process the message, you can send back ack in the form
channel.BasicAck(ea.DeliveryTag, false);
which will get him out of line.
But what about whether it is impossible to process the message? perhaps a temporary outage, and you don’t want the message removed from the queue to be simply inserted in the back and continued from the next message?
I tried using
channel.BasicNack(ea.DeliveryTag, false, true);
but next time he will still receive the same message and will not go to the next message in the queue
my full code
class Program
{
private static IModel channel;
private static QueueingBasicConsumer consumer;
private static IConnection Connection;
static void Main(string[] args)
{
Connection = GetRabbitMqConnection();
channel = Connection.CreateModel();
channel.BasicQos(0, 1, false);
consumer = new QueueingBasicConsumer(channel);
channel.BasicConsume("SMSQueue", false, consumer);
while (true)
{
if (!channel.IsOpen)
{
throw new Exception("Channel is closed");
}
var ea = consumer.Queue.Dequeue();
string jsonified = Encoding.UTF8.GetString(ea.Body);
var message = JsonConvert.DeserializeObject<SmsRecords>(jsonified);
if (ProcessMessage())
channel.BasicAck(ea.DeliveryTag, false);
else
channel.BasicNack(ea.DeliveryTag, false, true);
}
}
private static bool ProcessMessage()
{
return false;
}
public static IConnection GetRabbitMqConnection()
{
try
{
var connectionFactory = new ConnectionFactory
{
UserName = "guest",
Password = "guest",
HostName = "localhost"
};
return connectionFactory.CreateConnection();
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
return null;
}
}
}
source
share