DZone Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world

Eric has posted 2 posts at DZone. View Full User Profile

AMQP Custom Headers In Ruby Client (tmm1-amqp)

11.29.2009
| 5153 views |
  • submit to reddit
        Want to send custom headers with your AMQP Basic messages?  The protocol mentions a "headers" field of type "table", which is where these get stuffed.  But how is this implemented in the Ruby client?

Publishing is as simple as this:

my_exchange.publish( my_message, :headers => { my_header => "foo" } )

Then on the subscribing end, the headers are accessible as an array of key-value pairs:

my_queue.subscribe do |hdr, body|
    pair = hdr.headers.find {|k, v| k == :my_header}
    my_header_value = (pair ? pair[1] : nil)
end

Despite Aman's generally excellent documentation of his AMQP client (http://github.com/tmm1/amqp), I couldn't find this without hunting through the code.

Eric