Class: RabbitMQ::Actors::RoutingConsumer Abstract

Inherits:
Base::Consumer show all
Defined in:
lib/rabbitmq/actors/patterns/routing/routing_consumer.rb

Overview

This class is abstract.

Subclass and override #perform to define your customized routing worker class.

A consumer of messages from RabbitMQ based on routing keys.

Examples:

class TennisListener < RabbitMQ::Actors::RoutingConsumer
  def initialize
    super(exchange_name:   'sports',
          binding_keys:    ['tennis'],
          logger:          Rails.logger,
          on_cancellation: ->{ ActiveRecord::Base.connection.close })
  end

  private

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

class FootballListener < RabbitMQ::Actors::RoutingConsumer
  def initialize
    super(exchange_name:   'sports',
          binding_keys:    ['football', 'soccer'],
          logger:          Rails.logger,
          on_cancellation: ->{ ActiveRecord::Base.connection.close })
  end

  private

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

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

TennisListener.new.start!
FootballListener.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:, binding_keys: '#', **opts) ⇒ RoutingConsumer

Rest of options required by your subclass.

Parameters:

  • :exchange_name (String)

    name of the exchange where to consume messages from.

  • :binding_keys (String, Array)

    list of routing keys this worker is interested in. Default to all: ‘#’

  • 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.



63
64
65
# File 'lib/rabbitmq/actors/patterns/routing/routing_consumer.rb', line 63

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

Instance Attribute Details

#binding_keysObject (readonly)

Returns the value of attribute binding_keys.



55
56
57
# File 'lib/rabbitmq/actors/patterns/routing/routing_consumer.rb', line 55

def binding_keys
  @binding_keys
end

#exchange_nameObject (readonly)

Returns the value of attribute exchange_name.



51
52
53
# File 'lib/rabbitmq/actors/patterns/routing/routing_consumer.rb', line 51

def exchange_name
  @exchange_name
end