AlexD
AlexD

Reputation: 4310

RabbitMQ with NodeJS - using amqplib to get message count

How do I get the number of messages currently en-queued?

My code is basically the following:

function readQueue() {
    var open = require('amqplib').connect(config.rabbitServer);

    open.then(function (conn) {
        var ok = conn.createChannel();
        ok = ok.then(function (ch) {
            ch.prefetch(config.bulkSize);

            setInterval(function () {
                handleMessages();
            }, config.bulkInterval);

            ch.assertQueue(config.inputQueue);
            ch.consume(config.inputQueue, function (msg) {
                if (msg !== null) {
                    pendingMessages.push(msg);
                }
            });
        });
        return ok;
    }).then(null, console.warn);
}

I found nothing in the documentation or while debugging, and I did see a different library that allows this, so wondering if amqplib supports this as well.

Upvotes: 8

Views: 11649

Answers (4)

Egor Ermolaev
Egor Ermolaev

Reputation: 61

You should call channel.checkQueue(queueName) and then you will get an object { queue: 'queueName', messageCount: 1, consumerCount: 0 } where the property messageCount which is the exactly current number of messages in the queue

Upvotes: 5

Deniz Husaj
Deniz Husaj

Reputation: 99

You can get the queue-length with amqplib.

In my case the queue has the feature 'durable:true'. You have to pass it as an option.

var amqp = require('amqplib/callback_api');

amqp.connect(amqp_url, function(err, conn) {
  conn.createChannel(function(err, ch) {
    var q = 'task2_queue';

    ch.assertQueue(q, {durable: true}, function(err, ok) {
      console.log(ok);
    });
  });
});

It will return an object like this:

{ queue: 'task2_queue', messageCount: 34, consumerCount: 2 }

For more information: https://www.squaremobius.net/amqp.node/channel_api.html#channel_assertQueue

Upvotes: 5

Vikesh
Vikesh

Reputation: 1

I couldn't find a direct solution using node, but by using api from RabbitMQ I was able to get message count. After enabling management plugin of RabbitMQ the apis can be accessed using http://127.0.0.1:15672/api/queues/vhost/name and user login as guest with password guest.

var request = require('request');
var count_url = "http://guest:[email protected]:15672/api/queues/%2f/" + q;
var mincount = 0;
..........
..........
request({
    url : count_url
}, function(error, response, body) {
    console.log("Called RabbitMQ API");
    if (error) {
        console.error("Unable to fetch Queued Msgs Count" + error);
        return;
    }
    else
    {
        var message = JSON.parse(body);

        if (message.hasOwnProperty("messages_ready")) {
            // this DOES NOT COUNT UnAck msgs
            var msg_ready = JSON.stringify(message.messages_ready);
            console.log("message.messages_ready=" + msg_ready);
            if (msg_ready == mincount) {
                console.log("mincount Reached ..Requesting Producer");
                ///Code to Produce msgs  ..
            }
        }
        if (message.hasOwnProperty("messages")) {
            // _messages_ total messages i.e including unAck
            var msg = JSON.stringify(message.messages);
            console.log("message.messages=" + msg);
        }
    }
});

Upvotes: 0

Derick Bailey
Derick Bailey

Reputation: 72888

I think the assertQueue method call will return an object that contains the current message count. I don't remember the exact property name off-hand, but it should be in there.

The real trick, though, is that this number will never be updated once you call assertQueue. The only way to get an updated message count is to call assertQueue again. This can have some performance implications if you're checking it too frequently.

Upvotes: 3

Related Questions