-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
133 lines (126 loc) · 4.61 KB
/
index.js
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
122
123
124
125
126
127
128
129
130
131
132
133
let amqp = require('amqplib/callback_api')
function bail(err) {
console.error(err);
}
function encode(doc) {
return new Buffer(JSON.stringify(doc));
}
class rabbitAdapter {
constructor(
hostname,
port,
username,
password,
locale = "en_US",
frameMax = 0,
heartbeat = 0,
vhost = "/",
protocol = 'amqp'
) {
this.hostname = hostname;
this.port = port;
this.username = username;
this.password = password;
this.locale = locale;
this.frameMax = frameMax;
this.heartbeat = heartbeat;
this.vhost = vhost;
this.amqp = amqp;
this.protocol = protocol
/**
* Publish to an Exchange and queue
*
* @function
*
* @param {string} exchangeName
* Exchange
* @param {string} queueName
* Queue Name
* @param {object} message
* Message as object (json)
*/
this.publishExchangeQueueMessage = async (exchangeName, queueName, message) => {
this.amqp.connect(
{
hostname: this.hostname,
port: this.port,
username: this.username,
password: this.password,
locale: this.locale,
frameMax: this.frameMax,
heartbeat: this.heartbeat,
vhost: this.vhost,
protocol: this.protocol,
}, (err, conn) => {
if (err != null) bail(err);
// Create Exchange
let cE = async (conn, exchangeName) => {
if (!conn || conn == undefined) {
return
}
try {
conn.createChannel(on_open);
}
catch (erro) {
console.log(erro)
}
function on_open(err, ch) {
if (err != null) bail(err);
let common_options = { durable: true, noAck: true };
ch.assertExchange(exchangeName, 'direct', common_options);
ch.sendToQueue(queueName, encode(message));
ch.close(function () {
conn.close();
});
}
}
cE(conn, exchangeName)
})
}
/**
* Listen to exchange and queue
*
* @function
*
* @param {string} exchangeName
* Exchange Name
* @param {string} queueName
* Queue Name
* @param {Object} onListen
* Callback
*/
this.listenExchange = async (exchangeName, queueName, onListen) => {
this.amqp.connect(
{
hostname: this.hostname,
port: this.port,
username: this.username,
password: this.password,
locale: this.locale,
frameMax: this.frameMax,
heartbeat: this.heartbeat,
vhost: this.vhost,
protocol: this.protocol,
}, (err, conn) => {
if (err != null) bail(err);
const listen = async (conn, exchangeName, queueName, onListen) => {
if (!conn || conn == undefined) {
return
}
conn.createChannel((err, ch) => {
if (err != null) bail(err);
let common_options = { durable: true, noAck: true };
ch.assertExchange(exchangeName, 'direct', common_options);
ch.assertQueue(queueName, { exclusive: true }, (err, q) => {
console.log(" [*] Waiting for messages in %s. To exit press CTRL+C", q.queue);
ch.bindQueue(q.queue, exchangeName, '');
ch.consume(q.queue, onListen, { noAck: true });
})
})
}
listen(conn, exchangeName, queueName, onListen);
});
}
}
}
module.exports.rabbitAdapter = rabbitAdapter