MQ.once('storedRedelivery', function(_queueName) { // Here we make sure that the listener received the message the first time. But this does not // ensure it doesn't receive it the second time. That is what the `assert.fail` is for in the // listener assert.equal(handledMessages, 1); assert.equal(queueName, _queueName); // Make sure we can take the item off the redelivery queue MQ.subscribeQueue('oae-util-mq-redeliverqueue', {'prefetchCount': 1}, function(data, listenerCallback) { assert.ok(data); assert.ok(data.headers); assert.equal(data.deliveryInfo.queue, queueName); assert.equal(data.deliveryInfo.exchange, exchangeName); assert.equal(data.deliveryInfo.routingKey, routingKey); assert.equal(data.data.data, 'test'); // Don't accept any more messages on this queue MQ.unsubscribeQueue('oae-util-mq-redeliverqueue', function(err) { assert.ok(!err); // Acknowledge the redelivered message so it doesn't go in an infinite redelivery loop listenerCallback(); return callback(); }); }); });
MQ.declareQueue(queueName, {'durable': false, 'autoDelete': true}, function(err) { assert.ok(!err); var handledMessages = 0; var listener = function(msg) { handledMessages++; // We should only receive one message assert.strictEqual(handledMessages, 1); // Verify the message we receive is correct assert.strictEqual(msg.text, data.text); }; MQ.subscribeQueue(queueName, {}, listener, function(err) { assert.ok(!err); MQ.bindQueueToExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); MQ.submit(exchangeName, routingKey, data, function() { // Unbind the queue from the exchange, we should no longer receive any messages MQ.unbindQueueFromExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); // Submit one more message. If it ends up at our listener the test will fail MQ.submit(exchangeName, routingKey, data, function() { return callback(); }); }); }); }); }); });
MQ.declareQueue(queueName, {'durable': false, 'autoDelete': true}, function(err) { assert.ok(!err); // This test passes if we receive a message from RabbitMQ for each of our routing keys var receivedMessage = _.after(routingKeys.length, function(message) { return callback(); }); // Subscribe for incoming messages MQ.subscribeQueue(queueName, {}, receivedMessage, function(err) { assert.ok(!err); // When our queue is bound for all routing keys, we will submit a message for each one var queueBound = _.after(routingKeys.length, function() { _.each(routingKeys, function(routingKey) { MQ.submit(exchangeName, routingKey, data); }); }); // Bind our queue for all routing keys _.each(routingKeys, function(routingKey) { MQ.bindQueueToExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); queueBound(); }); }); }); });
MQ.declareQueue(queueName, QueueConstants.queue.OPTIONS, function(err) { if (err) { return callback(err); } // Subscribe to our queue for new events return MQ.subscribeQueue(queueName, QueueConstants.subscribe.OPTIONS, _handlePushActivity, callback); });
MQ.declareQueue(queueName, {'durable': false, 'autoDelete': true}, function(err) { assert.ok(!err); var listener = function(msg) { // Verify the message we receive is correct assert.strictEqual(msg.text, data.text); // Unbind the queue so both the queue and exchange will go away when we restart rabbitmq-server MQ.unbindQueueFromExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); return callback(); }); }; MQ.subscribeQueue(queueName, {}, listener, function(err) { assert.ok(!err); MQ.bindQueueToExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); MQ.submit(exchangeName, routingKey, data); }); }); });
MQ.declareQueue(queueName, {'durable': false, 'autoDelete': true}, function(err) { assert.ok(!err); // A listener that ensures it only handles the rejected message once var handledMessages = 0; var listener = function(msg, callback) { handledMessages++; if (handledMessages > 1) { // Throw in a new tick to ensure it doesn't get caught by MQ for automatic acknowledgement process.nextTick(function() { assert.fail('Should only have handled the message at most once'); }); } }; // Subscribe to the queue and allow it to start accepting messages on the exchange MQ.subscribeQueue(queueName, {'ack': true}, listener, function(err) { assert.ok(!err); MQ.bindQueueToExchange(queueName, exchangeName, routingKey, function(err) { assert.ok(!err); // Submit a message that we can handle MQ.submit(exchangeName, routingKey, {'data': 'test'}, null, function(err) { assert.ok(!err); }); // When the raw message comes in, reject it so it gets redelivered _bindPreHandleOnce(queueName, function(_queueName, data, headers, deliveryInfo, message) { // Reject the message, indicating that we want it requeued and redelivered MQ.rejectMessage(message, true, () => { // Ensure that rabbitmq intercepts the redelivery of the rejected message and stuffs it in the redelivery queue // for manual intervention MQ.once('storedRedelivery', function(_queueName) { // Here we make sure that the listener received the message the first time. But this does not // ensure it doesn't receive it the second time. That is what the `assert.fail` is for in the // listener assert.equal(handledMessages, 1); assert.equal(queueName, _queueName); // Make sure we can take the item off the redelivery queue MQ.subscribeQueue('oae-util-mq-redeliverqueue', {'prefetchCount': 1}, function(data, listenerCallback) { assert.ok(data); assert.ok(data.headers); assert.equal(data.deliveryInfo.queue, queueName); assert.equal(data.deliveryInfo.exchange, exchangeName); assert.equal(data.deliveryInfo.routingKey, routingKey); assert.equal(data.data.data, 'test'); // Don't accept any more messages on this queue MQ.unsubscribeQueue('oae-util-mq-redeliverqueue', function(err) { assert.ok(!err); // Acknowledge the redelivered message so it doesn't go in an infinite redelivery loop listenerCallback(); return callback(); }); }); }); }); }); }); }); });