úlfurinn

A less obtrusive RPC with AMQP

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.


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

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