Wayne Conrad's Blog

home

A few tips for using bunny library with RabbitMQ

05 Aug 2015

Some things I want to remember about using the bunny library to publish and read messages from a RabbitMQ server.

No blocking read

The bunny library does not have a blocking read, so you’ll have to synthesize it yourself:

# Returns delivery_info, properties, body
def synchronous_get
  loop do
    a = @channel.basic_get(...)
    return a unless a.first.nil?
    sleep(4)
  end
end

Publishing messages reliably

If you need to be sure that a message was accepted, you’ll have to jump through some hoops:

Set the channel into “confirm select” mode:

@channel.confirm_select

Register a callback that will be called if the message is returned:

@exchange.on_return do
  @returned = true
end

Now, to publish a message:

@returned = false
@exchange.publish(..., persistent: true)
@channel.wait_for_confirms
if @returned
  raise RoutingError, "Message could not be routed"
end
comments powered by Disqus