forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_server.rb
51 lines (38 loc) · 863 Bytes
/
rpc_server.rb
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
48
49
50
51
#!/usr/bin/env ruby
# encoding: utf-8
require "bunny"
conn = Bunny.new(:automatically_recover => false)
conn.start
ch = conn.create_channel
class FibonacciServer
def initialize(ch)
@ch = ch
end
def start(queue_name)
@q = @ch.queue(queue_name)
@x = @ch.default_exchange
@q.subscribe(:block => true) do |delivery_info, properties, payload|
n = payload.to_i
r = self.class.fib(n)
puts " [.] fib(#{n})"
@x.publish(r.to_s, :routing_key => properties.reply_to, :correlation_id => properties.correlation_id)
end
end
def self.fib(n)
case n
when 0 then 0
when 1 then 1
else
fib(n - 1) + fib(n - 2)
end
end
end
begin
server = FibonacciServer.new(ch)
puts " [x] Awaiting RPC requests"
server.start("rpc_queue")
rescue Interrupt => _
ch.close
conn.close
exit(0)
end