UNPKG

3.28 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const index_1 = require("../index");
4const RabbitmqProducer_1 = require("./RabbitmqProducer");
5const RabbitmqConsumer_1 = require("./RabbitmqConsumer");
6const RabbitmqMgtHttpApi_1 = require("./RabbitmqMgtHttpApi");
7const RabbitmqHealthCheck_1 = require("./RabbitmqHealthCheck");
8class RabbitmqClientPlugin {
9 constructor() {
10 this.name = 'RabbitmqClientPlugin';
11 }
12 getName() {
13 return this.name;
14 }
15 willStart(app) {
16 if (!app.hasConfig('rabbitmq')) {
17 throw new Error('RabbitmqPlugin has been registered but could not find config using key "rabbitmq"');
18 }
19 const context = app.getContext();
20 const clientConf = context.getConfig('rabbitmq.client');
21 // register health check
22 const mgtAPI = new index_1.BaseSingletonDefinition(RabbitmqMgtHttpApi_1.RabbitmqMgtHttpApi);
23 context.registerSingletons(mgtAPI);
24 const rhc = new index_1.BaseSingletonDefinition(RabbitmqHealthCheck_1.RabbitmqHealthCheck, 'HC_rabbitmq');
25 rhc.constructorParamByValue('rabbitmq');
26 context.registerDefinition(rhc);
27 if (context.hasConfig('rabbitmq.producer')) {
28 const producerConfs = context.getConfig('rabbitmq.producer');
29 Object.keys(producerConfs).forEach((key) => {
30 const TypeConsumer = 'producer';
31 const name = `${key}_${TypeConsumer}`;
32 const clientSingleton = new index_1.BaseSingletonDefinition(RabbitmqProducer_1.RabbitmqProducer, name);
33 clientSingleton.constructorParamByValue(clientConf);
34 clientSingleton.constructorParamByValue(name);
35 clientSingleton.constructorParamByValue(producerConfs[key]);
36 clientSingleton.setPropertyByValue('shutdownFunction', () => context.lcStop());
37 clientSingleton.startFunction('init');
38 clientSingleton.stopFunction('close');
39 context.registerSingletons(clientSingleton);
40 });
41 }
42 if (context.hasConfig('rabbitmq.consumer')) {
43 const consumerConfs = context.getConfig('rabbitmq.consumer');
44 Object.keys(consumerConfs).forEach((key) => {
45 const clientTypeConsumer = 'consumer';
46 const name = `${key}_${clientTypeConsumer}`;
47 const consumerSingleton = new index_1.BaseSingletonDefinition(RabbitmqConsumer_1.RabbitmqConsumer, name);
48 consumerSingleton.constructorParamByValue(clientConf);
49 consumerSingleton.constructorParamByValue(name);
50 consumerSingleton.constructorParamByValue(consumerConfs[key]);
51 consumerSingleton.constructorParamByRef(consumerConfs[key]['messageHandler']);
52 consumerSingleton.setPropertyByValue('shutdownFunction', () => context.lcStop());
53 consumerSingleton.startFunction('init');
54 consumerSingleton.stopFunction('close');
55 consumerSingleton.withLazyLoading(false);
56 context.registerSingletons(consumerSingleton);
57 });
58 }
59 }
60}
61exports.default = RabbitmqClientPlugin;
62//# sourceMappingURL=RabbitmqClientPlugin.js.map
\No newline at end of file