@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,67 @@
|
|
|
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.RabbitMQClient = void 0;
|
|
16
|
+
// Import the amqplib library for RabbitMQ
|
|
17
|
+
const logger_1 = require("../Util/logger");
|
|
18
|
+
const BaseRabbitMQClient_1 = __importDefault(require("./BaseRabbitMQClient"));
|
|
19
|
+
const ConnectionManager_1 = __importDefault(require("./ConnectionManager"));
|
|
20
|
+
class RabbitMQClient extends BaseRabbitMQClient_1.default {
|
|
21
|
+
constructor() {
|
|
22
|
+
super();
|
|
23
|
+
}
|
|
24
|
+
/**
|
|
25
|
+
* Function to initialize the RabbitMQ connection
|
|
26
|
+
*/
|
|
27
|
+
initialize() {
|
|
28
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
29
|
+
try {
|
|
30
|
+
logger_1.Logger.info('Reached to initialize', 'initialize');
|
|
31
|
+
// Create a connection to RabbitMQ server
|
|
32
|
+
const connectionManager = yield ConnectionManager_1.default.getInstance();
|
|
33
|
+
yield connectionManager.createConnection();
|
|
34
|
+
logger_1.Logger.info('RabbitMQ connection created', 'initialize');
|
|
35
|
+
}
|
|
36
|
+
catch (error) {
|
|
37
|
+
// Log and rethrow any errors that occur during initialization
|
|
38
|
+
logger_1.Logger.error('Failed to create RabbitMQ connection', 'initialize', error);
|
|
39
|
+
throw error;
|
|
40
|
+
}
|
|
41
|
+
});
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Function to close the connection and channel
|
|
45
|
+
*/
|
|
46
|
+
disconnect() {
|
|
47
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
48
|
+
var _a, _b;
|
|
49
|
+
try {
|
|
50
|
+
logger_1.Logger.info('Reached to disconnect', 'disconnect');
|
|
51
|
+
// Close the channel
|
|
52
|
+
yield ((_a = this.channel) === null || _a === void 0 ? void 0 : _a.close());
|
|
53
|
+
// Close the connection
|
|
54
|
+
yield ((_b = this.connection) === null || _b === void 0 ? void 0 : _b.close());
|
|
55
|
+
logger_1.Logger.info('RabbitMQ connection closed', 'disconnect');
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
// Log and rethrow any errors that occur during closing
|
|
59
|
+
logger_1.Logger.error('Failed to close RabbitMQ connection', 'disconnect', error);
|
|
60
|
+
throw error;
|
|
61
|
+
}
|
|
62
|
+
});
|
|
63
|
+
}
|
|
64
|
+
;
|
|
65
|
+
}
|
|
66
|
+
exports.RabbitMQClient = RabbitMQClient;
|
|
67
|
+
//# sourceMappingURL=RabbitMQClient.js.map
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
type ActionType<T> = (input: T) => void;
|
|
2
|
+
declare class RetryManager<T> {
|
|
3
|
+
private static instance;
|
|
4
|
+
private readonly max_retry;
|
|
5
|
+
private readonly retry_delay;
|
|
6
|
+
constructor(max_retry: number, retry_delay: string);
|
|
7
|
+
/**
|
|
8
|
+
* Function for create singlton class instance and return instance
|
|
9
|
+
* @param max_retry
|
|
10
|
+
* @param retry_delay
|
|
11
|
+
* @returns
|
|
12
|
+
*/
|
|
13
|
+
static getInstance<T>(max_retry: number, retry_delay: string): RetryManager<T>;
|
|
14
|
+
/**
|
|
15
|
+
* Function for perform action with retry
|
|
16
|
+
* @param actionHandler
|
|
17
|
+
* @param context
|
|
18
|
+
* @param failActionHandler
|
|
19
|
+
* @returns
|
|
20
|
+
*/
|
|
21
|
+
performActionWithRetry(actionHandler: ActionType<T>, context: T, failActionHandler: ActionType<T>): Promise<boolean>;
|
|
22
|
+
}
|
|
23
|
+
export default RetryManager;
|
|
@@ -0,0 +1,72 @@
|
|
|
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
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
12
|
+
const logger_1 = require("../Util/logger");
|
|
13
|
+
class RetryManager {
|
|
14
|
+
constructor(max_retry, retry_delay) {
|
|
15
|
+
this.max_retry = max_retry;
|
|
16
|
+
this.retry_delay = retry_delay === null || retry_delay === void 0 ? void 0 : retry_delay.split("|");
|
|
17
|
+
}
|
|
18
|
+
/**
|
|
19
|
+
* Function for create singlton class instance and return instance
|
|
20
|
+
* @param max_retry
|
|
21
|
+
* @param retry_delay
|
|
22
|
+
* @returns
|
|
23
|
+
*/
|
|
24
|
+
static getInstance(max_retry, retry_delay) {
|
|
25
|
+
if (!RetryManager.instance) {
|
|
26
|
+
RetryManager.instance = new RetryManager(max_retry, retry_delay);
|
|
27
|
+
}
|
|
28
|
+
return RetryManager.instance;
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Function for perform action with retry
|
|
32
|
+
* @param actionHandler
|
|
33
|
+
* @param context
|
|
34
|
+
* @param failActionHandler
|
|
35
|
+
* @returns
|
|
36
|
+
*/
|
|
37
|
+
performActionWithRetry(actionHandler, context, failActionHandler) {
|
|
38
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
39
|
+
logger_1.Logger.info("Reached: in perform action with retry", "performActionWithRetry");
|
|
40
|
+
let attempt = 0;
|
|
41
|
+
while (attempt < this.max_retry) {
|
|
42
|
+
try {
|
|
43
|
+
actionHandler(context);
|
|
44
|
+
return true;
|
|
45
|
+
}
|
|
46
|
+
catch (error) {
|
|
47
|
+
attempt++;
|
|
48
|
+
logger_1.Logger.error(`Failed to publish message. Attempt ${attempt} of ${this.max_retry}. Error:`, 'performActionWithRetry', { message: error.message });
|
|
49
|
+
if (attempt >= this.max_retry) {
|
|
50
|
+
logger_1.Logger.error('Max retries reached. Failed to publish message.', 'performActionWithRetry');
|
|
51
|
+
// Optionally: Log or persist the failed message to a DB, file, or other storage for manual intervention
|
|
52
|
+
failActionHandler(context);
|
|
53
|
+
return false;
|
|
54
|
+
}
|
|
55
|
+
// Wait before retrying (Exponential Backoff)
|
|
56
|
+
let retryDelay;
|
|
57
|
+
if (this.retry_delay.length - 1 === attempt) {
|
|
58
|
+
retryDelay = Number(this.retry_delay[attempt - 1]);
|
|
59
|
+
}
|
|
60
|
+
else {
|
|
61
|
+
retryDelay = Number(this.retry_delay[this.retry_delay.length - 1]);
|
|
62
|
+
}
|
|
63
|
+
logger_1.Logger.info(`Retrying in ${retryDelay / 1000} seconds...`, 'performActionWithRetry');
|
|
64
|
+
yield new Promise((resolve) => setTimeout(resolve, retryDelay));
|
|
65
|
+
}
|
|
66
|
+
}
|
|
67
|
+
return false;
|
|
68
|
+
});
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
exports.default = RetryManager;
|
|
72
|
+
//# sourceMappingURL=RetryManager.js.map
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { PublishMessageInputType } from "../types/PublishMessageInputType";
|
|
2
|
+
export interface IMessageBrokerClient {
|
|
3
|
+
initialize(configData: object | null): void;
|
|
4
|
+
disconnect(): void;
|
|
5
|
+
publishMessage(request: PublishMessageInputType): Promise<boolean>;
|
|
6
|
+
publishMessageToExchange(request: PublishMessageInputType): Promise<boolean>;
|
|
7
|
+
}
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import * as amqp from 'amqplib';
|
|
2
|
+
import ConnectionWrapper from '../interface/ConnectionWrapper';
|
|
3
|
+
import { PublishMessageInputType } from '../types/PublishMessageInputType';
|
|
4
|
+
import { ActionType } from '../types/ActionType';
|
|
5
|
+
export declare class MessageBrokerClient {
|
|
6
|
+
/**
|
|
7
|
+
* Function to check the status of the connection
|
|
8
|
+
* @returns boolean
|
|
9
|
+
*/
|
|
10
|
+
isConnected(): Promise<boolean>;
|
|
11
|
+
/**
|
|
12
|
+
* Function to initialize the connection, channel and setup the queues and exchanges
|
|
13
|
+
* @param reqData
|
|
14
|
+
* @returns
|
|
15
|
+
*/
|
|
16
|
+
initialize(): Promise<{
|
|
17
|
+
connection: amqp.Connection;
|
|
18
|
+
channel: amqp.Channel;
|
|
19
|
+
} | undefined>;
|
|
20
|
+
/**
|
|
21
|
+
* Function to publish a message with retry
|
|
22
|
+
* @param request
|
|
23
|
+
* @param classInstance
|
|
24
|
+
* @param messagePublisherErrorHandler
|
|
25
|
+
* @returns
|
|
26
|
+
*/
|
|
27
|
+
publishMessage(request: PublishMessageInputType): Promise<true | undefined>;
|
|
28
|
+
/**
|
|
29
|
+
* Function to publish a message with retry
|
|
30
|
+
* @param request
|
|
31
|
+
* @param classInstance
|
|
32
|
+
* @param messagePublisherErrorHandler
|
|
33
|
+
* @returns
|
|
34
|
+
*/
|
|
35
|
+
publishMessageToExchange(request: PublishMessageInputType): Promise<true | undefined>;
|
|
36
|
+
/**
|
|
37
|
+
* Funciton to message handler
|
|
38
|
+
*/
|
|
39
|
+
messagePublisherErrorHandler(request: PublishMessageInputType): Promise<boolean>;
|
|
40
|
+
/**
|
|
41
|
+
* Funvction for initialize consumer queue
|
|
42
|
+
* @param consumerQueues
|
|
43
|
+
*/
|
|
44
|
+
initializeConsumers(classInstance: any, consumerHandler: string, consumerErrorHandler: string): Promise<void>;
|
|
45
|
+
/**
|
|
46
|
+
* Function for perform action with retry
|
|
47
|
+
* @param actionHandler
|
|
48
|
+
* @param context
|
|
49
|
+
* @param failActionHandler
|
|
50
|
+
* @returns
|
|
51
|
+
*/
|
|
52
|
+
performActionWithRetry<T>(actionHandler: ActionType<T>, failActionHandler: ActionType<T>, context: T): Promise<boolean>;
|
|
53
|
+
/**
|
|
54
|
+
* Function to consume message with retry
|
|
55
|
+
* @param queueName
|
|
56
|
+
*/
|
|
57
|
+
consumeMessage(queueName: string, classInstance: any, consumerHandler: string, consumerErrorHandler: string): Promise<amqp.Replies.Consume | undefined>;
|
|
58
|
+
/**
|
|
59
|
+
* Function to send message to dead later queue
|
|
60
|
+
* @param attempt
|
|
61
|
+
* @param data
|
|
62
|
+
* @param queueName
|
|
63
|
+
* @retruns
|
|
64
|
+
*/
|
|
65
|
+
sendMessageToDeadLatter(attempt: number, data: any): Promise<boolean | undefined>;
|
|
66
|
+
/***
|
|
67
|
+
* Function to send message to retry queue
|
|
68
|
+
* @param attempt
|
|
69
|
+
* @param data
|
|
70
|
+
* @param retryDelay
|
|
71
|
+
* @param queueName
|
|
72
|
+
* @returns
|
|
73
|
+
*/
|
|
74
|
+
sendMessageToRetryQueue(attempt: number, data: any, retryDelay: any, queueName: string): Promise<boolean | undefined>;
|
|
75
|
+
/**
|
|
76
|
+
* Function to find the queue, exchange and add binding
|
|
77
|
+
* @param queueName
|
|
78
|
+
* @param exchangeName
|
|
79
|
+
* @returns
|
|
80
|
+
*/
|
|
81
|
+
findQueueAndExchange(queueName: string, exchangeName: string): Promise<void>;
|
|
82
|
+
/**
|
|
83
|
+
* Function to setup the queues and exchanges
|
|
84
|
+
* @params array
|
|
85
|
+
* @rtrun
|
|
86
|
+
*/
|
|
87
|
+
private setupQueuesAndExchanges;
|
|
88
|
+
/**
|
|
89
|
+
* Function to binding queue and exchange
|
|
90
|
+
* @param exchangeName
|
|
91
|
+
* @param queueName
|
|
92
|
+
* @param routingKey
|
|
93
|
+
*/
|
|
94
|
+
private bindQueueAndExchanges;
|
|
95
|
+
/**
|
|
96
|
+
* Function to create the queue
|
|
97
|
+
* @param data
|
|
98
|
+
*/
|
|
99
|
+
private createQueue;
|
|
100
|
+
/**
|
|
101
|
+
* Function to create the exchange
|
|
102
|
+
* @param data
|
|
103
|
+
*/
|
|
104
|
+
private createExchange;
|
|
105
|
+
/**
|
|
106
|
+
* Function to connect create to RabbitMQ
|
|
107
|
+
*/
|
|
108
|
+
createConnection(): Promise<ConnectionWrapper>;
|
|
109
|
+
/**
|
|
110
|
+
* Function for bind connection event handler on close and error setupQueuesAndExchanges
|
|
111
|
+
* @returns
|
|
112
|
+
*/
|
|
113
|
+
private bindConnectionEventHandler;
|
|
114
|
+
}
|