Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

RabbitMQ - Get total count of messages enqueued

Tags:

java

rabbitmq

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?

like image 277
Code Geass Avatar asked Jul 16 '13 18:07

Code Geass


People also ask

How do I get all messages from RabbitMQ?

exports = (connection, queue) => { init(connection, queue); return { getMessages: (queueName, cleanQueue) => new Promise((resolve) => { let messages = []; let i = 1; getChannel(). then((ch) => { ch. consume(queueName, (msg) => { messages. push(msg); console.

How many messages can RabbitMQ?

Queues are single-threaded in RabbitMQ, and one queue can handle up to about 50 thousand messages.

How do I get RabbitMQ metrics?

The web UIOnce you've started the broker and installed the RabbitMQ management plugin, you'll have access to a built-in metrics UI. Point a browser to the root of the web server, e.g., localhost:15672 , to see a number of dashboards. These correspond roughly to the endpoints of the HTTP API.

How do I track messages on RabbitMQ?

To actually trace messages a RabbitMQ user needs to bind a queue (this is the destination of the traced messages) to the amq. rabbitmq. trace exchange and use the appropriate routing key based on what we want to trace: # trace every message sent to any exchange and delivered by any queue.


1 Answers

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).

like image 189
pinepain Avatar answered Sep 19 '22 16:09

pinepain