Eki Eqbal
Eki Eqbal

Reputation: 6027

Resend the request to the same queue in RabbitMQ and Ruby

I have a ruby process to consume RabbitMQ queue :

AMQP.start(:host => $AMQP_URL) do |connection|
  @channel ||= AMQP::Channel.new(connection)
  @queue   ||= @channel.queue("results")

  @queue.subscribe do |body|
    puts "Received -> #{body}"       
    # Publish the same message again to the same queue 
  end
end

I know it is not practical, but I would love to know how am I supposed to publish the same message to the same queue, it didn't work out for me with direct channel, if there is anyways even just to keep the msg in the queue instead of deleting it or just to republish the msg again it would be great

Any ideas ?

Upvotes: 0

Views: 519

Answers (1)

Loris Guignard
Loris Guignard

Reputation: 151

The proper way to do it would just be to reject the message with a negative acknowledgement, it will be automatically requeued:

@queue.subscribe do |metadata, payload|
  # reject and requeue
  channel.reject(metadata.delivery_tag, true)
end

Anyway, if you want to do the publish manually, the "metadata" parameter in the previous example will give you all the information you need.

Upvotes: 1

Related Questions