rabbitmq-tutorials/ruby-amqp/rpc_server.rb
Michael Klishin 2ec87888cb ruby => ruby-amqp
ruby should be done with Bunny 0.9 which is much easier to use
for beginners.
2013-07-17 18:07:03 +04:00

35 lines
680 B
Ruby
Executable File

#!/usr/bin/env ruby
# encoding: utf-8
require "amqp"
def fib(n)
return n if n == 0 || n == 1
return fib(n - 1) + fib(n - 2)
end
AMQP.start(:host => "localhost") do |connection|
channel = AMQP::Channel.new(connection)
queue = channel.queue("rpc_queue")
Signal.trap("INT") do
connection.close do
EM.stop { exit }
end
end
channel.prefetch(1)
queue.subscribe(:ack => true) do |header, body|
n = body.to_i
puts " [.] fib(#{n})"
response = fib(n)
AMQP::Exchange.default.publish(response.to_s, :routing_key => header.reply_to, :correlation_id => header.correlation_id)
header.ack
end
puts " [x] Awaiting RPC requests"
end