Skip to content

Instantly share code, notes, and snippets.

@daixque daixque/consumer.rb
Created Feb 20, 2012

Embed
What would you like to do?
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
You can’t perform that action at this time.