简体   繁体   中英

node.js AMQP not returning callback

I am using node AMQP module to connect to RabbitMQ. I am able to connect, create an exchange, queue and able to send/publish the message to the exchange. I can confirm the messages are published on the management console.

Problem is I am not receiving the callback for exchange publish call. This is my code.

Initialization: (app is the express.js instance)

                app.rabbitMQConnection = amqp.createConnection({ host: 'myurl.com', login: 'login', password: 'pwd' });
                app.rabbitMQConnection.on('ready', function(){
                    console.log("RabbitMQ server connected");
                    app.rabbitMQConnection_e = app.rabbitMQConnection.exchange('my-exchange', { confirm: true, durable: true, autoDelete: false }, function (q) {
                        app.rabbitMQConnection_q_lisorders = app.rabbitMQConnection.queue('shoe-orders', {autoDelete: false, durable: true}, function (q) {
                            app.rabbitMQConnection_q_lisorders.bind(app.rabbitMQConnection_e, '#');
                        });
                    });
                });

Then when I need to send a message I use:

                    app.rabbitMQConnection_e.publish('routingKey', { message: myMessage }, {  deliveryMode: 2 }, function(transmissionFailed){
                            if (transmissionFailed == true){ 
                                        console.log("message failed");
                            }else{
                                        console.log("message sent");
                            }
                    });

Callback function(transmissionFailed) is never called. Please help!

Doc is not clear enough. As we use it,

when calling: connection.exchange(name, options={}, openCallback)

Then callback has exchange object which is used to publish messages.

Your code would be:

app.rabbitMQConnection.exchange('my-exchange', { confirm: true, durable: true, autoDelete: false }, function (q) {
    app.rabbitMQConnection_e = q;
    .....
});

And then:

app.rabbitMQConnection_e.publish('routingKey', { message: myMessage }, {  deliveryMode: 2 }, function(transmissionFailed){
                        if (transmissionFailed == true){ 
                                    console.log("message failed");
                        }else{
                                    console.log("message sent");
                        }
                });

callback is a function that will get called if the exchange is in confirm mode, the value sent will be true or false, this is the presense of a error so true, means an error occured and false, means the publish was successfull

so we should set the exchange as confirm mode when create an exchange

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM