/** * Subscribe to balanced action commands * * @param {String} action * @memberof AmqpTransporter */ subscribeBalancedRequest(action) { const queue = `${this.prefix}.${PACKET_REQUEST}B.${action}`; return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_REQUEST, true)) .then(() => this.channel.consume( queue, this._consumeCB(PACKET_REQUEST, true), this.opts.consumeOptions )); }
/** * Send data buffer. * * @param {String} topic * @param {Buffer} data * @param {Object} meta * * @returns {Promise} */ send(topic, data, { balanced, packet }) { /* istanbul ignore next*/ if (!this.channel) return this.broker.Promise.resolve(); if (packet.target != null || balanced) { this.channel.sendToQueue(topic, data, this.opts.messageOptions); } else { this.channel.publish(topic, "", data, this.opts.messageOptions); } return this.broker.Promise.resolve(); }
/** * 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)); } }
const needAck = [PACKET_REQUEST].indexOf(cmd) !== -1; return this.channel.assertQueue(topic, this._getQueueOptions(cmd)) .then(() => this.channel.consume( topic, this._consumeCB(cmd, needAck), this.channel.assertExchange(topic, "fanout", this.opts.exchangeOptions), this.channel.assertQueue(queueName, this._getQueueOptions(cmd)), ]) .then(() => this.broker.Promise.all([ this.channel.bindQueue(...bindingArgs), this.channel.consume( queueName, this._consumeCB(cmd),
.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; this.logger.info("AMQP channel is created."); channel.prefetch(this.opts.prefetch); .on("close", () => { this.channel = null; this.logger.info("AMQP channel is closed gracefully."); }) .on("error", (err) => { .on("drain", () => { this.logger.info("AMQP channel is drained.");
_listenForResponses() { return this.channel.consume(this.replyQueue, msg => { const correlationId = msg.properties.correlationId; const handler = this.idToCallbackMap[correlationId]; if (handler) { handler(JSON.parse(msg.content.toString())); } }, {noAck: true}); }
request(queue, message, callback) { const id = uuid.v4(); this.idToCallbackMap[id] = callback; this.channel.sendToQueue(queue, new Buffer(JSON.stringify(message)), {correlationId: id, replyTo: this.replyQueue} ); }
amqp .connect('amqp://localhost') .then(conn => { connection = conn; return conn.createChannel(); }) .then(ch => { channel = ch; produce(); }) .catch(err => console.log(err)) ;
async connect () { this.connection = await amqp.connect(this.connectionString) this.channel = await this.connection.createChannel() await this.channel.assertQueue(this.queueName) return null }
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)) ; }
/** * Subscribe to balanced event command * * @param {String} event * @param {String} group * @memberof AmqpTransporter */ subscribeBalancedEvent(event, group) { const queue = `${this.prefix}.${PACKET_EVENT}B.${group}.${event}`; return this.channel.assertQueue(queue, this._getQueueOptions(PACKET_EVENT + "LB", true)) .then(() => this.channel.consume( queue, this._consumeCB(PACKET_EVENT, true), this.opts.consumeOptions )); }