Class: Bunny::Consumer
- Inherits:
-
Object
- Object
- Bunny::Consumer
- Defined in:
- lib/bunny/consumer.rb
Overview
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 Queue#subscribe instead of subclassing this class.
Instance Attribute Summary collapse
- #arguments ⇒ Object readonly
-
#channel ⇒ Object
readonly
API.
- #consumer_tag ⇒ Object
- #exclusive ⇒ Object readonly
- #no_ack ⇒ Object readonly
- #queue ⇒ Object readonly
Instance Method Summary collapse
-
#automatic_acknowledgement? ⇒ Boolean
True if this consumer uses automatic acknowledgement mode.
-
#call(*args) ⇒ Object
(also: #handle_delivery)
Invokes message delivery handler.
-
#cancel ⇒ Object
Cancels this consumer.
-
#handle_cancellation(basic_cancel) ⇒ Object
Invokes consumer cancellation notification handler.
-
#initialize(channel, queue, consumer_tag = channel.generate_consumer_tag, no_ack = true, exclusive = false, arguments = {}) ⇒ Consumer
constructor
A new instance of Consumer.
-
#inspect ⇒ String
More detailed human-readable string representation of this consumer.
-
#manual_acknowledgement? ⇒ Boolean
True if this consumer uses manual (explicit) acknowledgement mode.
-
#on_cancellation(&block) ⇒ Object
Defines consumer cancellation notification handler.
-
#on_delivery(&block) ⇒ Object
Defines message delivery handler.
-
#queue_name ⇒ String
Name of the queue this consumer is on.
- #recover_from_network_failure ⇒ Object
-
#to_s ⇒ String
Brief human-readable string representation of this consumer.
Constructor Details
#initialize(channel, queue, consumer_tag = channel.generate_consumer_tag, no_ack = true, exclusive = false, arguments = {}) ⇒ Consumer
Returns a new instance of Consumer.
34 35 36 37 38 39 40 41 42 43 44 |
# 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 |
Instance Attribute Details
#arguments ⇒ Object (readonly)
20 21 22 |
# File 'lib/bunny/consumer.rb', line 20 def arguments @arguments end |
#channel ⇒ Object (readonly)
API
17 18 19 |
# File 'lib/bunny/consumer.rb', line 17 def channel @channel end |
#consumer_tag ⇒ Object
19 20 21 |
# File 'lib/bunny/consumer.rb', line 19 def consumer_tag @consumer_tag end |
#exclusive ⇒ Object (readonly)
22 23 24 |
# File 'lib/bunny/consumer.rb', line 22 def exclusive @exclusive end |
#no_ack ⇒ Object (readonly)
21 22 23 |
# File 'lib/bunny/consumer.rb', line 21 def no_ack @no_ack end |
#queue ⇒ Object (readonly)
18 19 20 |
# File 'lib/bunny/consumer.rb', line 18 def queue @queue end |
Instance Method Details
#automatic_acknowledgement? ⇒ Boolean
Returns true if this consumer uses automatic acknowledgement mode.
99 100 101 |
# File 'lib/bunny/consumer.rb', line 99 def automatic_acknowledgement? @no_ack == true end |
#call(*args) ⇒ Object Also known as: handle_delivery
Invokes message delivery handler
55 56 57 |
# File 'lib/bunny/consumer.rb', line 55 def call(*args) @on_delivery.call(*args) if @on_delivery end |
#cancel ⇒ Object
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.
83 84 85 |
# File 'lib/bunny/consumer.rb', line 83 def cancel @channel.basic_cancel(@consumer_tag) end |
#handle_cancellation(basic_cancel) ⇒ Object
Invokes consumer cancellation notification handler
72 73 74 75 76 |
# File 'lib/bunny/consumer.rb', line 72 def handle_cancellation(basic_cancel) @on_cancellation.each do |fn| fn.call(basic_cancel) end end |
#inspect ⇒ String
Returns More detailed human-readable string representation of this consumer.
88 89 90 |
# 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? ⇒ Boolean
Returns true if this consumer uses manual (explicit) acknowledgement mode.
105 106 107 |
# File 'lib/bunny/consumer.rb', line 105 def manual_acknowledgement? @no_ack == false end |
#on_cancellation(&block) ⇒ Object
Defines consumer cancellation notification handler
65 66 67 68 |
# File 'lib/bunny/consumer.rb', line 65 def on_cancellation(&block) @on_cancellation << block self end |
#on_delivery(&block) ⇒ Object
Defines message delivery handler
48 49 50 51 |
# File 'lib/bunny/consumer.rb', line 48 def on_delivery(&block) @on_delivery = block self end |
#queue_name ⇒ String
Returns Name of the queue this consumer is on.
111 112 113 114 115 116 117 |
# 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 ⇒ Object
124 125 126 |
# File 'lib/bunny/consumer.rb', line 124 def recover_from_network_failure @channel.basic_consume_with(self) end |
#to_s ⇒ String
Returns Brief human-readable string representation of this consumer.
93 94 95 |
# 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 |