Skip to content

Instantly share code, notes, and snippets.

@StasKoval
Forked from gaffneyc/rpc_consumer.rb
Created February 27, 2016 07:14
Show Gist options
  • Save StasKoval/779c7776f8f33a320cbe to your computer and use it in GitHub Desktop.
Save StasKoval/779c7776f8f33a320cbe to your computer and use it in GitHub Desktop.
Basic RPC using RabbitMQ and Bunny
require 'rubygems'
gem 'gaffneyc-bunny'
require 'bunny'
# Example of simple RPC using server named queues
Bunny.open do |amqp|
# Exchange definition
amqp.exchange('reply', :type => :direct)
amqp.exchange('out', :type => :direct)
amqp.queue('rpc').bind('out')
# Publish!
amqp.queue('rpc').subscribe(:header => true) do |msg|
puts msg[:payload]
amqp.exchange('reply').publish("[response goes here]", :key => msg[:header].reply_to)
end
end
require 'rubygems'
gem 'gaffneyc-bunny'
require 'bunny'
gem 'SystemTimer'
require 'system_timer'
# Example of simple RPC using server named queues
Bunny.open do |amqp|
# Exchange definition
amqp.exchange('reply', :type => :direct)
amqp.exchange('out', :type => :direct)
# Create the temporary queue and bind it
reply = amqp.queue
reply.bind('reply', :key => reply.name)
# Publish!
amqp.exchange('out').publish("[contents go here]", :reply_to => reply.name)
begin
SystemTimer.timeout_after(15) do
reply.subscribe do |msg|
puts msg
reply.unsubscribe
end
end
rescue Timeout::Error
puts "Timed out waiting for response"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment