/
rpc.rb
46 lines (40 loc) · 1.27 KB
/
rpc.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
# A simple usage example for Beetle
require "rubygems"
require File.expand_path(File.dirname(__FILE__)+"/../lib/beetle")
# suppress debug messages
Beetle.config.logger.level = Logger::DEBUG
Beetle.config.servers = "localhost:5672, localhost:5673"
# instantiate a client
client = Beetle::Client.new
# register a durable queue named 'echo'
# this implicitly registers a durable topic exchange called 'echo'
client.register_queue(:echo)
client.register_message(:echo)
if ARGV.include?("--server")
# register a handler for the echo message, listing on queue "echo"
# echoing all data sent to it
client.register_handler(:echo) do |m|
# send data back to publisher
m.data
end
# start the subscriber
client.listen do
puts "started echo server"
trap("INT") { puts "stopped echo server"; client.stop_listening }
end
else
n = 100
ms = Benchmark.ms do
n.times do |i|
content = "Hello #{i}"
# puts "performing RPC with message content '#{content}'"
status, result = client.rpc(:echo, content)
# puts "status #{status}"
# puts "result #{result}"
# puts
$stderr.puts "processing failure for message '#{content}'" if result != content
end
end
printf "Runtime: %dms\n", ms
printf "Milliseconds per RPC: %.1f\n", ms/n
end