forked from rabbitmq/rabbitmq-tutorials
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rpc_server.rb
59 lines (47 loc) · 1.13 KB
/
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
52
53
54
55
56
57
58
59
#!/usr/bin/env ruby
require 'bunny'
class FibonacciServer
def initialize
@connection = Bunny.new
@connection.start
@channel = @connection.create_channel
end
def start(queue_name)
@queue = channel.queue(queue_name)
@exchange = channel.default_exchange
subscribe_to_queue
end
def stop
channel.close
connection.close
end
def loop_forever
# This loop only exists to keep the main thread
# alive. Many real world apps won't need this.
loop { sleep 5 }
end
private
attr_reader :channel, :exchange, :queue, :connection
def subscribe_to_queue
queue.subscribe do |_delivery_info, properties, payload|
result = fibonacci(payload.to_i)
exchange.publish(
result.to_s,
routing_key: properties.reply_to,
correlation_id: properties.correlation_id
)
end
end
def fibonacci(value)
return value if value.zero? || value == 1
fibonacci(value - 1) + fibonacci(value - 2)
end
end
begin
server = FibonacciServer.new
puts ' [x] Awaiting RPC requests'
server.start('rpc_queue')
server.loop_forever
rescue Interrupt => _
server.stop
end