@munlib/common 1.0.59 → 1.0.60
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/build/events/base-listener.d.ts +14 -18
- package/build/events/base-listener.js +45 -20
- package/package.json +1 -1
|
@@ -1,21 +1,17 @@
|
|
|
1
|
-
import { Channel
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
exchangeName: ExchangeNames;
|
|
5
|
-
exchangeType: ExchangeTypes;
|
|
6
|
-
queueName: QueueNames;
|
|
7
|
-
routingKey: RoutingKeys;
|
|
8
|
-
data: any;
|
|
1
|
+
import { Channel } from "amqplib";
|
|
2
|
+
interface ListenerData<T> {
|
|
3
|
+
data: T;
|
|
9
4
|
}
|
|
10
|
-
export declare abstract class Listener<T extends
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
5
|
+
export declare abstract class Listener<T extends ListenerData<T["data"]>> {
|
|
6
|
+
private connection;
|
|
7
|
+
private channel;
|
|
8
|
+
private exchangeName;
|
|
9
|
+
private exchangeType;
|
|
10
|
+
private queueName;
|
|
11
|
+
private routingKey;
|
|
12
|
+
abstract onMessage(data: T["data"], msg: any, channel: Channel): void;
|
|
13
|
+
constructor(exchangeName: string, exchangeType: string, queueName: string, routingKey: string);
|
|
14
|
+
connect(): Promise<void>;
|
|
15
|
+
close(): Promise<void>;
|
|
20
16
|
}
|
|
21
17
|
export {};
|
|
@@ -37,45 +37,70 @@ var __generator = (this && this.__generator) || function (thisArg, body) {
|
|
|
37
37
|
};
|
|
38
38
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
39
39
|
exports.Listener = void 0;
|
|
40
|
+
var amqplib_1 = require("amqplib");
|
|
40
41
|
var Listener = /** @class */ (function () {
|
|
41
|
-
function Listener(
|
|
42
|
-
this.channel = channel;
|
|
42
|
+
function Listener(exchangeName, exchangeType, queueName, routingKey) {
|
|
43
43
|
this.exchangeName = exchangeName;
|
|
44
44
|
this.exchangeType = exchangeType;
|
|
45
45
|
this.queueName = queueName;
|
|
46
46
|
this.routingKey = routingKey;
|
|
47
47
|
}
|
|
48
|
-
Listener.prototype.
|
|
48
|
+
Listener.prototype.connect = function () {
|
|
49
49
|
return __awaiter(this, void 0, void 0, function () {
|
|
50
|
+
var _a, _b, queue;
|
|
50
51
|
var _this = this;
|
|
52
|
+
return __generator(this, function (_c) {
|
|
53
|
+
switch (_c.label) {
|
|
54
|
+
case 0:
|
|
55
|
+
_a = this;
|
|
56
|
+
return [4 /*yield*/, (0, amqplib_1.connect)(process.env.MESSAGE_BROKER_URL)];
|
|
57
|
+
case 1:
|
|
58
|
+
_a.connection = _c.sent();
|
|
59
|
+
_b = this;
|
|
60
|
+
return [4 /*yield*/, this.connection.createChannel()];
|
|
61
|
+
case 2:
|
|
62
|
+
_b.channel = _c.sent();
|
|
63
|
+
return [4 /*yield*/, this.channel.assertExchange(this.exchangeName, this.exchangeType, { durable: false })];
|
|
64
|
+
case 3:
|
|
65
|
+
_c.sent();
|
|
66
|
+
return [4 /*yield*/, this.channel.assertQueue(this.queueName, {
|
|
67
|
+
exclusive: false,
|
|
68
|
+
})];
|
|
69
|
+
case 4:
|
|
70
|
+
queue = (_c.sent()).queue;
|
|
71
|
+
console.log("Listening for messages on queue: ".concat(queue));
|
|
72
|
+
// Bind the queue to the exchange with a routing key
|
|
73
|
+
this.channel.bindQueue(queue, this.exchangeName, this.routingKey);
|
|
74
|
+
this.channel.consume(queue, function (msg) { return __awaiter(_this, void 0, void 0, function () {
|
|
75
|
+
var data;
|
|
76
|
+
return __generator(this, function (_a) {
|
|
77
|
+
if (msg) {
|
|
78
|
+
data = JSON.parse(msg.content.toString());
|
|
79
|
+
this.onMessage(data, msg, this.channel);
|
|
80
|
+
}
|
|
81
|
+
return [2 /*return*/];
|
|
82
|
+
});
|
|
83
|
+
}); });
|
|
84
|
+
return [2 /*return*/];
|
|
85
|
+
}
|
|
86
|
+
});
|
|
87
|
+
});
|
|
88
|
+
};
|
|
89
|
+
Listener.prototype.close = function () {
|
|
90
|
+
return __awaiter(this, void 0, void 0, function () {
|
|
51
91
|
return __generator(this, function (_a) {
|
|
52
92
|
switch (_a.label) {
|
|
53
|
-
case 0: return [4 /*yield*/, this.channel.
|
|
54
|
-
durable: false,
|
|
55
|
-
})];
|
|
93
|
+
case 0: return [4 /*yield*/, this.channel.close()];
|
|
56
94
|
case 1:
|
|
57
95
|
_a.sent();
|
|
58
|
-
return [4 /*yield*/, this.
|
|
96
|
+
return [4 /*yield*/, this.connection.close()];
|
|
59
97
|
case 2:
|
|
60
98
|
_a.sent();
|
|
61
|
-
return [4 /*yield*/, this.channel.bindQueue(this.queueName, this.exchangeName, this.routingKey)];
|
|
62
|
-
case 3:
|
|
63
|
-
_a.sent();
|
|
64
|
-
this.channel.consume(this.queueName, function (msg) {
|
|
65
|
-
if (msg) {
|
|
66
|
-
var parsedData = _this.parseMessage(msg);
|
|
67
|
-
_this.onMessage(parsedData, msg);
|
|
68
|
-
}
|
|
69
|
-
});
|
|
70
99
|
return [2 /*return*/];
|
|
71
100
|
}
|
|
72
101
|
});
|
|
73
102
|
});
|
|
74
103
|
};
|
|
75
|
-
Listener.prototype.parseMessage = function (msg) {
|
|
76
|
-
var data = msg.content.toString();
|
|
77
|
-
return JSON.parse(data);
|
|
78
|
-
};
|
|
79
104
|
return Listener;
|
|
80
105
|
}());
|
|
81
106
|
exports.Listener = Listener;
|