Class: GorgonBunny::Consumer

Inherits:
Object
  • Object
show all
Defined in:
lib/gorgon_bunny/lib/gorgon_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.

Parameters:

  • channel (GorgonBunny::Channel)

    Channel this consumer will use

  • queue (GorgonBunny::Queue, String)

    Queue messages will be consumed from

  • consumer_tag (String) (defaults to: channel.generate_consumer_tag)

    Consumer tag (unique identifier). Generally it is better to let GorgonBunny generate one. Empty string means RabbitMQ will generate consumer tag.

  • no_ack (Boolean) (defaults to: true)

    (true) If true, delivered messages will be automatically acknowledged. If false, manual acknowledgements will be necessary.

  • exclusive (Boolean) (defaults to: false)

    (false) Should this consumer be exclusive?

  • arguments (Hash) (defaults to: {})

    (nil) Optional arguments that may be used by RabbitMQ extensions, etc



34
35
36
37
38
39
40
41
42
# File 'lib/gorgon_bunny/lib/gorgon_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
end

Instance Attribute Details

#argumentsObject (readonly)



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

def arguments
  @arguments
end

#channelObject (readonly)

API



17
18
19
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 17

def channel
  @channel
end

#consumer_tagObject



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

def consumer_tag
  @consumer_tag
end

#exclusiveObject (readonly)



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

def exclusive
  @exclusive
end

#no_ackObject (readonly)



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

def no_ack
  @no_ack
end

#queueObject (readonly)



18
19
20
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 18

def queue
  @queue
end

Instance Method Details

#automatic_acknowledgement?Boolean

Returns true if this consumer uses automatic acknowledgement mode.

Returns:

  • (Boolean)

    true if this consumer uses automatic acknowledgement mode



95
96
97
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 95

def automatic_acknowledgement?
  @no_ack == true
end

#call(*args) ⇒ Object Also known as: handle_delivery

Invokes message delivery handler



53
54
55
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 53

def call(*args)
  @on_delivery.call(*args) if @on_delivery
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.



79
80
81
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 79

def cancel
  @channel.basic_cancel(@consumer_tag)
end

#handle_cancellation(basic_cancel) ⇒ Object

Invokes consumer cancellation notification handler



70
71
72
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 70

def handle_cancellation(basic_cancel)
  @on_cancellation.call(basic_cancel) if @on_cancellation
end

#inspectString

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

Returns:

  • (String)

    More detailed human-readable string representation of this consumer



84
85
86
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 84

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.

Returns:

  • (Boolean)

    true if this consumer uses manual (explicit) acknowledgement mode



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

def manual_acknowledgement?
  @no_ack == false
end

#on_cancellation(&block) ⇒ Object

Defines consumer cancellation notification handler



63
64
65
66
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 63

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

#on_delivery(&block) ⇒ Object

Defines message delivery handler



46
47
48
49
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 46

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

#queue_nameObject



115
116
117
118
119
120
121
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 115

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

#recover_from_network_failureObject



110
111
112
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 110

def recover_from_network_failure
  @channel.basic_consume_with(self)
end

#to_sString

Returns Brief human-readable string representation of this consumer.

Returns:

  • (String)

    Brief human-readable string representation of this consumer



89
90
91
# File 'lib/gorgon_bunny/lib/gorgon_bunny/consumer.rb', line 89

def to_s
  "#<#{self.class.name}:#{object_id} @channel_id=#{@channel.number} @queue=#{self.queue_name}> @consumer_tag=#{@consumer_tag}>"
end