This repository has been archived by the owner on Mar 10, 2020. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 129
/
amqp_ascoltatore.js
230 lines (181 loc) · 5.5 KB
/
amqp_ascoltatore.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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
"use strict";
var util = require("./util");
var wrap = util.wrap;
var defer = util.defer;
var TrieAscoltatore = require("./trie_ascoltatore");
var AbstractAscoltatore = require('./abstract_ascoltatore');
var steed = require("steed")();
var SubsCounter = require("./subs_counter");
var debug = require("debug")("ascoltatori:amqp");
/**
* The AMQPAscoltatore is a class that inherits from AbstractAscoltatore.
* It is backed by node-amqp.
* It creates or use an exchange with the given name, using a "topic" topology.
* It creates a single amqp queue for this process, in order to keep
* the overhead low.
*
* It accepts these options:
* - `client`, which is passed through to the amq.createConnection method;
* - `exchange`, the exchange name;
* - `amqp`, the amqp module (it will automatically be required if not present);
*
* @param {Object} opts The options for creating this ascoltatore.
* @api public
*/
function AMQPAscoltatore(opts) {
AbstractAscoltatore.call(this, opts, {
separator: '.',
wildcardOne: '*',
wildcardSome: '#'
});
this._opts = opts || {};
this._opts.amqp = this._opts.amqp || require("amqp");
this._ascoltatore = new TrieAscoltatore(opts);
this._subs_counter = new SubsCounter();
this._startConn();
}
/**
* The client connection decends from AbstractAscoltatore.
*
* @api private
*/
AMQPAscoltatore.prototype = Object.create(AbstractAscoltatore.prototype);
/**
* Starts a new connection to an AMQP server.
* Do nothing if it is already started.
*
* @api private
*/
AMQPAscoltatore.prototype._startConn = function() {
var conn = null,
that = this;
if (this._client_conn === undefined) {
conn = this._opts.amqp.createConnection(this._opts.client);
this._client_conn = conn;
conn.on("error", function(error) {
if (typeof error === 'string') {
error = (new Error(error));
}
that.emit("error", error);
});
debug("connecting to " + this._opts.client);
steed.series([
function(callback) {
that._client_conn.once("ready", wrap(callback));
},
function(callback) {
debug("connected");
that._exchange = conn.exchange(that._opts.exchange, {
type: "topic",
confirm: true
});
that._exchange.once("open", wrap(callback));
},
function(callback) {
debug("created exchange " + that._opts.exchange);
that._queue = conn.queue(util.buildIdentifier(), wrap(callback));
that._queue.setMaxListeners(0); // avoid problems with listeners
},
function(callback) {
that._queue.subscribe({
ack: true,
prefetchCount: 42
}, function(message, headers, deliveryInfo) {
that._queue.shift();
var topic = that._recvTopic(deliveryInfo.routingKey);
debug("new message received from queue on topic " + topic);
that._ascoltatore.publish(topic, message.data.toString());
});
that._queue.once("basicConsumeOk", function() {
defer(callback);
});
},
function(callback) {
debug("subscribed to queue");
that.emit("ready");
callback();
}
]);
}
return this._client_conn;
};
AMQPAscoltatore.prototype.subscribe = function subscribe(topic, callback, done) {
this._raiseIfClosed();
this._ascoltatore.subscribe(topic, callback);
if (!this._subs_counter.include(topic)) {
debug("binding queue to topic " + topic);
this._queue.once("queueBindOk", function() {
// trick against node-amqp not working
// as advertised
setTimeout(function() {
debug("queue bound to topic " + topic);
defer(done);
}, 5);
});
this._queue.bind(this._exchange, this._subTopic(topic));
} else {
defer(done);
}
this._subs_counter.add(topic);
debug("registered new subscriber for topic " + topic);
};
AMQPAscoltatore.prototype.publish = function publish(topic, message, done) {
this._raiseIfClosed();
debug("new message published to " + topic);
this._exchange.publish(this._pubTopic(topic), String(message));
defer(done);
};
AMQPAscoltatore.prototype.unsubscribe = function unsubscribe(topic, callback, done) {
this._raiseIfClosed();
this._subs_counter.remove(topic);
debug("deregistered subscriber for topic " + topic);
this._ascoltatore.unsubscribe(topic, callback);
if (!this._subs_counter.include(topic)) {
this._queue.once("queueUnbindOk", function() {
debug("queue unbound to topic " + topic);
defer(done);
});
this._queue.unbind(this._exchange, this._subTopic(topic));
} else {
defer(done);
}
return this;
};
AMQPAscoltatore.prototype.close = function close(done) {
var that = this;
if (this._closed) {
wrap(done)();
return;
}
if (this._closing) {
this.on("closed", done);
return;
}
this._closing = true;
if (this._client_conn !== undefined) {
var doClose = function () {
if (that._closed) {
debug("closing twice, one was an error");
return;
}
debug("closed");
defer(done);
that.emit("closed");
};
this._client_conn.on("close", doClose);
this._queue.destroy();
this._client_conn.end();
this._client_conn.removeAllListeners("error");
this._client_conn.on("error", doClose);
delete this._client_conn;
delete this._exchange;
delete this._queue;
}
};
util.aliasAscoltatore(AMQPAscoltatore.prototype);
/**
* Exports the AMQPAscoltatore
*
* @api public
*/
module.exports = AMQPAscoltatore;