Class: RabbitMQ::Actors::Base::Agent Abstract

Inherits:
Object
  • Object
show all
Defined in:
lib/rabbitmq/actors/base/agent.rb

Overview

This class is abstract.

Subclass and override #pre_initialize and/or #post_initialize to define actual agent classes.

The base class for RabbitMQ producers, workers, publishers, consumers…

Examples:

module RabbitMQ
  module Actors
    module Base

      class Producer < Agent
        def post_initialize(queue_name:, opts = {})
          set_reply_queue(opts[:reply_queue_name])
        end

        def publish(message, correlation_key: true)
          options = { routing_key: queue.name, durable: durable }
          options.merge!(reply_to: reply_queue.name)           if reply_queue
          options.merge!(correlation_id: SecureRandom.hex(10)) if correlation_key
          exchange.publish(message, options)
        end

        def close!
          channel.close
        end
      end
    end
  end
end

Direct Known Subclasses

Consumer, Producer

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**opts) ⇒ Agent

Instantiate a new agent. #pre_initialize and #post_initialize methods are called just at the beginning and end respectively. Redefine them in your subclass to complete your subclass initialization process.

Rest of options required by your subclasses.

Parameters:

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :queue_name (String)

    the queue name to bind the agent with if any.

  • :exclusive (Boolean) — default: true

    if the queue can only be used by this agent and removed when this agent’s connection is closed.

  • :auto_delete (Boolean) — default: true

    if the queue will be deleted when there are no more consumers subscribed to it.

  • :logger (Logger)

    the logger where to output info about agent’s activity.



49
50
51
52
53
54
# File 'lib/rabbitmq/actors/base/agent.rb', line 49

def initialize(**opts)
  pre_initialize(**opts)
  set_queue(opts[:queue_name], **opts) if opts[:queue_name]
  set_logger(opts[:logger])
  post_initialize(**opts)
end

Instance Attribute Details

#queueObject (readonly)

Returns the value of attribute queue.



35
36
37
# File 'lib/rabbitmq/actors/base/agent.rb', line 35

def queue
  @queue
end