Code Geass
Code Geass

Reputation: 291

RabbitMQ - Get total count of messages enqueued

I have a Java client which monitors RabbitMQ queue. I am able to get the count of messages currently in queue with this code

@Resource
RabbitAdmin rabbitAdmin;
..........

DeclareOk declareOk = rabbitAdmin.getRabbitTemplate().execute(new ChannelCallback<DeclareOk>() {
        public DeclareOk doInRabbit(Channel channel) throws Exception {
            return channel.queueDeclarePassive("test.pending");
        }
    });
     return declareOk.getMessageCount();

I want to get some more additional details like -

  1. Message body of currently enqueued items.
  2. Total number of messages that was enqueued in the queue since the queue was created.

Is there any way to retrieve these data in Java client?

Upvotes: 16

Views: 57515

Answers (3)

Hemant Thorat
Hemant Thorat

Reputation: 2456

To access queue details via http api,

http://public-domain-name:15672/api/queues/%2f/queue_name

To access queue details via command from localhost cli promt,

curl -i -u guest_uname:guest_password http://localhost:15672/api/queues/%2f/queue_name

Where, '%2f' is default vhost /. (%2f URL encoding of / character)

[NOTE - As mentioned in comment, management plugin needs to be enabled.]

Upvotes: 10

pinepain
pinepain

Reputation: 12859

With AMQP protocol (including RabbitMQ implementation) you can't get such info with 100% guarantee.

The closest number to messages count is messages count returned with queue.declare-ok (AMQP.Queue.DeclareOk in java AMQP client library).

Whilst messages count you receive with queue.declare-ok may match exact messages number enqueues, you can't rely on it as it doesn't count messages which waiting acknowledges or published to queue during transaction but not committed yet.

It really depends what kind of precission do you need.

As to enqueued messages body, you may want to manually extract all messages in queue, view their body and put them back to queue. This is the only way to do what you want.

You can get some information about messages count with Management Plugin, RabbitMQ Management HTTP API and rabbitmqctl util (see list_queues, list_channels).

You can't get total published messages count since queue was created and I think nobody implement such stats while it useless (FYI, with messages flow in average 10k per second you will not even reach uint64 in a few thousand years).

Upvotes: 19

Giumbix
Giumbix

Reputation: 151

AMQP.Queue.DeclareOk dok = channel.queueDeclare(QUEUE_NAME, true, false, false, queueArgs);
dok.getMessageCount();

Upvotes: 15

Related Questions