Module: AMQP::Boilerplate::Producer

Defined in:
lib/amqp/boilerplate/producer.rb

Overview

Use this module to turn a class into a potential producer that can deliver messages to an AMQP exchange.

You turn your class into a producer by extending the module and call the required macros #amqp and #amqp_message methods.

To specify exchange options you can call the optional macro #amqp_exchange method.

Examples:

Getting started

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

Configuring exchange

class MyProducer
  extend AMQP::Boilerplate::Producer

  amqp :routing_key => "hello.world"
  amqp_exchange :fanout, "amq.fanout", :durable => true
  amqp_message :message

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

Defined Under Namespace

Modules: InstanceMethods

Instance Method Summary collapse

Instance Method Details

#amqp(opts = {}) ⇒ void

This method returns an undefined value.

Macro that sets up amqp for a class.

Parameters:


40
41
42
43
# File 'lib/amqp/boilerplate/producer.rb', line 40

def amqp(opts={})
  send :include, InstanceMethods
  @amqp_boilerplate_options = opts
end

#amqp_boilerplate_exchangeObject

TODO Can we do this in a nicer way?


81
82
83
# File 'lib/amqp/boilerplate/producer.rb', line 81

def amqp_boilerplate_exchange
  @amqp_boilerplate_exchange || amqp_exchange
end

#amqp_boilerplate_messageObject

TODO Can we do this in a nicer way?


86
87
88
# File 'lib/amqp/boilerplate/producer.rb', line 86

def amqp_boilerplate_message
  @amqp_boilerplate_message
end

#amqp_boilerplate_optionsObject

TODO Can we do this in a nicer way?


76
77
78
# File 'lib/amqp/boilerplate/producer.rb', line 76

def amqp_boilerplate_options
  @amqp_boilerplate_options
end

#amqp_exchange(type = :direct, name = AMQ::Protocol::EMPTY_STRING, opts = {}) ⇒ void

This method returns an undefined value.

Configuration for the exchange to be used

Parameters:

  • type (Symbol) (defaults to: :direct)

    Exchange type

    There are 4 supported exchange types: direct, fanout, topic and headers. Exchange type determines how exchange processes and routes messages.

  • name (String) (defaults to: AMQ::Protocol::EMPTY_STRING)

    Exchange name

  • opts (Hash) (defaults to: {})

    a customizable set of options

See Also:

  • Exchange#initialize

54
55
56
# File 'lib/amqp/boilerplate/producer.rb', line 54

def amqp_exchange(type=:direct, name=AMQ::Protocol::EMPTY_STRING, opts={})
  @amqp_boilerplate_exchange = [type, name, opts]
end

#amqp_message(method_name) ⇒ void

This method returns an undefined value.

Choose the method that will return the actual message (payload) to be delivered to the exchange

This method SHOULD return a string.

Examples:

amqp_message :message

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

Parameters:

  • method_name (Symbol)

    Name of method that generates message


71
72
73
# File 'lib/amqp/boilerplate/producer.rb', line 71

def amqp_message(method_name)
  @amqp_boilerplate_message = method_name
end