Loofer
Loofer

Reputation: 6965

Can RabbitMQ tell me how many times a message has been delivered?

I am consuming some messages from a RabbitMQ queue.

My consumption of the message can be flakey and so if I fail I would like to retry.

my handler code looks a bit like this

            ........
            var handlers = new GetHandlers();
            try
            {
                handlers.Handle<T>(output);
               //log handled
            }
            catch (Exception ex)
            {
                //log fail
                channel.BasicNack(ea.DeliveryTag, false, true);
                return;
            }


            channel.BasicAck(ea.DeliveryTag, false);
            // Log all good.
        };

There is a Redelivered = true/false property on the message, however that does not give me enough info to know how many times I have seen this message before.

Is there a way of getting Rabbit to set the number of delivery attempts?

Upvotes: 3

Views: 1205

Answers (1)

theMayer
theMayer

Reputation: 16157

Not in the current version. However, you can re-publish the message with this information included as a header. Alternatively, you could use some sort of distributed, in-memory cache (think memcached or Couchbase) to store this state information.

From an architectural standpoint, your application should not behave differently depending on how many times the message was tried. If your app cannot process it due to being malformed, it should reject it at once. If there is a temporary condition, then you should not consume messages until that condition has resolved. Obviously there are exceptions to every rule though.

Upvotes: 2

Related Questions