Parent

Class/Module Index [+]

Quicksearch

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 (false) If false, delivered messages will be automatically acknowledged.

If true, 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        = no_ack
end

Public Instance Methods

call(*args) click to toggle source

Invokes message delivery handler @private

# File lib/bunny/consumer.rb, line 52
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 78
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 69
def handle_cancellation(basic_cancel)
  @on_cancellation.call(basic_cancel) if @on_cancellation
end
handle_delivery(*args) click to toggle source
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 83
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
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 62
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 45
def on_delivery(&block)
  @on_delivery = block
  self
end
queue_name() click to toggle source

@private

# File lib/bunny/consumer.rb, line 102
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 97
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 88
def to_s
  "#<#{self.class.name}:#{object_id} @channel_id=#{@channel.number} @queue=#{self.queue_name}> @consumer_tag=#{@consumer_tag}>"
end

[Validate]

Generated with the Darkfish Rdoc Generator 2.