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
API
Public Class Methods
Source
# File lib/bunny/consumer.rb, line 36 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
@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
Public Instance Methods
Source
# File lib/bunny/consumer.rb, line 101 def automatic_acknowledgement? @no_ack == true end
@return [Boolean] true if this consumer uses automatic acknowledgement mode @api public
Source
# File lib/bunny/consumer.rb, line 57 def call(*args) @on_delivery.call(*args) if @on_delivery end
Invokes message delivery handler @private
Source
# File lib/bunny/consumer.rb, line 85 def cancel @channel.basic_cancel(@consumer_tag) end
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
Source
# File lib/bunny/consumer.rb, line 74 def handle_cancellation(basic_cancel) @on_cancellation.each do |fn| fn.call(basic_cancel) end end
Invokes consumer cancellation notification handler @private
Source
# File lib/bunny/consumer.rb, line 90 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
@return [String] More detailed human-readable string representation of this consumer
Source
# File lib/bunny/consumer.rb, line 107 def manual_acknowledgement? @no_ack == false end
@return [Boolean] true if this consumer uses manual (explicit) acknowledgement mode @api public
Source
# File lib/bunny/consumer.rb, line 67 def on_cancellation(&block) @on_cancellation << block self end
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
Source
# File lib/bunny/consumer.rb, line 50 def on_delivery(&block) @on_delivery = block self end
Defines message delivery handler @api public
Source
# File lib/bunny/consumer.rb, line 113 def queue_name if @queue.respond_to?(:name) @queue.name else @queue end end
@return [String] Name of the queue this consumer is on @api public
Source
# File lib/bunny/consumer.rb, line 126 def recover_from_network_failure @channel.basic_consume_with(self) end
@private
Source
# File lib/bunny/consumer.rb, line 95 def to_s "#<#{self.class.name}:#{object_id} @channel_id=#{@channel.number} @queue=#{self.queue_name} @consumer_tag=#{@consumer_tag}>" end
@return [String] Brief human-readable string representation of this consumer