尝试为消息(个人)设置过期,队列中仍然有 'ready'?

Trying to set expiration to messages (individual), still 'ready' in queue?

我正在尝试将 RabbitMQ 消息发布到队列(默认情况下没有设置 TTL),其中发布的每条消息都可以指定一个单独的 TTL。我相信我在 C# 中按如下方式执行此操作:

    public override bool PostData(object data, int? ttl = null)
    {
        try
        {
            _channel.QueueDeclare(_queue, true, false, false, null);

            var body = NET.ObjectToByteArray(data);
            var properties = _channel.CreateBasicProperties();
            if (ttl != null)  // <----- here
                properties.Expiration = ttl.ToString();
            properties.SetPersistent(true);

            // Publish the actual message on the specified queue
            _channel.BasicPublish("", _queue, properties, body);
        }
        catch (Exception e)
        {
            Console.WriteLine(e.StackTrace);
        }

        return false;
    }

我可以使用 properties.IsExpirationPresent() which returns true 检查过期设置。但是,如果我检查有关队列的高级信息(通过管理插件),我会看到队列中有我发布的 N 消息,即使 TTL 已过(30 秒),队列仍然将它们设为 Ready

是否应该在 TTL 到期时将它们从队列中移除,还是仍然在队列中看到它们是否正常?

您可以设置两个 Per-Queue Message TTL and individual Per-Message TTL. If both set, the shortest one will be used. Messages with expired TTL removed from queue when they reach it head (see Caveats 部分以获取详细信息)。

此外,来自 another answer about TTL in RabbitMQ

Messages with expired ttl will stay in queue as long as they not reached queue head. Don't worry, they will not be send to consumer, but they will take some resources until they reach head. This is how RabbitMQ queues works (they stick to FIFO idea, which is sometimes may break strict compatibility with AMQP protocol). See Caveats section in Time-To-Live Extensions for more.