Skip to content

Instantly share code, notes, and snippets.

@waynerobinson
Created August 31, 2011 03:55
Show Gist options
  • Save waynerobinson/1182783 to your computer and use it in GitHub Desktop.
Save waynerobinson/1182783 to your computer and use it in GitHub Desktop.
require 'rubygems'
require 'eventmachine'
require 'amqp'
class QueueDaemon
class << self
def exchange
@exchange
end
def ready?
not @exchange.nil?
end
def start!
thread = Thread.new do
EventMachine.run do
puts "Asimov: Attempting Connection"
AMQP.connect(
:on_possible_authentication_failure => Proc.new {puts("Asimov: Possible Authentication Failure")},
:on_tcp_connection_failure => Proc.new {puts("Asimov: TCP failure")},
:on_error => Proc.new {puts("Fucked")},
:on_tcp_loss => Proc.new {puts("Still Fucked")},
:on_connection_interrruption => Proc.new {puts("DODGY")}
) do |connection|
puts "Asimov: Connected to host"
channel = AMQP::Channel.new(connection)
queue = channel.queue("amqpgem.examples.hello_world", :auto_delete => true)
@exchange = channel.default_exchange
queue.subscribe do |payload|
puts "Received a message: #{payload}."
end
end
puts "Blocked like a boss"
end
end
while not ready?
sleep 0.25
end
thread
end
end
end
# In something like config/initializers/queue_daemon.rb
t = QueueDaemon.start!
9.times do |i|
QueueDaemon.exchange.publish "Hello, world #{i}!", :routing_key => "amqpgem.examples.hello_world", :app_id => "Hello world"
end
t.join
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment