-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathAmqpService.js
More file actions
122 lines (106 loc) · 2.89 KB
/
Copy pathAmqpService.js
File metadata and controls
122 lines (106 loc) · 2.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
/**
* Copyright 2017–2018, LaborX PTY
* Licensed under the AGPL Version 3 license.
* @author Kirill Sergeev <cloudkserg11@gmail.com>
*/
const EventEmitter = require('events'),
amqp = require('amqplib');
/**
* Class for subscribe on amqp events
* from other middlewares
* listen only selected messages
*
* @class AmqpServer
* @extends {EventEmitter}
*/
class AmqpService extends EventEmitter {
/**
*
* constructor
* @param {String} url
* @param {String} exchange
* @param {String} serviceName
* options are:
* url - url for rabbit
* exchange - name exchange in rabbit
* serviceName - service name of created queues for binding in rabbit
*
*/
constructor (url, exchange, serviceName) {
if (!url || !exchange || !serviceName)
throw new Error('Not set url, exchange, serverName for constructor');
super();
this.url = url;
this.exchange = exchange;
this.serviceName = serviceName;
}
/**
* function for start (connect to rabbit)
*
* @memberOf AmqpServer
*/
async start () {
this.amqpInstance = await amqp.connect(this.url);
this.channel = await this.amqpInstance.createChannel();
this._onClosed = () => {
throw new Error('rabbitmq process has finished!');
};
this.channel.on('close', this._onClosed);
}
/**
* function to subscribe to this channel
* when get msg on this channel
* emit msg with type=emitMessage parameter
*
* @param {String} routing
* @param {String} emitMessage
*
* @memberOf AmqpServer
*/
async addBind (queue, routing, emitMessage) {
await this.channel.assertQueue(`${this.serviceName}.${queue}`, {autoDelete: true});
await this.channel.bindQueue(`${this.serviceName}.${queue}`, this.exchange, `${this.serviceName}.${routing}`);
this.channel.consume(`${this.serviceName}.${queue}`, async (data) => {
if (data.fields.routingKey === `${this.serviceName}.${routing}`)
this.emit(emitMessage, JSON.parse(data.content), data.fields.routingKey);
this.channel.ack(data);
});
}
/**
*
* Function to publish msg
*
* @param {String} routing
* @param {String} msg
*
* @memberOf AmqpService
*/
async publishMsg (routing, msg) {
return await this.channel.publish(this.exchange, `${this.serviceName}.${routing}`,
new Buffer(JSON.stringify(msg)));
}
/**
* function to unsubscribe from this channel
*
* @param {String} routing
*
* @memberOf AmqpServer
*/
async delBind (routing) {
await this.channel.cancel(`${this.serviceName}.${routing}`);
}
/**
* Function for close connection to rabbitmq
*
*
* @memberOf AmqpServer
*/
async close () {
if (this._onClosed && this.channel)
this.channel.removeListener('close', this._onClosed);
if (this.channel)
await this.channel.close();
await this.amqpInstance.close();
}
}
module.exports = AmqpService;