@platform-x/hep-message-broker-client 1.0.2 → 1.1.3

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.
Files changed (68) hide show
  1. package/README.md +1 -1
  2. package/dist/src/Util/commonUtil.js.map +1 -0
  3. package/dist/src/Util/constants.js +8 -0
  4. package/dist/src/Util/constants.js.map +1 -0
  5. package/dist/src/Util/logger.js.map +1 -0
  6. package/dist/src/Util/requestTracer.js.map +1 -0
  7. package/dist/src/config/ConfigManager.js.map +1 -0
  8. package/dist/src/config/index.js +1 -1
  9. package/dist/src/config/index.js.map +1 -0
  10. package/dist/src/index.js +22 -1
  11. package/dist/src/index.js.map +1 -0
  12. package/dist/src/messageBroker/BaseRabbitMQClient.js.map +1 -0
  13. package/dist/src/messageBroker/ConnectionManager.js.map +1 -0
  14. package/dist/src/messageBroker/MessageBrokerClient.js.map +1 -0
  15. package/dist/src/messageBroker/MessageConsumer.js.map +1 -0
  16. package/dist/src/messageBroker/MessageProducer.js.map +1 -0
  17. package/dist/src/messageBroker/RabbitMQClient.js.map +1 -0
  18. package/dist/src/messageBroker/RetryManager.js.map +1 -0
  19. package/dist/src/messageBroker/interface/ConnectionWrapper.js.map +1 -0
  20. package/dist/src/messageBroker/interface/IMessageBrokerClient.js.map +1 -0
  21. package/dist/src/messageBroker/rabbitmq/MessageBrokerClient.js +5 -1
  22. package/dist/src/messageBroker/rabbitmq/MessageBrokerClient.js.map +1 -0
  23. package/dist/src/messageBroker/types/ActionType.js.map +1 -0
  24. package/dist/src/messageBroker/types/PublishMessageInputType.js.map +1 -0
  25. package/dist/src/models/MessageModel.js.map +1 -0
  26. package/dist/src/models/NotificationMessageModel.js.map +1 -0
  27. package/package.json +43 -40
  28. package/src/Util/commonUtil.ts +41 -0
  29. package/src/Util/constants.ts +4 -0
  30. package/src/Util/logger.ts +219 -0
  31. package/src/Util/requestTracer.ts +28 -0
  32. package/src/config/ConfigManager.ts +35 -0
  33. package/src/config/index.ts +31 -0
  34. package/src/index.ts +73 -0
  35. package/src/messageBroker/BaseRabbitMQClient.ts +30 -0
  36. package/src/messageBroker/ConnectionManager.ts +182 -0
  37. package/src/messageBroker/MessageBrokerClient.ts +88 -0
  38. package/src/messageBroker/MessageConsumer.ts +85 -0
  39. package/src/messageBroker/MessageProducer.ts +142 -0
  40. package/src/messageBroker/RabbitMQClient.ts +47 -0
  41. package/src/messageBroker/RetryManager.ts +64 -0
  42. package/src/messageBroker/interface/ConnectionWrapper.ts +7 -0
  43. package/src/messageBroker/interface/IMessageBrokerClient.ts +11 -0
  44. package/src/messageBroker/rabbitmq/MessageBrokerClient.ts +681 -0
  45. package/src/messageBroker/types/ActionType.ts +1 -0
  46. package/src/messageBroker/types/PublishMessageInputType.ts +8 -0
  47. package/src/models/MessageModel.ts +14 -0
  48. package/tsconfig.json +73 -0
  49. package/dist/src/Util/commonUtil.d.ts +0 -16
  50. package/dist/src/Util/logger.d.ts +0 -68
  51. package/dist/src/Util/requestTracer.d.ts +0 -7
  52. package/dist/src/config/ConfigManager.d.ts +0 -9
  53. package/dist/src/config/index.d.ts +0 -29
  54. package/dist/src/index.d.ts +0 -2
  55. package/dist/src/messageBroker/BaseRabbitMQClient.d.ts +0 -16
  56. package/dist/src/messageBroker/ConnectionManager.d.ts +0 -60
  57. package/dist/src/messageBroker/MessageBrokerClient.d.ts +0 -34
  58. package/dist/src/messageBroker/MessageConsumer.d.ts +0 -26
  59. package/dist/src/messageBroker/MessageProducer.d.ts +0 -29
  60. package/dist/src/messageBroker/RabbitMQClient.d.ts +0 -12
  61. package/dist/src/messageBroker/RetryManager.d.ts +0 -23
  62. package/dist/src/messageBroker/interface/ConnectionWrapper.d.ts +0 -6
  63. package/dist/src/messageBroker/interface/IMessageBrokerClient.d.ts +0 -7
  64. package/dist/src/messageBroker/rabbitmq/MessageBrokerClient.d.ts +0 -122
  65. package/dist/src/messageBroker/types/ActionType.d.ts +0 -1
  66. package/dist/src/messageBroker/types/PublishMessageInputType.d.ts +0 -7
  67. package/dist/src/models/MessageModel.d.ts +0 -5
  68. /package/{dist/src/models/NotificationMessageModel.d.ts → src/models/NotificationMessageModel.ts} +0 -0
@@ -0,0 +1,47 @@
1
+ // Import the amqplib library for RabbitMQ
2
+ import { Logger } from '../Util/logger';
3
+ import BaseRabbitMQClient from './BaseRabbitMQClient';
4
+ import ConnectionManager from './ConnectionManager';
5
+
6
+
7
+ export class RabbitMQClient extends BaseRabbitMQClient {
8
+ constructor() {
9
+ super();
10
+ }
11
+
12
+ /**
13
+ * Function to initialize the RabbitMQ connection
14
+ */
15
+ public async initialize() {
16
+ try {
17
+ Logger.info('Reached to initialize', 'initialize');
18
+ // Create a connection to RabbitMQ server
19
+ const connectionManager = await ConnectionManager.getInstance();
20
+ await connectionManager.createConnection();
21
+ Logger.info('RabbitMQ connection created', 'initialize');
22
+ } catch (error) {
23
+ // Log and rethrow any errors that occur during initialization
24
+ Logger.error('Failed to create RabbitMQ connection', 'initialize', error);
25
+ throw error;
26
+ }
27
+ }
28
+
29
+ /**
30
+ * Function to close the connection and channel
31
+ */
32
+ public async disconnect() {
33
+ try {
34
+ Logger.info('Reached to disconnect', 'disconnect');
35
+ // Close the channel
36
+ await this.channel?.close();
37
+ // Close the connection
38
+ await this.connection?.close();
39
+ Logger.info('RabbitMQ connection closed', 'disconnect');
40
+ } catch (error) {
41
+ // Log and rethrow any errors that occur during closing
42
+ Logger.error('Failed to close RabbitMQ connection', 'disconnect', error);
43
+ throw error;
44
+ }
45
+ };
46
+
47
+ }
@@ -0,0 +1,64 @@
1
+ import { Logger } from '../Util/logger';
2
+ type ActionType<T> = (input: T) => void;
3
+
4
+ class RetryManager<T>{
5
+ private static instance: RetryManager<any>;
6
+ private readonly max_retry:number;
7
+ private readonly retry_delay:string[]|string;
8
+
9
+ constructor(max_retry:number, retry_delay:string){
10
+ this.max_retry = max_retry;
11
+ this.retry_delay = retry_delay?.split("|");
12
+ }
13
+
14
+ /**
15
+ * Function for create singlton class instance and return instance
16
+ * @param max_retry
17
+ * @param retry_delay
18
+ * @returns
19
+ */
20
+ public static getInstance<T>(max_retry: number, retry_delay: string): RetryManager<T> {
21
+ if (!RetryManager.instance) {
22
+ RetryManager.instance = new RetryManager<T>(max_retry, retry_delay);
23
+ }
24
+ return RetryManager.instance as RetryManager<T>;
25
+ }
26
+ /**
27
+ * Function for perform action with retry
28
+ * @param actionHandler
29
+ * @param context
30
+ * @param failActionHandler
31
+ * @returns
32
+ */
33
+ public async performActionWithRetry(actionHandler:ActionType<T>, context:T, failActionHandler:ActionType<T>):Promise<boolean>{
34
+ Logger.info("Reached: in perform action with retry", "performActionWithRetry")
35
+ let attempt: number = 0;
36
+ while (attempt < this.max_retry) {
37
+ try {
38
+ actionHandler(context);
39
+ return true;
40
+ } catch (error) {
41
+ attempt++;
42
+ Logger.error(`Failed to publish message. Attempt ${attempt} of ${this.max_retry}. Error:`, 'performActionWithRetry', { message: (error as Error).message });
43
+ if (attempt >= this.max_retry) {
44
+ Logger.error('Max retries reached. Failed to publish message.', 'performActionWithRetry');
45
+ // Optionally: Log or persist the failed message to a DB, file, or other storage for manual intervention
46
+ failActionHandler(context);
47
+ return false;
48
+ }
49
+ // Wait before retrying (Exponential Backoff)
50
+ let retryDelay: number;
51
+ if(this.retry_delay.length-1 === attempt){
52
+ retryDelay = Number(this.retry_delay[attempt - 1]);
53
+ }else{
54
+ retryDelay = Number(this.retry_delay[this.retry_delay.length-1]);
55
+ }
56
+ Logger.info(`Retrying in ${retryDelay / 1000} seconds...`, 'performActionWithRetry');
57
+ await new Promise((resolve) => setTimeout(resolve, retryDelay));
58
+ }
59
+ }
60
+ return false;
61
+ }
62
+
63
+ }
64
+ export default RetryManager;
@@ -0,0 +1,7 @@
1
+ import * as amqp from 'amqplib';
2
+
3
+ interface ConnectionWrapper {
4
+ connection: amqp.Connection,
5
+ channel: amqp.Channel
6
+ }
7
+ export default ConnectionWrapper;
@@ -0,0 +1,11 @@
1
+ import { PublishMessageInputType } from "../types/PublishMessageInputType";
2
+
3
+ export interface IMessageBrokerClient {
4
+ initialize(configData: object | null): void;
5
+ disconnect(): void;
6
+ // will create a class for publishOptions instead of object
7
+ publishMessage(request:PublishMessageInputType): Promise<boolean>;
8
+ // will create a class for publishOptions instead of object
9
+ publishMessageToExchange(request: PublishMessageInputType):Promise<boolean>;
10
+ // publish confirm in publishMessage need to implement
11
+ }