@platform-x/hep-message-broker-client 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +1 -0
- package/dist/src/Util/commonUtil.d.ts +16 -0
- package/dist/src/Util/commonUtil.js +39 -0
- package/dist/src/Util/logger.d.ts +68 -0
- package/dist/src/Util/logger.js +195 -0
- package/dist/src/Util/requestTracer.d.ts +7 -0
- package/dist/src/Util/requestTracer.js +34 -0
- package/dist/src/config/ConfigManager.d.ts +9 -0
- package/dist/src/config/ConfigManager.js +67 -0
- package/dist/src/config/index.d.ts +29 -0
- package/dist/src/config/index.js +38 -0
- package/dist/src/index.d.ts +2 -0
- package/dist/src/index.js +6 -0
- package/dist/src/messageBroker/BaseRabbitMQClient.d.ts +16 -0
- package/dist/src/messageBroker/BaseRabbitMQClient.js +29 -0
- package/dist/src/messageBroker/ConnectionManager.d.ts +60 -0
- package/dist/src/messageBroker/ConnectionManager.js +227 -0
- package/dist/src/messageBroker/MessageBrokerClient.d.ts +34 -0
- package/dist/src/messageBroker/MessageBrokerClient.js +102 -0
- package/dist/src/messageBroker/MessageConsumer.d.ts +26 -0
- package/dist/src/messageBroker/MessageConsumer.js +102 -0
- package/dist/src/messageBroker/MessageProducer.d.ts +29 -0
- package/dist/src/messageBroker/MessageProducer.js +130 -0
- package/dist/src/messageBroker/RabbitMQClient.d.ts +12 -0
- package/dist/src/messageBroker/RabbitMQClient.js +67 -0
- package/dist/src/messageBroker/RetryManager.d.ts +23 -0
- package/dist/src/messageBroker/RetryManager.js +72 -0
- package/dist/src/messageBroker/interface/ConnectionWrapper.d.ts +6 -0
- package/dist/src/messageBroker/interface/ConnectionWrapper.js +3 -0
- package/dist/src/messageBroker/interface/IMessageBrokerClient.d.ts +7 -0
- package/dist/src/messageBroker/interface/IMessageBrokerClient.js +3 -0
- package/dist/src/messageBroker/rabbitmq/MessageBrokerClient.d.ts +114 -0
- package/dist/src/messageBroker/rabbitmq/MessageBrokerClient.js +706 -0
- package/dist/src/messageBroker/types/ActionType.d.ts +1 -0
- package/dist/src/messageBroker/types/ActionType.js +3 -0
- package/dist/src/messageBroker/types/PublishMessageInputType.d.ts +7 -0
- package/dist/src/messageBroker/types/PublishMessageInputType.js +3 -0
- package/dist/src/models/MessageModel.d.ts +5 -0
- package/dist/src/models/MessageModel.js +15 -0
- package/dist/src/models/NotificationMessageModel.d.ts +0 -0
- package/dist/src/models/NotificationMessageModel.js +2 -0
- package/package.json +40 -0
|
@@ -0,0 +1,227 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
36
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
37
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
38
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
39
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
40
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
41
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
42
|
+
});
|
|
43
|
+
};
|
|
44
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
45
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
46
|
+
};
|
|
47
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
48
|
+
// Import the amqplib library for RabbitMQ
|
|
49
|
+
const amqp = __importStar(require("amqplib"));
|
|
50
|
+
const logger_1 = require("../Util/logger");
|
|
51
|
+
const ConfigManager_1 = __importDefault(require("../config/ConfigManager"));
|
|
52
|
+
const config_1 = __importDefault(require("../config"));
|
|
53
|
+
let isConnectionOpen = false;
|
|
54
|
+
let MAX_RETRIES = 1;
|
|
55
|
+
class ConnectionManager {
|
|
56
|
+
constructor() {
|
|
57
|
+
this.configData = {};
|
|
58
|
+
this.configManager = ConfigManager_1.default.getInstance();
|
|
59
|
+
this.init();
|
|
60
|
+
}
|
|
61
|
+
/**
|
|
62
|
+
* Function for get channel
|
|
63
|
+
*/
|
|
64
|
+
getChannel() {
|
|
65
|
+
return this.channel;
|
|
66
|
+
}
|
|
67
|
+
/**
|
|
68
|
+
* Function for get channel
|
|
69
|
+
*/
|
|
70
|
+
getConnection() {
|
|
71
|
+
return this.connection;
|
|
72
|
+
}
|
|
73
|
+
/**
|
|
74
|
+
* Function for create and return intance
|
|
75
|
+
* @returns
|
|
76
|
+
*/
|
|
77
|
+
static getInstance() {
|
|
78
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
79
|
+
if (!ConnectionManager.instance) {
|
|
80
|
+
ConnectionManager.instance = new ConnectionManager();
|
|
81
|
+
}
|
|
82
|
+
return ConnectionManager.instance;
|
|
83
|
+
});
|
|
84
|
+
}
|
|
85
|
+
/**
|
|
86
|
+
* Function to connect create to RabbitMQ
|
|
87
|
+
*/
|
|
88
|
+
createConnection() {
|
|
89
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
90
|
+
var _a, _b, _c, _d, _e;
|
|
91
|
+
try {
|
|
92
|
+
logger_1.Logger.info('Reached to createConnection', 'createConnection');
|
|
93
|
+
this.configManager.loadConfig((_a = config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.RABBITMQ) === null || _a === void 0 ? void 0 : _a.CONFIG_PATH);
|
|
94
|
+
let url = `amqp://${(_b = this.configData) === null || _b === void 0 ? void 0 : _b.username}:${(_c = this.configData) === null || _c === void 0 ? void 0 : _c.password}@${(_d = this.configData) === null || _d === void 0 ? void 0 : _d.host}`;
|
|
95
|
+
// Create a connection to RabbitMQ server
|
|
96
|
+
this.connection = yield amqp.connect(url, { heartbeat: (_e = this.configData) === null || _e === void 0 ? void 0 : _e.heartbeat });
|
|
97
|
+
isConnectionOpen = true;
|
|
98
|
+
// check the connection status
|
|
99
|
+
this.bindConnectionEventHandler();
|
|
100
|
+
// create channel
|
|
101
|
+
this.channel = yield this.connection.createConfirmChannel();
|
|
102
|
+
logger_1.Logger.info('RabbitMQ connected successfully! ', 'createConnection');
|
|
103
|
+
// create exchange and queue and bind with exchange
|
|
104
|
+
yield this.setupQueuesAndExchanges();
|
|
105
|
+
const connectionWrapper = { connection: this.connection, channel: this.channel };
|
|
106
|
+
return connectionWrapper;
|
|
107
|
+
}
|
|
108
|
+
catch (error) {
|
|
109
|
+
// Log and rethrow any errors that occur during connection
|
|
110
|
+
logger_1.Logger.error('Failed to connect to RabbitMQ', 'connection', error === null || error === void 0 ? void 0 : error.message);
|
|
111
|
+
return { connection: null, channel: null };
|
|
112
|
+
}
|
|
113
|
+
});
|
|
114
|
+
}
|
|
115
|
+
/**
|
|
116
|
+
* Function for bind connection event handler on close and error setupQueuesAndExchanges
|
|
117
|
+
* @returns
|
|
118
|
+
*/
|
|
119
|
+
bindConnectionEventHandler() {
|
|
120
|
+
var _a, _b;
|
|
121
|
+
try {
|
|
122
|
+
(_a = this.connection) === null || _a === void 0 ? void 0 : _a.on('close', (err) => {
|
|
123
|
+
logger_1.Logger.error(`RabbitMQ: bindConnectionEventHandler:close`, `Connection closed`, err);
|
|
124
|
+
isConnectionOpen = false;
|
|
125
|
+
});
|
|
126
|
+
(_b = this.connection) === null || _b === void 0 ? void 0 : _b.on('error', (err) => {
|
|
127
|
+
logger_1.Logger.error(`RabbitMQ: bindConnectionEventHandler:error`, `Connection error`, err);
|
|
128
|
+
isConnectionOpen = false;
|
|
129
|
+
});
|
|
130
|
+
return isConnectionOpen;
|
|
131
|
+
}
|
|
132
|
+
catch (error) {
|
|
133
|
+
isConnectionOpen = false;
|
|
134
|
+
return isConnectionOpen;
|
|
135
|
+
}
|
|
136
|
+
}
|
|
137
|
+
/**
|
|
138
|
+
* Function to check the status of the connection
|
|
139
|
+
* @returns boolean
|
|
140
|
+
*/
|
|
141
|
+
isConnected() {
|
|
142
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
143
|
+
return isConnectionOpen;
|
|
144
|
+
});
|
|
145
|
+
}
|
|
146
|
+
;
|
|
147
|
+
/**
|
|
148
|
+
* Function to initialize the connection, channel and setup the queues and exchanges
|
|
149
|
+
* @param reqData
|
|
150
|
+
* @returns
|
|
151
|
+
*/
|
|
152
|
+
initialize() {
|
|
153
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
154
|
+
var _a;
|
|
155
|
+
logger_1.Logger.info('Reached to initialize', 'initialize');
|
|
156
|
+
let retries = 0;
|
|
157
|
+
MAX_RETRIES = (_a = this.configData) === null || _a === void 0 ? void 0 : _a.max_retries;
|
|
158
|
+
// Retrie times connection
|
|
159
|
+
while (retries < MAX_RETRIES) {
|
|
160
|
+
try {
|
|
161
|
+
yield this.createConnection();
|
|
162
|
+
return { connection: this.connection, channel: this.channel };
|
|
163
|
+
}
|
|
164
|
+
catch (err) {
|
|
165
|
+
retries++;
|
|
166
|
+
logger_1.Logger.error('initialize:Failed to connect to RabbitMQ', `Attempt ${retries} of ${MAX_RETRIES}.`);
|
|
167
|
+
if (retries >= MAX_RETRIES) {
|
|
168
|
+
logger_1.Logger.error('Max retries reached, cannot connect to RabbitMQ.', 'initialize');
|
|
169
|
+
return { connection: this.connection, channel: this.channel };
|
|
170
|
+
}
|
|
171
|
+
// Wait before retrying (e.g., 10 seconds)
|
|
172
|
+
yield new Promise(resolve => setTimeout(resolve, 10000));
|
|
173
|
+
}
|
|
174
|
+
}
|
|
175
|
+
});
|
|
176
|
+
}
|
|
177
|
+
;
|
|
178
|
+
/**
|
|
179
|
+
* Function to setup the queues and exchanges
|
|
180
|
+
* @params array
|
|
181
|
+
* @rtrun
|
|
182
|
+
*/
|
|
183
|
+
setupQueuesAndExchanges() {
|
|
184
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
185
|
+
var _a, _b, _c;
|
|
186
|
+
logger_1.Logger.info('Reached to setupQueuesAndExchanges', 'setupQueuesAndExchanges');
|
|
187
|
+
try {
|
|
188
|
+
let exchanges = this.configData.exchanges;
|
|
189
|
+
let queues = this.configData.queues;
|
|
190
|
+
let bindings = this.configData.bindings;
|
|
191
|
+
// Declare the exchange with dlx
|
|
192
|
+
for (let data of exchanges) {
|
|
193
|
+
yield ((_a = this.channel) === null || _a === void 0 ? void 0 : _a.assertExchange(data === null || data === void 0 ? void 0 : data.name, data.type, { durable: data.durable, autoDelete: data.autoDelete }));
|
|
194
|
+
logger_1.Logger.info(`${data === null || data === void 0 ? void 0 : data.name} exchange created`, 'setupQueuesAndExchanges');
|
|
195
|
+
}
|
|
196
|
+
// Declare the queues with dlx
|
|
197
|
+
for (let data of queues) {
|
|
198
|
+
yield ((_b = this.channel) === null || _b === void 0 ? void 0 : _b.assertQueue(data === null || data === void 0 ? void 0 : data.name, { durable: data.durable, exclusive: data.exclusive, autoDelete: data.autoDelete, arguments: data.arguments }));
|
|
199
|
+
logger_1.Logger.info(`${data === null || data === void 0 ? void 0 : data.name} queue created `, 'setupQueuesAndExchanges');
|
|
200
|
+
}
|
|
201
|
+
// Declare the bindings for queues and exchanges with routing keys
|
|
202
|
+
for (let obj in bindings) {
|
|
203
|
+
(_c = bindings[obj]) === null || _c === void 0 ? void 0 : _c.forEach((key) => __awaiter(this, void 0, void 0, function* () {
|
|
204
|
+
var _a;
|
|
205
|
+
yield ((_a = this.channel) === null || _a === void 0 ? void 0 : _a.bindQueue(key === null || key === void 0 ? void 0 : key.queue, obj, key === null || key === void 0 ? void 0 : key.routingKey));
|
|
206
|
+
logger_1.Logger.info(`${key === null || key === void 0 ? void 0 : key.queue} bind with exchange name ${obj} and routing key ${key === null || key === void 0 ? void 0 : key.routingKey}`, 'setupQueuesAndExchanges');
|
|
207
|
+
}));
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
logger_1.Logger.error("Error on setup queues, exchanges and binding", 'setupQueuesAndExchanges', error.message);
|
|
212
|
+
throw error;
|
|
213
|
+
}
|
|
214
|
+
});
|
|
215
|
+
}
|
|
216
|
+
;
|
|
217
|
+
/**
|
|
218
|
+
* Function for load the config data
|
|
219
|
+
*/
|
|
220
|
+
init() {
|
|
221
|
+
var _a;
|
|
222
|
+
this.configManager.loadConfig((_a = config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.RABBITMQ) === null || _a === void 0 ? void 0 : _a.CONFIG_PATH);
|
|
223
|
+
this.configData = this.configManager.getConfig();
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
exports.default = ConnectionManager;
|
|
227
|
+
//# sourceMappingURL=ConnectionManager.js.map
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import { IMessageBrokerClient } from './interface/IMessageBrokerClient';
|
|
2
|
+
import { PublishMessageInputType } from './types/PublishMessageInputType';
|
|
3
|
+
export declare class MessageBrokerClient implements IMessageBrokerClient {
|
|
4
|
+
private readonly rabbitMQClient;
|
|
5
|
+
private readonly messageProducer;
|
|
6
|
+
private readonly configManager;
|
|
7
|
+
private readonly isEnabledRabbitMQ;
|
|
8
|
+
private readonly configData;
|
|
9
|
+
constructor();
|
|
10
|
+
/**
|
|
11
|
+
* Function to initialize the RabbitMQ connection
|
|
12
|
+
*/
|
|
13
|
+
initialize(): Promise<void>;
|
|
14
|
+
/**
|
|
15
|
+
* Function to close the connection and channel
|
|
16
|
+
*/
|
|
17
|
+
disconnect(): void;
|
|
18
|
+
/**
|
|
19
|
+
* Function to publish a message with retry
|
|
20
|
+
* @param request
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
publishMessage(request: PublishMessageInputType): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* Function to publish a message with retry to exchange
|
|
26
|
+
* @param request
|
|
27
|
+
* @returns
|
|
28
|
+
*/
|
|
29
|
+
publishMessageToExchange(request: PublishMessageInputType): Promise<boolean>;
|
|
30
|
+
/***
|
|
31
|
+
* Function to check if the connection is established
|
|
32
|
+
*/
|
|
33
|
+
isConnected(): Promise<boolean>;
|
|
34
|
+
}
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
exports.MessageBrokerClient = void 0;
|
|
16
|
+
const config_1 = __importDefault(require("../config"));
|
|
17
|
+
const ConfigManager_1 = __importDefault(require("../config/ConfigManager"));
|
|
18
|
+
const ConnectionManager_1 = __importDefault(require("./ConnectionManager"));
|
|
19
|
+
const MessageProducer_1 = __importDefault(require("./MessageProducer"));
|
|
20
|
+
const RabbitMQClient_1 = require("./RabbitMQClient");
|
|
21
|
+
const RetryManager_1 = __importDefault(require("./RetryManager"));
|
|
22
|
+
const connectionManager = ConnectionManager_1.default.getInstance();
|
|
23
|
+
class MessageBrokerClient {
|
|
24
|
+
constructor() {
|
|
25
|
+
var _a;
|
|
26
|
+
// private readonly retryManager: RetryManager<T>;
|
|
27
|
+
this.isEnabledRabbitMQ = false;
|
|
28
|
+
this.configData = {};
|
|
29
|
+
this.rabbitMQClient = new RabbitMQClient_1.RabbitMQClient();
|
|
30
|
+
this.messageProducer = new MessageProducer_1.default();
|
|
31
|
+
this.isEnabledRabbitMQ = ((_a = config_1.default === null || config_1.default === void 0 ? void 0 : config_1.default.RABBITMQ) === null || _a === void 0 ? void 0 : _a.IS_ENABLED) === 'true';
|
|
32
|
+
this.configManager = ConfigManager_1.default.getInstance();
|
|
33
|
+
this.configData = this.configManager.getConfig();
|
|
34
|
+
}
|
|
35
|
+
/**
|
|
36
|
+
* Function to initialize the RabbitMQ connection
|
|
37
|
+
*/
|
|
38
|
+
initialize() {
|
|
39
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
40
|
+
if (this.isEnabledRabbitMQ) {
|
|
41
|
+
this.rabbitMQClient.initialize();
|
|
42
|
+
}
|
|
43
|
+
});
|
|
44
|
+
}
|
|
45
|
+
/**
|
|
46
|
+
* Function to close the connection and channel
|
|
47
|
+
*/
|
|
48
|
+
disconnect() {
|
|
49
|
+
if (this.isEnabledRabbitMQ) {
|
|
50
|
+
this.rabbitMQClient.disconnect();
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
/**
|
|
54
|
+
* Function to publish a message with retry
|
|
55
|
+
* @param request
|
|
56
|
+
* @returns
|
|
57
|
+
*/
|
|
58
|
+
publishMessage(request) {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
if (this.isEnabledRabbitMQ) {
|
|
61
|
+
const boundPublishMessage = this.messageProducer.publishMessage.bind(this);
|
|
62
|
+
const boundConsumerHandler = this.messageProducer.messageHandler.bind(this);
|
|
63
|
+
let retry_delay = (_a = this.configData) === null || _a === void 0 ? void 0 : _a.retry_delay;
|
|
64
|
+
let max_retries = (_b = this.configData) === null || _b === void 0 ? void 0 : _b.max_retries;
|
|
65
|
+
// Instantiate RetryManager
|
|
66
|
+
const retryManager = RetryManager_1.default.getInstance(max_retries, retry_delay);
|
|
67
|
+
retryManager.performActionWithRetry(boundPublishMessage, request, boundConsumerHandler);
|
|
68
|
+
}
|
|
69
|
+
return Promise.resolve(false);
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Function to publish a message with retry to exchange
|
|
73
|
+
* @param request
|
|
74
|
+
* @returns
|
|
75
|
+
*/
|
|
76
|
+
publishMessageToExchange(request) {
|
|
77
|
+
var _a, _b;
|
|
78
|
+
if (this.isEnabledRabbitMQ) {
|
|
79
|
+
const boundPublishMessage = this.messageProducer.publishMessageToExchange.bind(this);
|
|
80
|
+
const boundConsumerHandler = this.messageProducer.messageHandler.bind(this);
|
|
81
|
+
let retry_delay = (_a = this.configData) === null || _a === void 0 ? void 0 : _a.retry_delay;
|
|
82
|
+
let max_retries = (_b = this.configData) === null || _b === void 0 ? void 0 : _b.max_retries;
|
|
83
|
+
// Instantiate RetryManager
|
|
84
|
+
const retryManager = RetryManager_1.default.getInstance(max_retries, retry_delay);
|
|
85
|
+
retryManager.performActionWithRetry(boundPublishMessage, request, boundConsumerHandler);
|
|
86
|
+
}
|
|
87
|
+
return Promise.resolve(false);
|
|
88
|
+
}
|
|
89
|
+
/***
|
|
90
|
+
* Function to check if the connection is established
|
|
91
|
+
*/
|
|
92
|
+
isConnected() {
|
|
93
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
94
|
+
if (this.isEnabledRabbitMQ) {
|
|
95
|
+
return (yield connectionManager) ? (yield connectionManager).isConnected() : Promise.resolve(false);
|
|
96
|
+
}
|
|
97
|
+
return Promise.resolve(false);
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.MessageBrokerClient = MessageBrokerClient;
|
|
102
|
+
//# sourceMappingURL=MessageBrokerClient.js.map
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import BaseRabbitMQClient from "./BaseRabbitMQClient";
|
|
2
|
+
declare class MessageConsumer extends BaseRabbitMQClient {
|
|
3
|
+
private readonly messageProducer;
|
|
4
|
+
constructor();
|
|
5
|
+
/**
|
|
6
|
+
* Funvction for initialize consumer queue
|
|
7
|
+
* @param consumerQueues
|
|
8
|
+
*/
|
|
9
|
+
initializeConsumers(): Promise<{
|
|
10
|
+
error: string;
|
|
11
|
+
} | undefined>;
|
|
12
|
+
/**
|
|
13
|
+
* Function to consume message with retry
|
|
14
|
+
* @param queueName
|
|
15
|
+
*/
|
|
16
|
+
private consumerMessage;
|
|
17
|
+
/**
|
|
18
|
+
* Function for consumer massage handler
|
|
19
|
+
* @param channel
|
|
20
|
+
* @param queue
|
|
21
|
+
* @param message
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
private consumerHandler;
|
|
25
|
+
}
|
|
26
|
+
export default MessageConsumer;
|
|
@@ -0,0 +1,102 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const logger_1 = require("../Util/logger");
|
|
16
|
+
const RetryManager_1 = __importDefault(require("./RetryManager"));
|
|
17
|
+
const MessageProducer_1 = __importDefault(require("./MessageProducer"));
|
|
18
|
+
const BaseRabbitMQClient_1 = __importDefault(require("./BaseRabbitMQClient"));
|
|
19
|
+
let configData = {};
|
|
20
|
+
let max_retries = 0; // Maximum retries for connection and queue
|
|
21
|
+
let retry_delay = ""; // Back-off time for message retry
|
|
22
|
+
class MessageConsumer extends BaseRabbitMQClient_1.default {
|
|
23
|
+
constructor() {
|
|
24
|
+
super();
|
|
25
|
+
this.messageProducer = new MessageProducer_1.default();
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Funvction for initialize consumer queue
|
|
29
|
+
* @param consumerQueues
|
|
30
|
+
*/
|
|
31
|
+
initializeConsumers() {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
try {
|
|
34
|
+
logger_1.Logger.info('Reached in initialize consumer', `initializeConsumers`);
|
|
35
|
+
let consume_queues = configData === null || configData === void 0 ? void 0 : configData.consume_queues;
|
|
36
|
+
// Consume messages from all queues
|
|
37
|
+
for (const queue in configData === null || configData === void 0 ? void 0 : configData.consume_queues) {
|
|
38
|
+
if (Object.prototype.hasOwnProperty.call(consume_queues, queue)) {
|
|
39
|
+
// Call function Consume messages with retry
|
|
40
|
+
yield this.consumerMessage(queue);
|
|
41
|
+
}
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
logger_1.Logger.error('Error initializing consumers:', 'initializeConsumers', error.message);
|
|
46
|
+
return { error: error.message };
|
|
47
|
+
}
|
|
48
|
+
});
|
|
49
|
+
}
|
|
50
|
+
/**
|
|
51
|
+
* Function to consume message with retry
|
|
52
|
+
* @param queueName
|
|
53
|
+
*/
|
|
54
|
+
consumerMessage(queueName) {
|
|
55
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
56
|
+
var _a;
|
|
57
|
+
logger_1.Logger.info("Reached: Consuming messages from the main queue", 'consumerMessage');
|
|
58
|
+
yield ((_a = this.channel) === null || _a === void 0 ? void 0 : _a.consume(queueName, (message) => __awaiter(this, void 0, void 0, function* () {
|
|
59
|
+
var _a, _b;
|
|
60
|
+
if (message) {
|
|
61
|
+
let msgData = message.content.toString();
|
|
62
|
+
const success = yield this.consumerHandler({ queueName, message: msgData });
|
|
63
|
+
try {
|
|
64
|
+
if (!success) {
|
|
65
|
+
const boundPublishMessage = this.messageProducer.publishMessage.bind(this);
|
|
66
|
+
const boundConsumerHandler = this.consumerHandler.bind(this);
|
|
67
|
+
retry_delay = configData === null || configData === void 0 ? void 0 : configData.retry_delay;
|
|
68
|
+
const reqData = { queueName, message };
|
|
69
|
+
// Instantiate RetryManager
|
|
70
|
+
const retryManager = RetryManager_1.default.getInstance(max_retries, retry_delay);
|
|
71
|
+
retryManager.performActionWithRetry(boundPublishMessage, reqData, boundConsumerHandler);
|
|
72
|
+
}
|
|
73
|
+
else {
|
|
74
|
+
logger_1.Logger.info(`Acknowledge the successful processing`, 'consumerMessage');
|
|
75
|
+
(_a = this.channel) === null || _a === void 0 ? void 0 : _a.ack(message); // Acknowledge the successful processing
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (error) {
|
|
79
|
+
logger_1.Logger.error(`Error processing message`, 'consumerMessage', { message: error.message });
|
|
80
|
+
(_b = this.channel) === null || _b === void 0 ? void 0 : _b.nack(message, false, false); // Move message to DLQ after failure
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
})));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
;
|
|
87
|
+
/**
|
|
88
|
+
* Function for consumer massage handler
|
|
89
|
+
* @param channel
|
|
90
|
+
* @param queue
|
|
91
|
+
* @param message
|
|
92
|
+
* @returns
|
|
93
|
+
*/
|
|
94
|
+
consumerHandler(request) {
|
|
95
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
96
|
+
logger_1.Logger.info(`${JSON.stringify(request)}`, 'consumerHandler');
|
|
97
|
+
return true;
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
}
|
|
101
|
+
exports.default = MessageConsumer;
|
|
102
|
+
//# sourceMappingURL=MessageConsumer.js.map
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
import BaseRabbitMQClient from "./BaseRabbitMQClient";
|
|
2
|
+
import ConnectionManager from "./ConnectionManager";
|
|
3
|
+
import { PublishMessageInputType } from "./types/PublishMessageInputType";
|
|
4
|
+
declare class MessageProducer extends BaseRabbitMQClient {
|
|
5
|
+
private readonly correlationId;
|
|
6
|
+
connectionManager: ConnectionManager;
|
|
7
|
+
constructor();
|
|
8
|
+
/**
|
|
9
|
+
* Function to publish a message with retry
|
|
10
|
+
* @param queueName
|
|
11
|
+
* @param message
|
|
12
|
+
* @returns
|
|
13
|
+
*/
|
|
14
|
+
publishMessage(request: PublishMessageInputType): Promise<boolean>;
|
|
15
|
+
/**
|
|
16
|
+
* Function to publish a message with retry
|
|
17
|
+
* @param exchangeName
|
|
18
|
+
* @param routingKey
|
|
19
|
+
* @param message
|
|
20
|
+
* @param options
|
|
21
|
+
* @returns
|
|
22
|
+
*/
|
|
23
|
+
publishMessageToExchange(request: PublishMessageInputType): Promise<boolean>;
|
|
24
|
+
/**
|
|
25
|
+
* Funciton to message handler
|
|
26
|
+
*/
|
|
27
|
+
messageHandler(request: PublishMessageInputType): Promise<boolean>;
|
|
28
|
+
}
|
|
29
|
+
export default MessageProducer;
|
|
@@ -0,0 +1,130 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
14
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
15
|
+
const commonUtil_1 = require("../Util/commonUtil");
|
|
16
|
+
const logger_1 = require("../Util/logger");
|
|
17
|
+
const BaseRabbitMQClient_1 = __importDefault(require("./BaseRabbitMQClient"));
|
|
18
|
+
const ConnectionManager_1 = __importDefault(require("./ConnectionManager"));
|
|
19
|
+
class MessageProducer extends BaseRabbitMQClient_1.default {
|
|
20
|
+
constructor() {
|
|
21
|
+
super();
|
|
22
|
+
this.correlationId = (0, commonUtil_1.generateCorrelationId)();
|
|
23
|
+
this.connectionManager = new ConnectionManager_1.default();
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Function to publish a message with retry
|
|
27
|
+
* @param queueName
|
|
28
|
+
* @param message
|
|
29
|
+
* @returns
|
|
30
|
+
*/
|
|
31
|
+
publishMessage(request) {
|
|
32
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
33
|
+
const queueName = request.queueName;
|
|
34
|
+
const data = request.message;
|
|
35
|
+
console.log(data.message, queueName, 'message');
|
|
36
|
+
logger_1.Logger.info('Reached:publishMessage', 'publishMessage');
|
|
37
|
+
const messageBuffer = Buffer.from(JSON.stringify(data === null || data === void 0 ? void 0 : data.message));
|
|
38
|
+
this.channel = this.connectionManager.getChannel();
|
|
39
|
+
try {
|
|
40
|
+
const isSent = yield new Promise((resolve, reject) => {
|
|
41
|
+
var _a, _b;
|
|
42
|
+
(_a = this.connection) === null || _a === void 0 ? void 0 : _a.on('close', (err) => {
|
|
43
|
+
logger_1.Logger.error(`RabbitMQ: bindConnectionEventHandler:close`, `Connection closed`, err);
|
|
44
|
+
});
|
|
45
|
+
let sent = (_b = this.channel) === null || _b === void 0 ? void 0 : _b.sendToQueue('update_metadata_queue', Buffer.from(JSON.stringify('Hello world test')), {
|
|
46
|
+
persistent: true, // Ensures the message is persisted
|
|
47
|
+
// correlationId: data?.message?.correlationId,
|
|
48
|
+
});
|
|
49
|
+
console.log("publish message successfully");
|
|
50
|
+
// console.log(sent, 'send')
|
|
51
|
+
if (sent) {
|
|
52
|
+
logger_1.Logger.debug(`Message sent successfully`, 'publishMessage', { data, queueName });
|
|
53
|
+
resolve(true);
|
|
54
|
+
}
|
|
55
|
+
else {
|
|
56
|
+
logger_1.Logger.error(`Failed to publish message.`, 'publishMessage', { message: messageBuffer.toString() });
|
|
57
|
+
reject(false);
|
|
58
|
+
}
|
|
59
|
+
});
|
|
60
|
+
console.log(isSent);
|
|
61
|
+
return true;
|
|
62
|
+
;
|
|
63
|
+
}
|
|
64
|
+
catch (error) {
|
|
65
|
+
console.log(error);
|
|
66
|
+
logger_1.Logger.error(`Error sending message: ${error}`, 'sendMessage', error);
|
|
67
|
+
return false;
|
|
68
|
+
}
|
|
69
|
+
});
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Function to publish a message with retry
|
|
73
|
+
* @param exchangeName
|
|
74
|
+
* @param routingKey
|
|
75
|
+
* @param message
|
|
76
|
+
* @param options
|
|
77
|
+
* @returns
|
|
78
|
+
*/
|
|
79
|
+
publishMessageToExchange(request) {
|
|
80
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
81
|
+
var _a;
|
|
82
|
+
const exchangeName = (_a = request === null || request === void 0 ? void 0 : request.exchangeName) !== null && _a !== void 0 ? _a : '';
|
|
83
|
+
const queueName = request.queueName;
|
|
84
|
+
const data = request === null || request === void 0 ? void 0 : request.message;
|
|
85
|
+
// const options:object | null = request?.options; // Optional parameter for retry count if you wnats to override the default retry count
|
|
86
|
+
// const messageObj = {
|
|
87
|
+
// message,
|
|
88
|
+
// correlationId:this.correlationId,
|
|
89
|
+
// }
|
|
90
|
+
logger_1.Logger.info('Reached:publishMessageToExchange', 'publishMessageToExchange');
|
|
91
|
+
// Publish the message
|
|
92
|
+
const messageBuffer = Buffer.from(JSON.stringify(data.message));
|
|
93
|
+
try {
|
|
94
|
+
const isSent = yield new Promise((resolve, reject) => {
|
|
95
|
+
var _a;
|
|
96
|
+
// this.channel?.on
|
|
97
|
+
const sent = (_a = this.channel) === null || _a === void 0 ? void 0 : _a.publish(exchangeName, queueName, messageBuffer, {
|
|
98
|
+
persistent: true, // Ensures the message is persisted
|
|
99
|
+
correlationId: this.correlationId,
|
|
100
|
+
});
|
|
101
|
+
if (sent) {
|
|
102
|
+
logger_1.Logger.debug(`Message sent successfully`, 'publishMessageToExchange', { data, queueName });
|
|
103
|
+
resolve(true);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
logger_1.Logger.error(`Failed to publish message to exchange.`, 'publishMessageToExchange', { message: messageBuffer.toString() });
|
|
107
|
+
reject(false);
|
|
108
|
+
}
|
|
109
|
+
});
|
|
110
|
+
return isSent;
|
|
111
|
+
}
|
|
112
|
+
catch (error) {
|
|
113
|
+
logger_1.Logger.error(`Error sending message: ${error}`, 'sendMessage');
|
|
114
|
+
return false;
|
|
115
|
+
}
|
|
116
|
+
});
|
|
117
|
+
}
|
|
118
|
+
;
|
|
119
|
+
/**
|
|
120
|
+
* Funciton to message handler
|
|
121
|
+
*/
|
|
122
|
+
messageHandler(request) {
|
|
123
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
124
|
+
logger_1.Logger.info(`${JSON.stringify(request)}`, 'messageHandler');
|
|
125
|
+
return true;
|
|
126
|
+
});
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
exports.default = MessageProducer;
|
|
130
|
+
//# sourceMappingURL=MessageProducer.js.map
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import BaseRabbitMQClient from './BaseRabbitMQClient';
|
|
2
|
+
export declare class RabbitMQClient extends BaseRabbitMQClient {
|
|
3
|
+
constructor();
|
|
4
|
+
/**
|
|
5
|
+
* Function to initialize the RabbitMQ connection
|
|
6
|
+
*/
|
|
7
|
+
initialize(): Promise<void>;
|
|
8
|
+
/**
|
|
9
|
+
* Function to close the connection and channel
|
|
10
|
+
*/
|
|
11
|
+
disconnect(): Promise<void>;
|
|
12
|
+
}
|