UNPKG

@midwayjs/rabbitmq

Version:

Midway Framework for rabbitmq

98 lines 4.29 kB
"use strict"; var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) { var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d; if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc); else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r; return c > 3 && r && Object.defineProperty(target, key, r), r; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.MidwayRabbitMQFramework = void 0; const core_1 = require("@midwayjs/core"); const mq_1 = require("./mq"); let MidwayRabbitMQFramework = class MidwayRabbitMQFramework extends core_1.BaseFramework { constructor() { super(...arguments); this.consumerHandlerList = []; } configure() { return this.configService.getConfiguration('rabbitmq'); } async applicationInitialize(options) { // Create a connection manager this.app = new mq_1.RabbitMQServer({ logger: this.logger, ...this.configurationOptions, }); } async run() { try { // init connection await this.app.connect(this.configurationOptions.url, this.configurationOptions.socketOptions); await this.loadSubscriber(); this.logger.info('Rabbitmq server start success'); } catch (error) { this.app.close(); throw error; } } async beforeStop() { await this.app.close(); } getFrameworkType() { return core_1.MidwayFrameworkType.MS_RABBITMQ; } async loadSubscriber() { // create channel const subscriberModules = (0, core_1.listModule)(core_1.MS_CONSUMER_KEY, module => { const metadata = (0, core_1.getClassMetadata)(core_1.MS_CONSUMER_KEY, module); return metadata.type === core_1.MSListenerType.RABBITMQ; }); for (const module of subscriberModules) { const data = (0, core_1.listPropertyDataFromClass)(core_1.MS_CONSUMER_KEY, module); for (const methodBindListeners of data) { // 循环绑定的方法和监听的配置信息 for (const listenerOptions of methodBindListeners) { await this.app.createConsumer(listenerOptions, async (data, channel, channelWrapper) => { const ctx = { data, channel, queueName: listenerOptions.queueName, ack: data => { return channelWrapper.ack(data); }, }; this.app.createAnonymousContext(ctx); const isPassed = await this.app .getFramework() .runGuard(ctx, module, listenerOptions.propertyKey); if (!isPassed) { throw new core_1.MidwayInvokeForbiddenError(listenerOptions.propertyKey, module); } const ins = await ctx.requestContext.getAsync(module); const fn = await this.applyMiddleware(async (ctx) => { return await ins[listenerOptions.propertyKey].call(ins, data); }); try { const result = await fn(ctx); if (result) { return channelWrapper.ack(data); } } catch (error) { this.logger.error(error); } }); } } } } getFrameworkName() { return 'midway:rabbitmq'; } }; MidwayRabbitMQFramework = __decorate([ (0, core_1.Framework)() ], MidwayRabbitMQFramework); exports.MidwayRabbitMQFramework = MidwayRabbitMQFramework; //# sourceMappingURL=framework.js.map