Advertisement
Guest User

Untitled

a guest
Feb 2nd, 2017
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.38 KB | None | 0 0
  1. require "bunny"
  2. require "json"
  3. require "securerandom"
  4.  
  5. QUEUE = "appId:queue"
  6. VHOST = 'shared'
  7.  
  8. def consume
  9. consumer_conn = Bunny.new(
  10. hostname: "us-east-1-a-queue.ably.io",
  11. username: 'app123.key',
  12. password: 'password',
  13. vhost: VHOST
  14. )
  15.  
  16. consumer_conn.start
  17. # Specifying durable is important as this needs to match the config of the queue which is durable when manually set up
  18. # But this does not work with this permission set as it tries to define a queu
  19. # consumer_queue = consumer_conn.create_channel.queue(QUEUE, durable: true)
  20. # consumer_queue.subscribe do |delivery_info, properties, content|
  21. # instead we use a basic consume client which we will need to tell customers to use
  22. channel = consumer_conn.create_channel
  23.  
  24. puts "Consuming channel #{QUEUE}..."
  25.  
  26. # arguments queue name, customer consumer tag if wanted, no_ack (false requires manual ACK)
  27. raise "Queue #{QUEUE} does not exist" unless consumer_conn.queue_exists?(QUEUE)
  28. channel.basic_consume(QUEUE, nil, false) do |delivery_info, properties, payload|
  29. puts " [x] consumed message: #{payload}, delivery tag: #{delivery_info.delivery_tag.to_i}"
  30. if rand(10) < 5
  31. channel.basic_ack(delivery_info.delivery_tag, false)
  32. else
  33. puts " [ ] Nacking #{payload}. Will be re-enqueued"
  34. channel.basic_nack(delivery_info.delivery_tag, false, true)
  35. end
  36. end
  37.  
  38. while true
  39. sleep 1
  40. end
  41. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement