Skip to content

Instantly share code, notes, and snippets.

@radamant
Created May 12, 2009 23:49
Show Gist options
  • Save radamant/110788 to your computer and use it in GitHub Desktop.
Save radamant/110788 to your computer and use it in GitHub Desktop.
require 'mq'
Signal.trap('INT') { AMQP.stop{ EM.stop } }
Signal.trap('TERM'){ AMQP.stop{ EM.stop } }
AMQP.start(:host => 'aiprabbitmq.amer.gettywan.com') do
queue = MQ.queue('foobar')
queue.pop{ |msg|
unless msg
# queue was empty
p [Time.now, :queue_empty!]
# try again in 1 second
EM.add_timer(5){ queue.pop }
else
# process this message
p [Time.now, msg]
# get the next message in the queue
EM.add_timer(5){queue.pop}
end
}
end
[Tue May 12 16:46:26 -0700 2009, :queue_empty!]
[Tue May 12 16:46:31 -0700 2009, "item 0 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:46:36 -0700 2009, "item 1 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:46:41 -0700 2009, "item 2 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:46:46 -0700 2009, "item 3 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:46:51 -0700 2009, "item 4 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:46:56 -0700 2009, "item 5 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:01 -0700 2009, "item 6 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:06 -0700 2009, "item 7 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:11 -0700 2009, "item 8 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:16 -0700 2009, "item 9 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:21 -0700 2009, "item 10 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:26 -0700 2009, "item 11 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:31 -0700 2009, "item 12 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:36 -0700 2009, "item 13 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:41 -0700 2009, "item 14 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:46 -0700 2009, "item 15 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:51 -0700 2009, "item 16 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:47:57 -0700 2009, "item 17 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:02 -0700 2009, "item 18 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:07 -0700 2009, "item 19 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:12 -0700 2009, "item 20 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:17 -0700 2009, "item 21 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:22 -0700 2009, "item 22 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:27 -0700 2009, "item 23 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:32 -0700 2009, "item 24 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:37 -0700 2009, "item 25 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:42 -0700 2009, "item 26 Tue May 12 16:46:29 -0700 2009"]
[Tue May 12 16:48:47 -0700 2009, "item 27 Tue May 12 16:46:29 -0700 2009"]
#!/bin/env ruby
require 'rubygems'
require 'amqp'
require 'mq'
Signal.trap('INT') { AMQP.stop{ EM.stop } }
Signal.trap('TERM'){ AMQP.stop{ EM.stop } }
AMQP.start(:host => 'aiprabbitmq.amer.gettywan.com') do
queue = MQ.queue('foobar hi peter')
100.times do |i|
queue.publish("item #{i} #{Time.now.to_s}")
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment