Skip to content

Instantly share code, notes, and snippets.

@daixque
Created February 20, 2012 09:32
Show Gist options
  • Save daixque/1868565 to your computer and use it in GitHub Desktop.
Save daixque/1868565 to your computer and use it in GitHub Desktop.
Sample AMQP consumer/producer for RabbitMQ with direct exchange
require 'amqp'
def run
config = {
:host => 'localhost'
}
AMQP.start(config) do |connection|
channel = AMQP::Channel.new(connection)
queue = channel.queue('', :auto_delete => true)
exchange = channel.direct 'ex.direct'
queue.bind(exchange, :routing_key => 'tasks').subscribe do |headers, payload|
puts payload
end
stopper = Proc.new { connection.close { EventMachine.stop } }
Signal.trap "INT", stopper
end
end
run
require 'amqp'
def run
config = {
:host => 'localhost'
}
AMQP.start(config) do |connection, open_ok|
channel = AMQP::Channel.new(connection)
exchange = channel.direct 'ex.direct'
msg = 'Hello, world'
exchange.publish(msg, :routing_key => 'tasks') do
puts "sent: #{msg}"
connection.close { EventMachine.stop }
end
end
end
run
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment