[rabbitmq-discuss] Reliable way to get number of messages in a queue

Matthew Sackman matthew at lshift.net
Wed Nov 18 17:18:53 GMT 2009


Hi Michael,

On Wed, Nov 18, 2009 at 04:58:43PM +0000, Michael Nacos wrote:
> I know there is nothing in the protocol supporting book-keeping information
> such as queue lengths etc.
> However, basic get ok returns a message count which is more or less correct,
> except when consumers are
> still attached to the queue (there's no connection closing until the very
> end of the experiment, we are reusing
> a few connections). I have been using rabbitmqctl to check to the queue
> lengths and according to rabbitmqctl
> the consumer processes do not keep up with the message generation rate but
> the external poller process gets
> a message count of zero. As soon as the consumers disconnect, message count
> reports the right number of
> unhandled messages again.

That's odd. However, you're really much much better off using
queue.declare to redeclare the existing queue - the declare-ok that you
get back contains the number of messages in the queue. This is a much
neater strategy than below. I would recommend you switch to using this
and repeat your test.

> Is this something that you have seen before/would expect? My polling
> strategy is: consume a single message
> from a queue with no_ack=false, read the message count, send no ack and
> disconnect.

That's a dangerous thing to do as it may change message order - the
message that is reinserted into the queue as a result of the absence of
the ack and the disconnect is not guaranteed to be placed at the head of
the queue - it may very likely be placed at the tail of the queue
instead.

Matthew




More information about the rabbitmq-discuss mailing list