-
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
·47 lines (37 loc) · 1.18 KB
/
rpc_client.js
File metadata and controls
executable file
·47 lines (37 loc) · 1.18 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
#!/usr/bin/env node
const amqp = require('amqplib');
const args = process.argv.slice(2);
if (args.length === 0) {
console.log("Usage: rpc_client.js num");
process.exit(1);
}
async function main() {
const connection = await amqp.connect('amqp://localhost');
const channel = await connection.createChannel();
const correlationId = generateUuid();
const num = parseInt(args[0]);
console.log(' [x] Requesting fib(%d)', num);
// Consume from the Direct Reply-to pseudo-queue (noAck is mandatory)
channel.consume('amq.rabbitmq.reply-to', function(msg) {
if (msg.properties.correlationId === correlationId) {
console.log(' [.] Got %s', msg.content.toString());
setTimeout(function() {
connection.close();
process.exit(0);
}, 500);
}
}, {
noAck: true
});
channel.sendToQueue('rpc_queue',
Buffer.from(num.toString()), {
correlationId: correlationId,
replyTo: 'amq.rabbitmq.reply-to'
});
}
function generateUuid() {
return Math.random().toString() +
Math.random().toString() +
Math.random().toString();
}
main();