Louise
Louise

Reputation: 1185

How do I get old messages from RabbitMQ?

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?

Upvotes: 5

Views: 8635

Answers (2)

Krish Chary
Krish Chary

Reputation: 109

Queue has to be parameter called durable its never losses

ch.queue(queue, {
       durable: true
     });
instead 
q    = ch.queue("")

Upvotes: 0

Louise
Louise

Reputation: 1185

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.

Upvotes: 9

Related Questions