c#Rabbit MQ Unacked消息

时间:2018-02-20 10:44:43

标签: c# .net rabbitmq message-queue

尝试发送ACK以从Rabbit队列中删除消息时出现问题。

Rabbit MQ版本:3.6.15。 RabbitClient 5.0.1的版本

这是我的代码:

var conn = Factory.CreateConnection();
var channel = conn.CreateModel();


//inside loop
var data = Get(conn, channel);

if (data == null)
    return true;

if (data.MessageCount == 0)
    return true;

var rabbitShortAd = JsonConvert.DeserializeObject<ShortAdRabbit>(Encoding.UTF8.GetString(Decompress(data.Body)));

//Process rabbitShortAd, save it in DB 
//And if no error, send ack

SendAck(data, conn, channel);
return false;
//end loop


public static BasicGetResult Get(IConnection conn, IModel channel)
{
     if (conn == null || !conn.IsOpen)
        conn = Factory.CreateConnection();

    if (channel == null || !channel.IsOpen)
        channel = conn.CreateModel();

    var queueName = "descriptions";
    channel.QueueDeclare(queue: queueName, durable: true, exclusive: false, autoDelete: false);
    return channel.BasicGet(queueName, false);
}

private static void SendAck(BasicGetResult data)
{
    if (conn == null || !conn.IsOpen)
        conn = Factory.CreateConnection();

    if (channel == null || !channel.IsOpen)
        channel = conn.CreateModel();

    channel.BasicAck(data.DeliveryTag, false);
}

处理的第一条消息将从队列中删除,然后所有其他消息都堆叠在未包装的列中。我的管理系统告诉我他将Rabbit MQ版本更新到3.6.15,现在我无法确认我的消息。代码中有什么问题吗?

1 个答案:

答案 0 :(得分:0)

找到等待使其发挥作用。

我必须实例化连接&amp;循环中的模型,然后关闭内部的每个连接/模型。

//inside the loop
var conn = Factory.CreateConnection();
var channel = conn.CreateModel();

try
{
   var data = Get(conn, channel);

   //Process

   channel.BasicAck(data.DeliveryTag, false);
}
catch(Exception e)
{
   //handle e
}
finally
{
   conn?.Close();
   channel?.Close();
}
//end of loop