Reece
Reece

Reputation: 764

NodeJS + RabbitMQ - How to limit the number of messages a receiver processes

I am using the amqplib node module and following the hello world send/receive tutorial.

https://github.com/squaremo/amqp.node/tree/master/examples/tutorials

My receivers/workers take that message and perform a CPU intensive task in the background, so I can only process about 5 messages at once.

What is the best way to control the number of messages that are being accepted by the receiver.

Code sample:

var amqp = require('amqplib');

amqp.connect('amqp://localhost').then(function(conn) {
    process.once('SIGINT', function() { conn.close(); });
    return conn.createChannel().then(function(ch) {

        var ok = ch.assertQueue('hello', {durable: false});

        ok = ok.then(function(_qok) {
            return ch.consume('hello', function(msg) {
                 console.log(" [x] Received '%s'", msg.content.toString());
            }, {noAck: true});
        });

            return ok.then(function(_consumeOk) {
            console.log(' [*] Waiting for messages. To exit press CTRL+C');
        });
    });
}).then(null, console.warn);

Upvotes: 0

Views: 1003

Answers (1)

jhilden
jhilden

Reputation: 12449

You need to set the Quality Of Service on the model. Here is how you would do that in C#

var _model = rabbitConnection.CreateModel();
// Configure the Quality of service for the model. Below is how what each setting means.
// BasicQos(0="Dont send me a new message untill I’ve finshed",  _fetchSize = "Send me N messages at a time", false ="Apply to this Model only")
_model.BasicQos(0, _fetchSize, false);

The QOS works with the Ack process. So until you Ack a message, it will only send you N (_fetchSize) at a time. I think you'll have to set noAck: false in your code to get this working.

Good luck!

Upvotes: 1

Related Questions