-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrpc_server.js
More file actions
31 lines (29 loc) · 835 Bytes
/
Copy pathrpc_server.js
File metadata and controls
31 lines (29 loc) · 835 Bytes
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
const amqp = require('amqplib/callback_api')
function fibonacci (n) {
if (n === 0 || n === 1) {
return n
} else {
return fibonacci(n - 1) + fibonacci(n - 2)
}
}
amqp.connect('amqp://localhost:5672', (err, conn) => {
if (err) console.log(err)
conn.createChannel((err, ch) => {
if (err) console.log(err)
const q = 'rpc_queue'
ch.assertQueue(q, { durable: false })
ch.prefetch(1)
console.log(' = Aguardando requisições RPC')
ch.consume(q, (msg) => {
const n = parseInt(msg.content.toString())
console.log(` = fibonacci(${n}) => replyTo: ${msg.properties.replyTo}`)
const r = fibonacci(n)
ch.sendToQueue(
msg.properties.replyTo,
Buffer.from(r.toString()),
{ correlationId: msg.properties.correlationId }
)
ch.ack(msg)
})
})
})