Class: RabbitMQ::Actors::Subscriber Abstract

Inherits:
Base::Consumer show all
Defined in:
lib/rabbitmq/actors/patterns/publish_subscribe/subscriber.rb

Overview

This class is abstract.

Subclass and override #perform to define your customized subscriber class.

A consumer of all messages produced by a fanout RabbitMQ exchange.

Examples:

class ScoresListener < RabbitMQ::Actors::Subscriber
  def initialize
    super(exchange_name:   'scores',
          logger:          Rails.logger,
          on_cancellation: ->{ ActiveRecord::Base.connection.close })
  end

  private

  def perform(**task)
    match_data = JSON.parse(task[:body])
    process_match(match_data)
  end
  ...
end

RabbitMQ::Server.url = 'amqp://localhost'

ScoresListener.new.start!

Instance Attribute Summary collapse

Attributes inherited from Base::Agent

#queue

Instance Method Summary collapse

Methods inherited from Base::Consumer

#start!

Constructor Details

#initialize(exchange_name:, **opts) ⇒ Subscriber

Rest of options required by your subclass.

Parameters:

  • :exchange_name (String)

    name of the exchange where to consume messages from.

  • opts (Hash)

    a customizable set of options

Options Hash (**opts):

  • :on_cancellation (Proc)

    to be executed before the worker is terminated

  • :logger (Logger)

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



38
39
40
# File 'lib/rabbitmq/actors/patterns/publish_subscribe/subscriber.rb', line 38

def initialize(exchange_name:, **opts)
  super(opts.merge(exchange_name: exchange_name))
end

Instance Attribute Details

#exchange_nameObject (readonly)

Returns the value of attribute exchange_name.



32
33
34
# File 'lib/rabbitmq/actors/patterns/publish_subscribe/subscriber.rb', line 32

def exchange_name
  @exchange_name
end