Skip to content

Instantly share code, notes, and snippets.

@celldee
Last active December 10, 2015 22:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save celldee/4500579 to your computer and use it in GitHub Desktop.
Save celldee/4500579 to your computer and use it in GitHub Desktop.
Bunny 0.9.0 consumer and publisher to test publisher confirms
Publisher
---------
require 'bunny'
conn = Bunny.new
conn.start
ch = conn.channel
ch.confirm_select
x = ch.direct('hello-exchange', :durable => true)
msg = ARGV[0]
100.times do |i|
x.publish("#{i}: #{msg}", :routing_key => 'hola')
end
ch.wait_for_confirms
#sleep 10
conn.close # Without this close method there is a risk of lost messages as at 0.9.0.pre5
--------
Consumer
--------
require 'bunny'
class HelloConsumer < Bunny::Consumer
end
conn = Bunny.new
conn.start
ch = conn.channel
x = ch.direct('hello-exchange', :durable => true)
q = ch.queue('hello-queue', :durable => true)
q.bind(x, :routing_key => 'hola')
consumer = HelloConsumer.new(ch, q, 'hello-consumer')
consumer.on_delivery do |delivery_info, properties, payload|
if payload == 'quit'
ch.work_pool.shutdown
else
puts payload
end
end
q.subscribe_with(consumer, :block => true)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment