Pregunta

I'm using RabbitMQ with node-amqp lib. I'm publishing messages with mandatory flag set, and when there is no route to any queue, RabbitMQ responds with basic.return as in specification.

My problem is that, as far as I can tell, basic.return is asynchronous and does not contain any information about for which message no queue was found. Even when exchange is in confirm mode). How the hell am I supposed to tell which message was returned?

¿Fue útil?

Solución

node-amqp emits 'basic-return' event on receiving the basic.return from amqp. Only thing of any use there is routing key. Since all messages with the same routing key are routed the same way. I assumed that once I get a basic.return about a specific routing key, all messages with this routing key can be considered undelivered

function deliver(routing_key, message, exchange, resolve, reject){
    var failed_delivery = function(ret){
        if(ret.routingKey == routing_key){
            exchange.removeListener('basic-return', failed_delivery);
            reject(new Error('failed to deliver'));
        }
    };
    exchange.on('basic-return', failed_delivery);
    exchange.publish( 
        routing_key,
        message,
        {   deliveryMode: 1, //non-persistent
            mandatory: true
        }, function(error_occurred, error){
            exchange.removeListener('basic-return', failed_delivery);
            if(error_occurred){
                reject(error);
            } else {
                resolve();
            }
    });
}

Otros consejos

I read the AMQP spec, because I've used the Basic Return without a problem before, but I'm also using the .NET client. I looked through the documentation on node-amqp, and I can't even see that it implements Basic.Return.

In any event, the server does respond with the full message when it could not be published. You may consider switching to a different Node.js library (for example, amqplib does have this feature (marked as Channel#on('return', function(msg) {...})).

Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top