Reputation: 7555
I am trying to publish RabbitMQ messages to a queue (which by default has no TTL set), where each message published can specify an individual TTL. I believe that I am doing this as follows in 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;
}
I am able to check that the expiration is set by using properties.IsExpirationPresent()
which returns true
. However, if I check high level information about the queue (via the management plugin), I see that the queue has N
messages which I have published, and even though the TTL has passed (30 seconds), the queue still has them as Ready
.
Should they be removed from the queue with the TTL expires, or is it normal to still see them in the queue?
Upvotes: 2
Views: 2682
Reputation: 12859
You can set both 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 section for details).
Also, from 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.
Upvotes: 4