Get total messages count in RabbitMQ

4,836 views
Skip to first unread message

Jorge Fabá Ferrández

unread,
Feb 23, 2015, 6:47:07 AM2/23/15
to rabbitm...@googlegroups.com

Hi,


I need to know the total messages in RabbitMQ from .NET client. I know the best solution is using API HTTP but I doesn't work properly.

I use confirmation of published messages, but I don't know why it doesn't work.

I use this code in order to publish a message in RabbitMQ:

private void message(string text)
{
    var factory = new ConnectionFactory() { HostName = hostName, UserName = user, Password = password };
    using (var connection = factory.CreateConnection())
    using (var channel = connection.CreateModel())
    {
        try
        {

            channel.ExchangeDeclare(exchangeNameProducer, "direct", true);
            channel.QueueDeclare(queueNameProducer, true, false, false, null);
            channel.ConfirmSelect();
            channel.QueueBind(queueNameProducer, exchangeNameProducer, "");
            var body = Encoding.UTF8.GetBytes(text);
            channel.BasicPublish(exchangeNameProducer, "", null, body);
            channel.WaitForConfirmsOrDie();
            log("After WaitForConfirm ,messageCount = " + this.getMessagesCount() + " ; message = " + text);
        }
        catch (Exception e)
        {
            log(e.Message);
        }
    }
}
 

"getMessagesCount" function asks to HTTP API to know total message count. Notice I use WaitForConfirmsOrDie and ConfirmSelect functions in order to wait until the message is published.

In my example, the consumer takes 30 seconds processing the message. However, the log always prints 0 messages remaining, but If I debug the code, the log prints 1 message remaining. If I use this code (waiting 1 second after publishing the message):

channel.BasicPublish(exchangeNameProducer, "", null, body);
channel.WaitForConfirmsOrDie();
Thread.Sleep(1000);
log("After WaitForConfirm ,messageCount = " + this.getMessagesCount() + " ; message = " + text);


then it works properly and the log writes "1 message left"

I think RabbitMQ lasts a few seconds to update message count from HTTP API, or it doesn't wait in WaitForConfirmsOrDie function.

Could you help me, please?

Thank you very much in advance.


Simon MacMullen

unread,
Feb 23, 2015, 6:53:03 AM2/23/15
to Jorge Fabá Ferrández, rabbitm...@googlegroups.com
On 23/02/15 11:47, Jorge Fabá Ferrández wrote:
> I think RabbitMQ lasts a few seconds to update message count from HTTP
> API, or it doesn't wait in WaitForConfirmsOrDie function.

Statistics in the HTTP API in general are not guaranteed to be up to
date, they will typically be 5-10 seconds behind the times.

If your client really needs an up to date count of messages in the queue
it can declare the queue again over AMQP and use the number returned
there. That's the equivalent of "messages_ready" in the HTTP API though,
it does not include unacknowledged messages out for delivery.

In general writing a client which decides what to do based on message
counts is likely to be fragile. What are you really trying to do?

Cheers, Simon

Jorge Fabá Ferrández

unread,
Feb 23, 2015, 7:00:24 AM2/23/15
to rabbitm...@googlegroups.com, jfa...@gmail.com
Firstly thank you very much for your answer.
I need to know total messages (ready + unacknowledged) because the client needs to ensure that the consumer has processed all message in order to publish new messages.

Is it possible to know total messages (ready + unacknowledged) using only .net client?

Thanks!!!!

Michael Klishin

unread,
Feb 24, 2015, 4:58:33 AM2/24/15
to rabbitm...@googlegroups.com, Jorge Fabá Ferrández
 On 23 February 2015 at 15:00:25, Jorge Fabá Ferrández (jfa...@gmail.com) wrote:
> Is it possible to know total messages (ready + unacknowledged)
> using only .net client?

No, queue.declare-ok in the protocol carries the number of messages ready to be consumed,
so that's not possible without using HTTP API regardless of the client.

EasyNetQ has an HTTP API client you may like:
https://github.com/EasyNetQ/EasyNetQ/wiki/Management-API-Introduction
--
MK

Staff Software Engineer, Pivotal/RabbitMQ

Jorge Fabá Ferrández

unread,
Feb 24, 2015, 5:10:50 AM2/24/15
to rabbitm...@googlegroups.com, jfa...@gmail.com
Ok, thank you very much.
Is there any way to update RabbitMQ statistics?
This way I could update statistics and, after that, query the HTTP API to get total message count.
Is it possible?

Thank you in advance.

Michael Klishin

unread,
Feb 24, 2015, 5:12:21 AM2/24/15
to rabbitm...@googlegroups.com, Jorge Fabá Ferrández
 On 24 February 2015 at 13:10:51, Jorge Fabá Ferrández (jfa...@gmail.com) wrote:
> Is there any way to update RabbitMQ statistics?
> This way I could update statistics and, after that, query the
> HTTP API to get total message count.
> Is it possible?

Sorry, w hat do you mean by "update statistics"? The stats/metrics available over HTTP API are
collected by RabbitMQ itself. You don't need to "refresh" them.

Jorge Fabá Ferrández

unread,
Feb 24, 2015, 5:17:19 AM2/24/15
to rabbitm...@googlegroups.com, jfa...@gmail.com
Simon MacMullen says "Statistics in the HTTP API in general are not guaranteed to be up to
date, they will typically be 5-10 seconds behind the times. "
I need updated statistics in order to know total message count.
Maybe there's a way to update this statistics and then query them.

Michael Klishin

unread,
Feb 24, 2015, 5:22:00 AM2/24/15
to rabbitm...@googlegroups.com, Jorge Fabá Ferrández
On 24 February 2015 at 13:17:21, Jorge Fabá Ferrández (jfa...@gmail.com) wrote:
> Simon MacMullen says "Statistics in the HTTP API in general
> are not guaranteed to be up to
> date, they will typically be 5-10 seconds behind the times. "
> I need updated statistics in order to know total message count.
> Maybe there's a way to update this statistics and then query them.

Ah, yes, the events are emitted asynchronously and as queues may be constantly moving,
the data may be some time behind but quite often that amount of time is insignificant.

There is no way to "refresh" the data: new events may be in flight at any given moment. You can
take a few snapshots and base your application decision of a series of measurements. This may lead
to better accuracy in decision making .

Jorge Fabá Ferrández

unread,
Feb 24, 2015, 5:25:05 AM2/24/15
to rabbitm...@googlegroups.com, jfa...@gmail.com
Ok, thank you very much!!
Reply all
Reply to author
Forward
Message has been deleted
0 new messages