Created
August 19, 2013 07:46
-
-
Save maxdemarzi/6266599 to your computer and use it in GitHub Desktop.
Rabbit
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 'bunny' | |
connection = Bunny.new | |
connection.start | |
channel = connection.create_channel | |
channel.prefetch(3) | |
exchange = channel.direct("moar") | |
cnt = 0 | |
messages = [] | |
queue = channel.queue("", :durable => true) | |
queue.purge #just for this test | |
queue.bind(exchange).subscribe(:ack => true, :block => false) do |delivery_info, metadata, payload| | |
# Display message count | |
puts queue.message_count | |
puts "Got the #{payload}" | |
messages << payload | |
cnt +=1 | |
if cnt >= 3 || queue.message_count == 0 | |
messages.each do |message| | |
puts "Added #{message} to transaction" | |
end | |
puts "Batched #{messages.size}:" | |
channel.acknowledge(delivery_info.delivery_tag, true) #true acknowledges all 3 messages | |
cnt = 0 | |
messages = [] | |
end | |
end | |
20.times do |t| | |
exchange.publish("Hello #{t}!") | |
end | |
sleep 1.0 | |
connection.close | |
#The problem with this method is that the 10th message gets stuck waiting in a queue. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment