How do I get old messages from RabbitMQ?

房东的猫 提交于 2019-12-08 13:27:12

问题


I'm publishing RabbitMQ messages using Bunny (Ruby) like this:

x.publish("Message !"+n.to_s, :routing_key => 'mychannel')

and subscribing like this:

    ch   = conn.create_channel
x    = ch.topic('fling',durable: true)
q    = ch.queue("")
q.bind(x, :routing_key => 'mychannel')


puts "Waiting for messages."
q.subscribe( :block => true) do |delivery_info, properties, body|
puts " [x] Received #{body}, message properties are #{properties.inspect}"

Once I start the subscriber, it immediately receives any messages which are sent. However, if I send messages without starting the subscriber, they aren't received when I start the subscriber (whether the sender is still pushing messages, or not).

Is it possible to go back through the queue and receive messages that were sent in the past, when no subscribers were listening?


回答1:


You're making a new queue each time you start the consumer! So when you restart the consumer, the new queue gets new messages, but doesn't have previous ones.

Do this:

q    = ch.queue("myqueue",durable: true)

instead of this:

q    = ch.queue("")

Then, as soon as you restart the consumer, it will immediately get all backed-up messages as fast as it can.



来源:https://stackoverflow.com/questions/33546568/how-do-i-get-old-messages-from-rabbitmq

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!