postwait / node-amqp

[UNMAINTAINED] node-amqp is an AMQP client for nodejs
MIT License
1.69k stars 357 forks source link

Messages rejected with `queue.shift(true, true)` aren't put back on the queue #210

Open saalaa opened 11 years ago

saalaa commented 11 years ago

The title pretty much says it all; in my tests, I was unable to put rejected messages back onto the queue.

Publisher code (don't let it run for too long):

var amqp = require('amqp');

var mq = amqp.createConnection({host: 'localhost'});

mq.on('ready', function () {
  var publish = function () {
    mq.publish('my-queue', {payload: 'Hello world!'});
  };

  setInterval(publish, 50);
});

Subscriber code:

var amqp = require('amqp');

var mq = amqp.createConnection({host: 'localhost'});

var options = {
  autoDelete: false,
  durable: true
};

mq.on('ready', function () {
  mq.queue('my-queue', options, function (q) {
    q.bind('#');

    q.subscribe({ack: true}, function (message) {
      console.log(message);
      q.shift(true, true);
    });
  });
});
saalaa commented 11 years ago

I looked at the tests and this one caught my attention:

https://github.com/postwait/node-amqp/blob/master/test/test-reject.js

I adjusted my code according to it and it's working. This bug is still valid since queue.shift() is not working as advertised as far as requeue is concerned.

For the record, here's the adjusted subscriber code:

var amqp = require('amqp');

var mq = amqp.createConnection({host: 'localhost'});

var options = {
  autoDelete: false,
  durable: true
};

mq.on('ready', function () {
  mq.queue('my-queue', options, function (q) {
    q.bind('#');

    q.subscribe({ack: true}, function (json, headers, info, message) {
      console.log(json);
      message.reject(true);
    });
  });
});
bausmeier commented 11 years ago

Your code in the original issue works perfectly for me. What version of node-amqp are you using? The options to reject and requeue a message using queue.shift have not been included in the version which has been published to npm yet. I expect that they will be included in 0.1.8.

bausmeier commented 11 years ago

The queue.shift code was removed when another pull request was merged in. See this comment.