Created
September 2, 2014 03:55
-
-
Save Imater/3fec74f4e4bd3b8706ac to your computer and use it in GitHub Desktop.
Receive from RabbitMQ queue
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fib = (n) -> | |
# Do it the ridiculous, but not most ridiculous, way. For better, | |
# see http://nayuki.eigenstate.org/page/fast-fibonacci-algorithms | |
a = 0 | |
b = 1 | |
i = 0 | |
while i < n | |
c = a + b | |
a = b | |
b = c | |
i++ | |
a | |
amqp = require("amqplib") | |
amqp.connect("amqp://localhost").then((conn) -> | |
process.once "SIGINT", -> | |
conn.close() | |
return | |
conn.createChannel().then (ch) -> | |
reply = (msg) -> | |
n = parseInt(msg.content.toString()) | |
console.log " [.] fib(%d)", n | |
response = fib(n) | |
ch.sendToQueue msg.properties.replyTo, new Buffer(response.toString()), | |
correlationId: msg.properties.correlationId | |
ch.ack msg | |
return | |
q = "rpc_queue" | |
ok = ch.assertQueue(q, | |
durable: false | |
) | |
ok = ok.then(-> | |
ch.prefetch 1 | |
ch.consume q, reply | |
) | |
return ok.then(-> | |
console.log " [x] Awaiting RPC requests" | |
return | |
) | |
return | |
).then null, console.warn |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment