Created
February 20, 2012 09:32
-
-
Save daixque/1868565 to your computer and use it in GitHub Desktop.
Sample AMQP consumer/producer for RabbitMQ with direct exchange
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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