A Less Obtrusive RPC With AMQP
Oct 11, 2012

The RPC example that comes with the AMQP gem has all the dangly callback stuff on the outside; I thought I’d try to hide some of the loose ends. It’s pretty much standard EM+Fiber magic. I might try to further improve on it later on.

The RPC example that comes with the AMQP gem has all the dangly callback stuff on the outside; I thought I’d try to hide some of the loose ends. It’s pretty much standard EM+Fiber magic. I might try to further improve on it later on.

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
class Calculator

  include AMQP_RPC::Client

  def initialize
    subscribe
  end

end

EM.run do

  Fiber.new do

    c = Calculator.new
    1.upto(50) { |i|
      puts c.remote_method(i)
    }

    EM.stop { exit }

  end.resume

end
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
class Calculator

  include AMQP_RPC::Server
  provide :remote_method

  def initialize
    subscribe
  end

  def remote_method x
    x * 2
  end

end

EM.run do

  server = Calculator.new

end

gist