Search code examples
node.jsasynchronousamqp

node-amqp and async ack


I am working on a project that uses rabbitmq. The queue I have to work with (lets call it "testq" gets from time to time about 35000 items that have to be worked on.

basically (very short):

var q = new Queue();
q.subscribe('testq', { ack: true, prefetchCount: 100 }, function(doc, object, queueOptions, originalDocument) {
//do some action
originalDocument.acknowledge();

});

My problem now is: I want to get 100 messages at a time (prefetch count = 100), but when do I know that all 100 messages have been processed so that I can acknowledge and get the next 100 messages? The "do some action block" is async :(

Thanks for any advice!


Solution

  • You acknowledge messages one by one and you should not worry about getting next 100 messages while it's a broker job to deliver messages to you. There is nothing wrong to have do some action asynchronous, just make acknowledge after some action is done, I guess it can be done via callbacks, as everything done in node.js. Something like that:

    q.subscribe(
        'testq',
        { ack: true, prefetchCount: 100 },
    
        function(doc, object, queueOptions, originalDocument) {
            doSomeAction(function() {
                originalDocument.acknowledge();
            });
        }
    );