Class: Bunny::Consumer

Inherits:
Object
  • Object
show all
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

Instance Method Summary collapse

Constructor Details

#initialize(channel, queue, consumer_tag = channel.generate_consumer_tag, no_ack = true, exclusive = false, arguments = {}) ⇒ Consumer

Returns a new instance of Consumer.



36
37
38
39
40
41
42
43
44
45
46
# 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

Instance Attribute Details

#argumentsObject (readonly)



22
23
24
# File 'lib/bunny/consumer.rb', line 22

def arguments
  @arguments
end

#channelObject (readonly)

API



19
20
21
# File 'lib/bunny/consumer.rb', line 19

def channel
  @channel
end

#consumer_tagObject



21
22
23
# File 'lib/bunny/consumer.rb', line 21

def consumer_tag
  @consumer_tag
end

#exclusiveObject (readonly)



24
25
26
# File 'lib/bunny/consumer.rb', line 24

def exclusive
  @exclusive
end

#no_ackObject (readonly)



23
24
25
# File 'lib/bunny/consumer.rb', line 23

def no_ack
  @no_ack
end

#queueObject (readonly)



20
21
22
# File 'lib/bunny/consumer.rb', line 20

def queue
  @queue
end

Instance Method Details

#automatic_acknowledgement?Boolean

Returns true if this consumer uses automatic acknowledgement mode.



101
102
103
# File 'lib/bunny/consumer.rb', line 101

def automatic_acknowledgement?
  @no_ack
end

#cancelObject

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.



85
86
87
# File 'lib/bunny/consumer.rb', line 85

def cancel
  @channel.basic_cancel(@consumer_tag)
end

#inspectString

Returns More detailed human-readable string representation of this consumer.



90
91
92
# 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

#manual_acknowledgement?Boolean

Returns true if this consumer uses manual (explicit) acknowledgement mode.



107
108
109
# File 'lib/bunny/consumer.rb', line 107

def manual_acknowledgement?
  !@no_ack
end

#on_cancellation(&block) ⇒ Object

Defines consumer cancellation notification handler



67
68
69
70
# File 'lib/bunny/consumer.rb', line 67

def on_cancellation(&block)
  @on_cancellation << block
  self
end

#on_delivery(&block) ⇒ Object

Defines message delivery handler



50
51
52
53
# File 'lib/bunny/consumer.rb', line 50

def on_delivery(&block)
  @on_delivery = block
  self
end

#queue_nameString

Returns Name of the queue this consumer is on.



113
114
115
116
117
118
119
# File 'lib/bunny/consumer.rb', line 113

def queue_name
  if @queue.respond_to?(:name)
    @queue.name
  else
    @queue
  end
end

#to_sString

Returns Brief human-readable string representation of this consumer.



95
96
97
# 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