Skip to content

Instantly share code, notes, and snippets.

@caius
Created June 24, 2016 10:55
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 caius/be1e8e5df7a381782a719272d4fce54e to your computer and use it in GitHub Desktop.
Save caius/be1e8e5df7a381782a719272d4fce54e to your computer and use it in GitHub Desktop.
RabbitMQ nack behaviour
"Got 0 (false)"
"Got 1 (false)"
"Got 2 (true)"
"Got 3 (false)"
"Got 4 (false)"
"Got 5 (false)"
"Got 6 (false)"
"Got 7 (false)"
"Got 8 (false)"
"Got 9 (false)"
"Got 10 (false)"
"Got 11 (false)"
"Got 12 (false)"
"Got 13 (false)"
"Got 14 (false)"
"Got 15 (false)"
"Got 2 (true)"
E, [2016-06-24T11:54:00.657081 #23195] ERROR -- #<Bunny::Session:0x7f8718b39bc8 guest@127.0.0.1:5672, vhost=/, addresses=[127.0.0.1:5672]>: Uncaught exception from consumer #<Bunny::Consumer:70109105835480 @channel_id=1 @queue=nack_tester> @consumer_tag=bunny-1466765638000-962307206775>: #<RuntimeError: Dup! Already seen this message> @ client.rb:10:in `block in <main>'
"Got 16 (false)"
"Got 17 (false)"
"Got 18 (false)"
"Got 19 (false)"
"Got 20 (false)"
"Got 21 (false)"
"Got 22 (false)"
"Got 23 (false)"
"Got 24 (false)"
"Got 25 (false)"
"Got 26 (false)"
"Got 27 (false)"
"Got 28 (false)"
"Got 29 (false)"
"Got 30 (false)"
"Got 31 (false)"
"Got 32 (false)"
"Got 33 (false)"
"Got 34 (false)"
"Got 35 (false)"
"Got 36 (false)"
"Got 37 (false)"
"Got 38 (false)"
"Got 39 (false)"
"Got 40 (false)"
"Got 41 (false)"
"Got 42 (false)"
"Got 43 (false)"
"Got 44 (false)"
"Got 45 (false)"
"Got 46 (false)"
"Got 47 (false)"
"Got 48 (false)"
"Got 49 (false)"
require_relative "./shared"
seen_ids = []
$q.subscribe(:block => true, :manual_ack => true) do |delivery_info, metadata, payload|
data = JSON.parse(payload)
p "Got #{data["id"]} (#{data["nack"]})"
if seen_ids.include?(data["id"])
raise "Dup! Already seen this message"
end
seen_ids << data["id"]
if data["nack"]
$channel.nack(delivery_info.delivery_tag, false, true)
else
$channel.ack(delivery_info.delivery_tag)
end
end
require_relative "./shared"
50.times do |i|
payload = {
:id => i,
:nack => (i == 2)
}
$x.publish(payload.to_json, :routing_key => $q.name)
end
$conn.close
require "bunny"
require "json"
$conn = Bunny.new
$conn.start
$channel = $conn.create_channel
$q = $channel.queue("nack_tester", :auto_delete => true)
$x = $channel.default_exchange
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment