refactored sender

This commit is contained in:
Giuseppe Privitera 2015-08-03 13:01:28 +01:00
parent 7f8f3d3c51
commit ea4d9dbbbf

View File

@ -3,17 +3,26 @@
var amqp = require('amqplib');
var when = require('when');
amqp.connect('amqp://localhost').then(function(conn) {
return when(conn.createChannel().then(function(ch) {
var conn = amqp.connect('amqp://localhost');
var ch = conn.then(createChannel).then(null, console.warn);
var q = 'hello';
var ok = ch.assertQueue(q, {durable: false});
return ok.then(function(_qok) {
var msg = 'Hello World!';
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent '%s'", msg);
return ch.close();
function createChannel(conn) {
return when(
conn.createChannel().
then(sendMessage)).
ensure(function() {
conn.close();
});
})).ensure(function() { conn.close(); });
}).then(null, console.warn);
}
function sendMessage(ch) {
var q = 'hello';
var ok = ch.assertQueue(q, {durable: false});
return ok.then(function(_ignore) {
var msg = 'Hello World!';
ch.sendToQueue(q, new Buffer(msg));
console.log(" [x] Sent '%s'", msg);
return ch.close();
});
}