amqp-boilerplate

amqp-boilerplate is a set of helper classes and modules to be used with the Ruby amqp gem.

Install

gem install amqp-boilerplate

Configuration

See AMQP::Boilerplate.configure for configuration options.

Ruby on Rails

Add a initializer amqp.rb to your config/initializer folder with the following code:

Rails.configuration.threadsafe!

AMQP::Boilerplate.configure do |config|
  config.logger = ::Rails.logger
  config.consumer_paths += %W( #{Rails.root}/app/consumers )
  config.connection_options = { :host => "localhost", :port => 5672, :vhost => Rails.env }
  config.on_unhandled_exception = Proc.new { |exception, consumer, , payload|
    puts "Do something with exceptions: #{exception}"
  }
  config.consumer_prefetch = 10
end

# Require all files that are no longer auto-loaded when Rails is in thread-safe mode
Dir[File.expand_path(File.join(Rails.root,'lib','**','*.rb'))].each {|f| require f}
Dir[File.expand_path(File.join(Rails.root,'app','producers','**','*.rb'))].each {|f| require f}

AMQP::Boilerplate.boot

Usage

amqp-boilerplate provides the AMQP::Boilerplate::Producer module for creating message producers, and the AMQP::Boilerplate::Consumer class for setting up a message consumer.

Producers

The following sample code shows a basic producer.

class MyProducer
  extend AMQP::Boilerplate::Producer

  amqp :routing_key => "hello.world"
  amqp_message :message

  def message
    "Look! I am a string that will be posted to the exchange."
  end
end

For more information please refer to AMQP::Boilerplate::Producer

Consumers

The following sample code shows a basic consumer.

class MyConsumer < AMQP::Boilerplate::Consumer
  amqp_queue "hello.world"

  def handle_message(payload, )
    puts "Received message: #{payload}"
  end
end

For more information please refer to AMQP::Boilerplate::Consumer

License

amqp-boilerplate is released under the MIT license.