class Bunny::Consumer

Base class that represents consumer interface. Subclasses of this class implement specific logic of handling consumer life cycle events. Note that when the only event you are interested in is message deliveries, it is recommended to just use {Bunny::Queue#subscribe} instead of subclassing this class.

@see Bunny::Queue#subscribe @see Bunny::Queue#subscribe_with @see rubybunny.info/articles/queues.html Queues and Consumers guide @api public

Attributes

arguments[R]
channel[R]

API

consumer_tag[RW]
exclusive[R]
no_ack[R]
queue[R]

Public Class Methods

new(channel, queue, consumer_tag = channel.generate_consumer_tag, no_ack = true, exclusive = false, arguments = {}) click to toggle source

@param [Bunny::Channel] channel Channel this consumer will use @param [Bunny::Queue,String] queue Queue messages will be consumed from @param [String] consumer_tag Consumer tag (unique identifier). Generally it is better to let Bunny generate one.

Empty string means RabbitMQ will generate consumer tag.

@param [Boolean] no_ack (true) If true, delivered messages will be automatically acknowledged.

If false, manual acknowledgements will be necessary.

@param [Boolean] exclusive (false) Should this consumer be exclusive? @param [Hash] arguments (nil) Optional arguments that may be used by RabbitMQ extensions, etc @api public

# File lib/bunny/consumer.rb, line 34
def initialize(channel, queue, consumer_tag = channel.generate_consumer_tag, no_ack = true, exclusive = false, arguments = {})
  @channel       = channel || raise(ArgumentError, "channel is nil")
  @queue         = queue   || raise(ArgumentError, "queue is nil")
  @consumer_tag  = consumer_tag
  @exclusive     = exclusive
  @arguments     = arguments
  # no_ack set to true = no manual ack = automatic ack. MK.
  @no_ack        = no_ack

  @on_cancellation = []
end

Public Instance Methods

automatic_acknowledgement?() click to toggle source

@return [Boolean] true if this consumer uses automatic acknowledgement mode @api public

# File lib/bunny/consumer.rb, line 99
def automatic_acknowledgement?
  @no_ack == true
end
call(*args) click to toggle source

Invokes message delivery handler @private

# File lib/bunny/consumer.rb, line 55
def call(*args)
  @on_delivery.call(*args) if @on_delivery
end
Also aliased as: handle_delivery
cancel() click to toggle source

Cancels this consumer. Messages for this consumer will no longer be delivered. If the queue it was on is auto-deleted and this consumer was the last one, the queue will be deleted.

@see rubybunny.info/articles/queues.html Queues and Consumers guide @api public

# File lib/bunny/consumer.rb, line 83
def cancel
  @channel.basic_cancel(@consumer_tag)
end
handle_cancellation(basic_cancel) click to toggle source

Invokes consumer cancellation notification handler @private

# File lib/bunny/consumer.rb, line 72
def handle_cancellation(basic_cancel)
  @on_cancellation.each do |fn|
    fn.call(basic_cancel)
  end
end
handle_delivery(*args)
Alias for: call
inspect() click to toggle source

@return [String] More detailed human-readable string representation of this consumer

# File lib/bunny/consumer.rb, line 88
def inspect
  "#<#{self.class.name}:#{object_id} @channel_id=#{@channel.number} @queue=#{self.queue_name}> @consumer_tag=#{@consumer_tag} @exclusive=#{@exclusive} @no_ack=#{@no_ack}>"
end
manual_acknowledgement?() click to toggle source

@return [Boolean] true if this consumer uses manual (explicit) acknowledgement mode @api public

# File lib/bunny/consumer.rb, line 105
def manual_acknowledgement?
  @no_ack == false
end
on_cancellation(&block) click to toggle source

Defines consumer cancellation notification handler

@see rubybunny.info/articles/queues.html Queues and Consumers guide @see rubybunny.info/articles/extensions.html RabbitMQ Extensions guide @api public

# File lib/bunny/consumer.rb, line 65
def on_cancellation(&block)
  @on_cancellation << block
  self
end
on_delivery(&block) click to toggle source

Defines message delivery handler @api public

# File lib/bunny/consumer.rb, line 48
def on_delivery(&block)
  @on_delivery = block
  self
end
queue_name() click to toggle source

@return [String] Name of the queue this consumer is on @api public

# File lib/bunny/consumer.rb, line 111
def queue_name
  if @queue.respond_to?(:name)
    @queue.name
  else
    @queue
  end
end
recover_from_network_failure() click to toggle source

@private

# File lib/bunny/consumer.rb, line 124
def recover_from_network_failure
  @channel.basic_consume_with(self)
end
to_s() click to toggle source

@return [String] Brief human-readable string representation of this consumer

# File lib/bunny/consumer.rb, line 93
def to_s
  "#<#{self.class.name}:#{object_id} @channel_id=#{@channel.number} @queue=#{self.queue_name}> @consumer_tag=#{@consumer_tag}>"
end