- 
      
- 
        Save StasKoval/779c7776f8f33a320cbe to your computer and use it in GitHub Desktop. 
    Basic RPC using RabbitMQ and Bunny
  
        
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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 | 
  
    
      This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
      Learn more about bidirectional Unicode characters
    
  
  
    
  | 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