Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 22nd, 2012  |  syntax: None  |  size: 0.82 KB  |  hits: 15  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. require "amqp"
  2.  
  3. AMQP_CONFIG = {
  4.   host: '127.0.0.1',
  5.   port: 5671,
  6.   username: '<...>',
  7.   password: '<...>',
  8.   ssl: true
  9. }
  10.  
  11. EventMachine.run do
  12.   connection = AMQP.connect(AMQP_CONFIG)
  13.   channel = AMQP::Channel.new(connection)
  14.   channel.queue("org.haggett.amqptest", :exclusive => true, :auto_delete => true) do |queue, declare_ok|
  15.     puts "Declared work queue."
  16.    
  17.     queue.subscribe(:ack => true) do |metadata, payload|
  18.       received_time = payload
  19.       start_time = Time.now.to_f
  20.      
  21.       EM.add_timer(rand(2.0)) do
  22.         end_time = Time.now.to_f
  23.        
  24.         channel.default_exchange.publish([received_time, start_time, end_time].join(":"), :routing_key => metadata.reply_to, :mandatory => true)
  25.         metadata.ack
  26.       end
  27.      
  28.     end
  29.   end
  30.  
  31.   Signal.trap("INT") { connection.close { EventMachine.stop } }
  32. end