jest.fn(() => new rhea.Connection())
/** * Disconnect from an AMQP 1.0 server * Close every receiver on the connections and the close the connection * @memberof Amqp10Transporter */ disconnect() { if (this.connection) { return this.broker.Promise.all(this.receivers.map(receiver => receiver.close())) .then(() => this.connection.close()) .then(() => { this.connection = null; this.connected = false; this.session = null; this.receivers = []; }) .catch(error => this.logger.error(error)); } }
/** * Subscribe to balanced event command * For EVENTB command types * These queues will be used when the "disableBalancer" set to true * * @param {String} event * @param {String} group * @memberof Amqp10Transporter */ subscribeBalancedEvent(event, group) { const queue = `${this.prefix}.${PACKET_EVENT}B.${group}.${event}`; const receiverOptions = Object.assign( { source: { address: queue }, autoaccept: false, session: this.session }, this._getQueueOptions(PACKET_EVENT + "LB", true) ); return this.connection.createReceiver(receiverOptions).then(receiver => { receiver.on("message", this._consumeCB(PACKET_EVENT, true)); this.receivers.push(receiver); }); }
}; return this.connection .createAwaitableSender(awaitableSenderOptions) .then(sender => { return sender .send(message) .catch(this.logger.error) .then(() => sender); }) .then(sender => { return sender.close({ closeSession: false }); }) .catch(error => this.logger.error(error));
jest.fn(() => Promise.resolve(new rhea.Session()))
this._getQueueOptions(PACKET_REQUEST, true) ); return this.connection.createReceiver(receiverOptions).then(receiver => { if (this.opts.prefetch !== 0) { receiver.addCredit(this.opts.prefetch); receiver.on("message", context => { const cb = this._consumeCB(PACKET_REQUEST, true)(context); if (isPromise(cb) && this.opts.prefetch !== 0) { return cb.then(() => receiver.addCredit(1)); receiver.addCredit(1);
}; return this.connection .createAwaitableSender(awaitableSenderOptions) .then(sender => { return sender .send(message) .catch(this.logger.error) .then(() => sender); }) .then(sender => { return sender.close({ closeSession: false }); }) .catch(error => this.logger.error(error));
rhea.Connection.prototype.open = jest.fn(() => Promise.reject()); expect(transporter.connection).toBeNull(); expect(transporter.connected).toEqual(false); rhea.Connection.prototype.open = jest.fn(() => Promise.resolve(new rhea.Connection())); }); }); transporter.receivers = [new rhea.Receiver(), new rhea.Receiver()]; return transporter .connect()
const connection = container.createConnection(connectionOptions); connection.on("disconnected", e => { this.logger.info("AMQP10 disconnected."); this.connected = false; if (e) { this.logger.error("AMQP10 connection error.", (this.connection && this.connection.error) || ""); errorCallback && errorCallback(e); .open() .then(connection => { this.connection = connection; this.connection.createSession().then(session => { this.session = session; this.logger.info("AMQP10 is connected"); this.connection._connection.setMaxListeners(0); this.session._session.setMaxListeners(0); this.session.setMaxListeners(0); this.connected = true; return this.onConnected(); }) .catch(e => { this.logger.error("AMQP10 connection error.", (this.connection && this.connection.error) || ""); this.logger.info("AMQP10 is disconnected."); this.connected = false;
}; return this.connection .createAwaitableSender(awaitableSenderOptions) .then(sender => { return sender .send(message) .catch(this.logger.error) .then(() => sender); }) .then(sender => { return sender.close({ closeSession: false }); }) .catch(error => this.logger.error(error));
}); return this.connection.createReceiver(receiverOptions).then(receiver => { if (this.opts.prefetch !== 0) { receiver.addCredit(this.opts.prefetch); receiver.on("message", context => { const cb = this._consumeCB(cmd, needAck)(context); if (isPromise(cb) && this.opts.prefetch !== 0) { return cb.then(() => receiver.addCredit(1)); receiver.addCredit(1); session: this.session }); return this.connection.createReceiver(receiverOptions).then(receiver => { receiver.on("message", context => { this._consumeCB(cmd, false)(context); });
jest.fn(() => Promise.resolve(new rhea.AwaitableSender()))
jest.fn(() => Promise.resolve(new rhea.Connection()))
jest.fn(() => Promise.resolve(new rhea.Receiver()))
async connect() { try { const data = this.connectionData; const connection = new Connection({ ...data, reconnect: false, }); this.connection = await connection.open(); } catch (error) { this.tentatives += 1 if (this.tentatives > 3) { console.log('maximum tentatives excedeed', this.tentatives) throw error } console.log('trying to connect again, waiting a second') await QueueHandler.sleep(1000) return this.connect() } }