2014-02-14 177 views
1

我正在使用RabbitMq进行通信,并且我想只消费一条消息并取消订阅。如何在红宝石兔子呢?我的订阅块是很容易的:从rabbitmq队列中获取一条消息并取消订阅

queue.subscribe(block: true) do |delivery_info, properties, payload| 
    puts "[consumer] #{q.name} received a message: #{payload}" 
    end 

回答

1

你可能已经想通了现在,但对于其他人...

按照documentation,你可以只使用basic_get。例如,

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) 
0

的另一种方式,可以使用队列的#pop方法。

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