sb-rabbitmq-provider 1.0.23 → 1.0.25
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/dist/index.d.ts +4 -0
- package/dist/index.js +23 -2
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
@@ -15,13 +15,16 @@ interface rabbitMQConfig {
|
|
15
15
|
vhost?: string | undefined;
|
16
16
|
exchangeName?: string;
|
17
17
|
exchangeType: string;
|
18
|
+
dropMessageOnChannelFailure?: boolean;
|
18
19
|
}
|
19
20
|
type MyCallback = (data: any) => any;
|
20
21
|
declare class RabbitMqConnection {
|
21
22
|
config: rabbitMQConfig;
|
22
23
|
exchange: string;
|
23
24
|
exchangeType: string;
|
25
|
+
dropMessageOnChannelFailure: boolean;
|
24
26
|
connection: any;
|
27
|
+
channel: any;
|
25
28
|
/**
|
26
29
|
* constructor for initialising url, exchange name, exchange type
|
27
30
|
* @param url rabbitmq url
|
@@ -38,6 +41,7 @@ declare class RabbitMqConnection {
|
|
38
41
|
/**
|
39
42
|
* single connection can have multiple channel like publishChannel and consumeChannel
|
40
43
|
* @returns channel object
|
44
|
+
* No need to use this method, as channel is created in createConnection method.
|
41
45
|
*/
|
42
46
|
createChannel(): Promise<RabbitMQChannel>;
|
43
47
|
/**
|
package/dist/index.js
CHANGED
@@ -27,12 +27,16 @@ class RabbitMqConnection {
|
|
27
27
|
constructor(config) {
|
28
28
|
this.exchange = 'EXCHANGE';
|
29
29
|
this.exchangeType = 'direct';
|
30
|
+
this.dropMessageOnChannelFailure = false;
|
30
31
|
if (config.exchangeName && typeof config.exchangeName === 'string') {
|
31
32
|
this.exchange = config.exchangeName;
|
32
33
|
}
|
33
34
|
if (config.exchangeType && typeof config.exchangeType === 'string') {
|
34
35
|
this.exchangeType = config.exchangeType;
|
35
36
|
}
|
37
|
+
if (config.dropMessageOnChannelFailure) {
|
38
|
+
this.dropMessageOnChannelFailure = true;
|
39
|
+
}
|
36
40
|
this.config = config;
|
37
41
|
}
|
38
42
|
/**
|
@@ -44,6 +48,11 @@ class RabbitMqConnection {
|
|
44
48
|
return __awaiter(this, void 0, void 0, function* () {
|
45
49
|
try {
|
46
50
|
const connection = yield amqplib_1.default.connect(this.config);
|
51
|
+
if (this.exchange && this.exchangeType) {
|
52
|
+
const channel = yield this.connection.createChannel();
|
53
|
+
yield channel.assertExchange(this.exchange, this.exchangeType);
|
54
|
+
this.channel = channel;
|
55
|
+
}
|
47
56
|
// console.log('---RabbitMQ Connection Created---')
|
48
57
|
this.connection = connection;
|
49
58
|
return connection;
|
@@ -56,6 +65,7 @@ class RabbitMqConnection {
|
|
56
65
|
/**
|
57
66
|
* single connection can have multiple channel like publishChannel and consumeChannel
|
58
67
|
* @returns channel object
|
68
|
+
* No need to use this method, as channel is created in createConnection method.
|
59
69
|
*/
|
60
70
|
createChannel() {
|
61
71
|
return __awaiter(this, void 0, void 0, function* () {
|
@@ -103,8 +113,19 @@ class RabbitMqConnection {
|
|
103
113
|
const { queue } = yield channel.assertQueue(queueName, queueOptions);
|
104
114
|
yield channel.bindQueue(queue, this.exchange, queueName);
|
105
115
|
channel.consume(queueName, (message) => {
|
106
|
-
|
107
|
-
|
116
|
+
try {
|
117
|
+
// channel.ack(message);
|
118
|
+
onMessage(message);
|
119
|
+
}
|
120
|
+
catch (e) {
|
121
|
+
console.log('Rabbitmq consume catch block ', e);
|
122
|
+
if (message !== null) {
|
123
|
+
if (this.dropMessageOnChannelFailure) {
|
124
|
+
return channel.nack(message, false, false);
|
125
|
+
}
|
126
|
+
return channel.nack(message);
|
127
|
+
}
|
128
|
+
}
|
108
129
|
}, options);
|
109
130
|
return { status: true, message: `Started consuming Queue: ${queueName}` };
|
110
131
|
}
|