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)); } }
.on("error", (err) => { .on("close", (err) => { this.connected = false; if (!this.connectionDisconnecting) { .on("blocked", (reason) => { this.logger.warn("AMQP connection is blocked.", reason); }) .on("unblocked", () => { this.logger.info("AMQP connection is unblocked."); }); .createChannel() .then((channel) => { this.channel = channel;
initialize() { return amqp .connect('amqp://localhost') .then(conn => conn.createChannel()) .then(channel => { this.channel = channel; return this.channel.assertQueue(this.qName); }) .then(q => this.queue = q.queue) .catch(err => console.log(err.stack)) ; }
async connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
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); });
subscribeQueue(queue, messageHandler) { return this.handler .then(conn => conn.createChannel()) .then(channel => { logger.info(`Consuming from ${queue}`); return channel.assertQueue(queue, { exclusive: false }) .then(() => channel.consume(queue, message => messageHandler(message.content.toString()), { noAck: true })) .then(() => logger.info(`Consumed from ${queue}`)) }) .catch(logger.error); }
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 }); }
const publish = async (exchangeName, exchangeType, message) => { const connect = await amqp.connect(config.rabbitMqUrl) const channel = await connect.createChannel() await channel.assertExchange(exchangeName, exchangeType, {durable: false}) await channel.publish(exchangeName, '', Buffer.from(message)) console.log('Message published: ', exchangeName, message) }
async function connect() { const connection = await amqp.connect('amqp://localhost:5672'); const channel = await connection.createChannel(); const queue = channel.assertQueue(QUEUES.JOBS); channel.consume(QUEUES.JOBS, (job) => { let data = JSON.parse(job.content); console.log(data); channel.ack(job); }); console.log('Waiting for the message!'); }
amqp .connect('amqp://localhost') .then(conn => { connection = conn; return conn.createChannel(); }) .then(ch => { channel = ch; produce(); }) .catch(err => console.log(err)) ;
const listen = async () => { // connect to RabbitMQ let connection = await amqp.connect(messageQueueConnection) // create a channel and prefetch one message at a time let channel = await connection.createChannel() await channel.prefetch(1) // start consuming messages await consume({ connection, channel }) }
sendMessage(exchange, key, message) { return this.handler .then(connection => connection.createChannel()) .then(channel => channel.assertExchange(exchange, 'topic', { durable: true }) .then(() => channel.publish(exchange, key, new Buffer(JSON.stringify(message)))) .then(() => logger.debug('Sent message :', message)) .then(() => channel.close()) ) .catch(logger.error); }
// Me conecto al server. amqp.connect('amqp://localhost', (err, conn)=>{ if (err) console.log('error al conectar',err); else conn.createChannel(createChannel); });
sendMessage(exchange, key, message) { return this.handler .then(connection => connection.createChannel()) .then(channel => channel.assertExchange(exchange, 'topic', { durable: true }) .then(() => channel.publish(exchange, key, new Buffer(JSON.stringify(message)))) .then(() => logger.debug('Sent message :', message)) .then(() => channel.close()) ) .catch(logger.error); }