amqp.connect(URI) .then((connection) => { const donePromise = destroy ? Promise.all( queues.map(deleteQueue(connection)).concat(exchanges.map(deleteExchange(connection))) ) : Promise.all(queues.map(clearQueue(connection))); return donePromise .then(() => connection.close()) .catch(() => {}); })
/** * Disconnect from an AMQP server * * @memberof AmqpTransporter * @description Close the connection and unbind this node's queues. * This prevents messages from being broadcasted to a dead node. * Note: Some methods of ending a node process don't allow disconnect to fire, meaning that * some dead nodes could still receive published packets. * Queues and Exchanges are not be deleted since they could contain important messages. */ disconnect() { this.connectionCount = 0; if (this.connection && this.channel && this.bindings) { return this.broker.Promise.all(this.bindings.map(binding => this.channel.unbindQueue(...binding))) .then(() => { this.channelDisconnecting = this.transit.disconnecting; this.connectionDisconnecting = this.transit.disconnecting; }) .then(() => this.channel.close()) .then(() => this.connection.close()) .then(() => { this.bindings = []; this.channel = null; this.connection = null; }) .catch(err => this.logger.warn(err)); } }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'hello'; var msg = 'Hello World!'; ch.assertQueue(q, {durable: false}); // Note: on Node 6 Buffer.from(msg) should be used ch.sendToQueue(q, new Buffer(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 500); });
amqp.connect('amqp://localhost').then(function(conn) { return when(conn.createChannel().then(function(ch) { var ex = 'logs'; var ok = ch.assertExchange(ex, 'fanout', {durable: true}) var message = process.argv.slice(2).join(' ') || Date.now().toString(); return ok.then(function() { ch.publish(ex, '', new Buffer(message)); console.log(" [x] Sent '%s'", message); return ch.close(); }); })).ensure(function() { conn.close(); }); }).then(null, console.warn);
async function init() { const connection = await amqp.connect(config.rabbitmq.url) const e = config.rabbitmq.exchange const channel = await connection.createChannel() await channel.assertExchange(e, 'fanout', { durable: false, autoDelete: true }) channel.publish(e, '', Buffer.from(process.argv[2] || 'something to do')) setTimeout(async () => { await connection.close() }, 500) }
disconnect() { return new Promise(resolve => { if (!this.conn) { resolve(); } this.conn.off('close', this.onClose); this.conn.close().then(() => { this.emit('close', this.conn); this.conn = null; resolve(); }); }); }
async function init() { const conn = await amqp.connect('amqp://localhost'); process.once('SIGINT', conn.close.bind(conn)); ch = await conn.createChannel(); logger.info('server has connected to mq'); ch.prefetch(1); }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var ex = 'topic_logs'; var args = process.argv.slice(2); var key = (args.length > 0) ? args[0] : 'anonymous.info'; var msg = args.slice(1).join(' ') || 'Hello World!'; ch.assertExchange(ex, 'topic', {durable: false}); ch.publish(ex, key, new Buffer(msg)); console.log(" [x] Sent %s: '%s'", key, msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 500); });
async function init() { const conn = await amqp.connect('amqp://localhost'); process.once('SIGINT', conn.close.bind(conn)); ch = await conn.createConfirmChannel(); logger.info('client has connected to mq'); await ch.assertQueue(queueName, { durable: false }); }
async function init() { const connection = await amqp.connect(config.rabbitmq.url) const q = config.rabbitmq.channel const channel = await connection.createChannel() await channel.assertQueue(q, { autoDelete: false }) channel.sendToQueue(q, Buffer.from(process.argv[2] || 'something to do')) setTimeout(async () => { await connection.close() }, 500) }
amqp.connect('amqp://localhost:5672', function (err, conn) { conn.createChannel(function (err, ch) { var q = 'hello'; var msg = 'Hello World 123!'; ch.assertQueue(q, { durable: false }); ch.sendToQueue(q, Buffer.from(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function () { conn.close(); process.exit(0) }, 500); });
async function init() { const conn = await amqp.connect('amqp://localhost'); process.once('SIGINT', conn.close.bind(conn)); ch = await conn.createChannel(); logger.info('client has connected to mq'); await ch.assertQueue(queueName, { durable: false }); }
amqp.connect('amqp://localhost', function(err, conn) { conn.createChannel(function(err, ch) { var q = 'myqueue'; var msg = 'hello world 1'; ch.assertQueue(q, {durable: false}); // Note: on Node 6 Buffer.from(msg) should be used ch.sendToQueue(q, Buffer.from(msg)); console.log(" [x] Sent %s", msg); }); setTimeout(function() { conn.close(); process.exit(0) }, 500); });