-
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 (35 loc) · 1.2 KB
/
rpc_client.js
File metadata and controls
executable file
·46 lines (35 loc) · 1.2 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
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);
const result = await new Promise((resolve) => {
// Consume from the Direct Reply-to pseudo-queue (noAck is mandatory)
channel.consume('amq.rabbitmq.reply-to', (msg) => {
if (msg.properties.correlationId === correlationId) {
resolve(msg.content.toString());
}
}, { noAck: true });
channel.sendToQueue('rpc_queue',
Buffer.from(num.toString()), {
correlationId: correlationId,
replyTo: 'amq.rabbitmq.reply-to'
});
});
console.log(' [.] Got %s', result);
await connection.close();
}
function generateUuid() {
return Math.random().toString() +
Math.random().toString() +
Math.random().toString();
}
main();