postwait / node-amqp

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

connection not getting closed #252

Closed sachinkale closed 10 years ago

sachinkale commented 10 years ago

I have express server setup to listen post request and put the post request in queue

var express = require('express'); var app = express(); app.use(express.bodyParser());

app.post('/test-page', function(req, res) { var amqp = require('amqp'); var connection = amqp.createConnection({url: "amqp://guest:guest@localhost:5672"},{defaultExchangeName: ''}); connection.on('ready',function(){ console.log('connected'); var messageToSend = req.body; var queueToSendTo = "xyz"; connection.queue(queueToSendTo,{'passive': true},function(){ connection.publish(queueToSendTo, messageToSend); connection.end(); res.send(200); //}); });

});

});

app.setMaxListeners(0);

app.listen(80);

The above code is suppose to collect the post request and put in queue, If I send 10 requests, there would be more than 300 messages in queue. I don't understand this behaviour or may be my understanding of putting 'publish' call in 'ready' function is wrong since the 'connected' log message in above code is printed more than 10 for 10 post request.

Is it happening due to 'connection.end' not closing the connection?

Please advise.

Thanks Sachin

P.S: I am using latest master of node-amqp with rabbit-server-3.1.4-1 on ubuntu 12.04

adione commented 10 years ago

Default connection options are set to auto-reconnect so to really close it you should set the options to false first and then invoke the close, e.g.:

connection.implOptions.reconnect = false;
connection.end();
sachinkale commented 10 years ago

Thank you that solved the issue.