@midwayjs/rabbitmq
Version:
Midway Framework for rabbitmq
112 lines • 4.5 kB
JavaScript
;
/**
* This RabbitMQ Server changed from https://github.com/JeniTurtle/egg-rabbitmq-plus/blob/master/rabbitmq.ts
*/
Object.defineProperty(exports, "__esModule", { value: true });
exports.RabbitMQServer = void 0;
const amqp = require("amqp-connection-manager");
const events_1 = require("events");
class RabbitMQServer extends events_1.EventEmitter {
constructor(options = {}) {
var _a;
super();
this.channelManagerSet = new Set();
this.connection = null;
this.logger = options.logger;
this.reconnectTime = (_a = options.reconnectTime) !== null && _a !== void 0 ? _a : 10 * 1000;
this.bindError();
}
bindError() {
this.on('error', err => {
this.logger.error(err);
});
}
createChannel(isConfirmChannel = false) {
if (!isConfirmChannel) {
return this.connection.connection.createChannel();
}
else {
return this.connection.connection.createConfirmChannel();
}
}
async connect(url, socketOptions) {
this.connection = await amqp.connect(url, socketOptions);
// 监听在设置创建 channel 的错误
this.connection.on('error', err => {
if (err) {
if (err.err) {
err = err.err;
}
this.logger.error('Message Queue error', err);
}
else {
this.logger.info('Message Queue disconnected!');
}
});
await new Promise((resolve, reject) => {
// 监听 成功连接 的通知
this.connection.on('connect', () => {
this.logger.info('Message Queue connected!');
resolve();
});
// 监听 连接失败的错误 通知
this.connection.on('connectFailed', err => {
if (err) {
if (err.err) {
err = err.err;
}
this.logger.error('Message Queue disconnected', err);
}
else {
this.logger.info('Message Queue disconnected!');
}
reject(err);
});
});
}
async createConsumer(listenerOptions, listenerCallback) {
const channelWrapper = this.connection.createChannel({
setup: (channel) => {
var _a, _b, _c;
// `channel` here is a regular amqplib `ConfirmChannel`.
const channelHandlers = [];
// create queue
channelHandlers.push(channel.assertQueue(listenerOptions.queueName, Object.assign({ durable: true }, listenerOptions)));
if (listenerOptions.exchange) {
// create exchange
channelHandlers.push(channel.assertExchange(listenerOptions.exchange, (_b = (_a = listenerOptions.exchangeOptions) === null || _a === void 0 ? void 0 : _a.type) !== null && _b !== void 0 ? _b : 'topic', listenerOptions.exchangeOptions));
// bind exchange and queue
channelHandlers.push(channel.bindQueue(listenerOptions.queueName, listenerOptions.exchange, listenerOptions.routingKey || listenerOptions.pattern, listenerOptions.exchangeOptions));
}
channelHandlers.push(channel.prefetch((_c = listenerOptions.prefetch) !== null && _c !== void 0 ? _c : 1));
// listen queue
channelHandlers.push(channel.consume(listenerOptions.queueName, async (msg) => {
await listenerCallback(msg, channel, channelWrapper);
}, listenerOptions.consumeOptions));
return Promise.all(channelHandlers);
},
json: true,
});
return channelWrapper.waitForConnect();
}
async closeConnection() {
try {
if (this.connection) {
await this.connection.close();
}
this.logger.debug('Message Queue connection close success');
}
catch (err) {
this.logger.error('Message Queue connection close error', err);
}
finally {
this.connection = null;
}
}
async close() {
this.logger.debug('Message Queue will be close');
await this.closeConnection();
}
}
exports.RabbitMQServer = RabbitMQServer;
//# sourceMappingURL=mq.js.map