Skip to content

Instantly share code, notes, and snippets.

@timblair
Last active August 29, 2015 14:16
Show Gist options
  • Save timblair/64d531e3f3b13d29c302 to your computer and use it in GitHub Desktop.
Save timblair/64d531e3f3b13d29c302 to your computer and use it in GitHub Desktop.
Simple RabbitMQ message listener
#!/usr/bin/env ruby
require "bunny"
abort "Usage: #{$0} BINDING_KEY [BINDING_KEY, ...]" if ARGV.empty?
conn = Bunny.new("amqp://development.vm:5672")
conn.start
channel = conn.create_channel
exchange = channel.exchange("event", no_declare: true)
queue = channel.queue("", exclusive: true, auto_delete: true)
ARGV.each { |key| queue.bind(exchange, :routing_key => key) }
begin
queue.subscribe(:block => true) do |delivery_info, properties, body|
puts "#{delivery_info.routing_key} [#{properties}]: #{body}"
end
rescue Interrupt => _
channel.close
conn.close
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment