-
Notifications
You must be signed in to change notification settings - Fork 3.6k
Expand file tree
/
Copy pathrpc_client.js
More file actions
executable file
·46 lines (36 loc) · 1.17 KB
/
rpc_client.js
File metadata and controls
executable file
·46 lines (36 loc) · 1.17 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
#!/usr/bin/env node
var amqp = require('amqplib');
var when = require('when');
var conn = amqp.connect('amqp://localhost')
conn.then(createChannel).then(null, console.warn);
function createChannel(conn) {
return when(conn.createChannel().then(requestFib)).ensure(function() { conn.close(); });
}
function requestFib(ch) {
var answer = when.defer();
var correlationId = generateUuid();
function maybeAnswer(msg) {
if (msg.properties.correlationId === correlationId) {
answer.resolve(msg.content.toString());
}
}
var ok = ch.assertQueue('', {exclusive: true})
.then(function(qok) { return qok.queue; });
ok = ok.then(function(queue) {
return ch.consume(queue, maybeAnswer, {noAck: true})
.then(function() { return queue; });
});
ok = ok.then(function(queue) {
console.log(' [x] Requesting fib(30)');
ch.sendToQueue('rpc_queue', new Buffer('30'), {
correlationId: correlationId, replyTo: queue
});
return answer.promise;
});
return ok.then(function(fibN) {
console.log(' [.] Got %d', fibN);
});
}
function generateUuid() {
return Math.random().toString() + Math.random().toString() + Math.random().toString();
}