1

I am using RabbitMq for communication, and I would like to consume just one message and unsubscribe. How to do it in ruby bunny? My subscribe block is pretty easy:

  queue.subscribe(block: true) do |delivery_info, properties, payload|
    puts "[consumer] #{q.name} received a message: #{payload}"
  end
Sławosz
  • 11,187
  • 15
  • 73
  • 106

2 Answers2

1

You probably already figured it out by now, but for anyone else...

According to the documentation, you can just use basic_get. For example,

conn = Bunny.new
conn.start
ch = conn.create_channel
delivery_info, properties, message = ch.basic_get("your_queue_name", :ack => true)
ch.acknowledge(delivery_info.delivery_tag)
Ray C.
  • 11
  • 1
  • 1
0

Another way could be using #pop method of queue.

conn = Bunny.new
conn.start

ch   = conn.create_channel
q = ch.queue("queue_name")
delivery_info, properties, payload = q.pop

You can find more details about it here

Ashik Salman
  • 1,819
  • 11
  • 15