Class: Bunny::Queue
- Inherits:
-
Object
- Object
- Bunny::Queue
- Defined in:
- lib/bunny/queue.rb
Overview
Represents AMQP 0.9.1 queue.
Defined Under Namespace
Instance Attribute Summary collapse
-
#channel ⇒ Bunny::Channel
readonly
Channel this queue uses.
-
#name ⇒ String
readonly
Queue name.
-
#options ⇒ Hash
readonly
Options this queue was created with.
Class Method Summary collapse
Instance Method Summary collapse
-
#arguments ⇒ Hash
Additional optional arguments (typically used by RabbitMQ extensions and plugins).
-
#auto_delete? ⇒ Boolean
True if this queue was declared as automatically deleted (deleted as soon as last consumer unbinds).
-
#bind(exchange, opts = {}) ⇒ Object
Binds queue to an exchange.
-
#consumer_count ⇒ Integer
How many active consumers the queue has.
-
#delete(opts = {}) ⇒ Object
Deletes the queue.
-
#durable? ⇒ Boolean
True if this queue was declared as durable (will survive broker restart).
-
#exclusive? ⇒ Boolean
True if this queue was declared as exclusive (limited to just one consumer).
-
#initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) ⇒ Queue
constructor
A new instance of Queue.
- #inspect ⇒ Object
-
#message_count ⇒ Integer
How many messages the queue has ready (e.g. not delivered but not unacknowledged).
-
#pop(opts = {:manual_ack => false}, &block) ⇒ Array
(also: #get)
Triple of delivery info, message properties and message content.
-
#publish(payload, opts = {}) ⇒ Object
Publishes a message to the queue via default exchange.
-
#purge(opts = {}) ⇒ Object
Purges a queue (removes all messages from it).
-
#server_named? ⇒ Boolean
True if this queue was declared as server named.
-
#status ⇒ Hash
A hash with information about the number of queue messages and consumers.
-
#subscribe(opts = { :consumer_tag => @channel.generate_consumer_tag, :manual_ack => false, :exclusive => false, :block => false, :on_cancellation => nil }, &block) ⇒ Object
Adds a consumer to the queue (subscribes for message deliveries).
-
#subscribe_with(consumer, opts = {:block => false}) ⇒ Object
Adds a consumer object to the queue (subscribes for message deliveries).
- #to_s ⇒ Object
-
#unbind(exchange, opts = {}) ⇒ Object
Unbinds queue from an exchange.
- #verify_type!(args) ⇒ Object protected
Constructor Details
#initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) ⇒ Queue
Returns a new instance of Queue.
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/bunny/queue.rb', line 54 def initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) # old Bunny versions pass a connection here. In that case, # we just use default channel from it. MK. @channel = channel @name = name = self.class.(name, opts) @durable = [:durable] @exclusive = [:exclusive] @server_named = @name.empty? @auto_delete = [:auto_delete] @type = [:type] @arguments = if @type and !@type.empty? then ([:arguments] || {}).merge({XArgs::QUEUE_TYPE => @type}) else [:arguments] end verify_type!(@arguments) # reassigns updated and verified arguments because Bunny::Channel#declare_queue # accepts a map of options [:arguments] = @arguments @bindings = Array.new @default_consumer = nil declare! unless opts[:no_declare] # for basic.deliver dispatch and such @channel.register_queue(self) # for topology recovery @channel.record_queue(self) end |
Instance Attribute Details
#channel ⇒ Bunny::Channel (readonly)
Returns Channel this queue uses.
34 35 36 |
# File 'lib/bunny/queue.rb', line 34 def channel @channel end |
#name ⇒ String (readonly)
Returns Queue name.
36 37 38 |
# File 'lib/bunny/queue.rb', line 36 def name @name end |
#options ⇒ Hash (readonly)
Returns Options this queue was created with.
38 39 40 |
# File 'lib/bunny/queue.rb', line 38 def end |
Class Method Details
.verify_type!(args0 = {}) ⇒ Object
368 369 370 371 372 373 374 |
# File 'lib/bunny/queue.rb', line 368 def self.verify_type!(args0 = {}) # be extra defensive args = args0 || {} q_type = args["x-queue-type"] || args[:"x-queue-type"] throw ArgumentError.new( "unsupported queue type #{q_type.inspect}, supported ones: #{Types::KNOWN.join(', ')}") if (q_type and !Types.known?(q_type)) end |
Instance Method Details
#arguments ⇒ Hash
Returns Additional optional arguments (typically used by RabbitMQ extensions and plugins).
119 120 121 |
# File 'lib/bunny/queue.rb', line 119 def arguments @arguments end |
#auto_delete? ⇒ Boolean
Returns true if this queue was declared as automatically deleted (deleted as soon as last consumer unbinds).
106 107 108 |
# File 'lib/bunny/queue.rb', line 106 def auto_delete? @auto_delete end |
#bind(exchange, opts = {}) ⇒ Object
Binds queue to an exchange
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/bunny/queue.rb', line 150 def bind(exchange, opts = {}) @channel.queue_bind(@name, exchange, opts) exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end # store bindings for automatic recovery. We need to be very careful to # not cause an infinite rebinding loop here when we recover. MK. binding = { :exchange => exchange_name, :routing_key => (opts[:routing_key] || opts[:key]), :arguments => opts[:arguments] } @bindings.push(binding) unless @bindings.include?(binding) self end |
#consumer_count ⇒ Integer
Returns How many active consumers the queue has.
363 364 365 366 |
# File 'lib/bunny/queue.rb', line 363 def consumer_count s = self.status s[:consumer_count] end |
#delete(opts = {}) ⇒ Object
Deletes the queue
332 333 334 335 336 |
# File 'lib/bunny/queue.rb', line 332 def delete(opts = {}) @channel.delete_recorded_queue_named(self.name) @channel.deregister_queue(self) @channel.queue_delete(@name, opts) end |
#durable? ⇒ Boolean
Returns true if this queue was declared as durable (will survive broker restart).
92 93 94 |
# File 'lib/bunny/queue.rb', line 92 def durable? @durable end |
#exclusive? ⇒ Boolean
Returns true if this queue was declared as exclusive (limited to just one consumer).
99 100 101 |
# File 'lib/bunny/queue.rb', line 99 def exclusive? @exclusive end |
#inspect ⇒ Object
135 136 137 |
# File 'lib/bunny/queue.rb', line 135 def inspect to_s end |
#message_count ⇒ Integer
Returns How many messages the queue has ready (e.g. not delivered but not unacknowledged).
357 358 359 360 |
# File 'lib/bunny/queue.rb', line 357 def s = self.status s[:message_count] end |
#pop(opts = {:manual_ack => false}, &block) ⇒ Array Also known as: get
Returns Triple of delivery info, message properties and message content. If the queue is empty, all three will be nils.
281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 |
# File 'lib/bunny/queue.rb', line 281 def pop(opts = {:manual_ack => false}, &block) unless opts[:ack].nil? warn "[DEPRECATION] `:ack` is deprecated. Please use `:manual_ack` instead." opts[:manual_ack] = opts[:ack] end get_response, properties, content = @channel.basic_get(@name, opts) if block if properties di = GetResponse.new(get_response, @channel) mp = MessageProperties.new(properties) block.call(di, mp, content) else block.call(nil, nil, nil) end else if properties di = GetResponse.new(get_response, @channel) mp = MessageProperties.new(properties) [di, mp, content] else [nil, nil, nil] end end end |
#publish(payload, opts = {}) ⇒ Object
Publishes a message to the queue via default exchange. Takes the same arguments as Exchange#publish
316 317 318 319 320 |
# File 'lib/bunny/queue.rb', line 316 def publish(payload, opts = {}) @channel.default_exchange.publish(payload, opts.merge(:routing_key => @name)) self end |
#purge(opts = {}) ⇒ Object
Purges a queue (removes all messages from it)
341 342 343 344 345 |
# File 'lib/bunny/queue.rb', line 341 def purge(opts = {}) @channel.queue_purge(@name, opts) self end |
#server_named? ⇒ Boolean
Returns true if this queue was declared as server named.
113 114 115 |
# File 'lib/bunny/queue.rb', line 113 def server_named? @server_named end |
#status ⇒ Hash
Returns A hash with information about the number of queue messages and consumers.
350 351 352 353 354 |
# File 'lib/bunny/queue.rb', line 350 def status queue_declare_ok = @channel.queue_declare(@name, .merge(:passive => true)) {:message_count => queue_declare_ok., :consumer_count => queue_declare_ok.consumer_count} end |
#subscribe(opts = { :consumer_tag => @channel.generate_consumer_tag, :manual_ack => false, :exclusive => false, :block => false, :on_cancellation => nil }, &block) ⇒ Object
Adds a consumer to the queue (subscribes for message deliveries).
207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 |
# File 'lib/bunny/queue.rb', line 207 def subscribe(opts = { :consumer_tag => @channel.generate_consumer_tag, :manual_ack => false, :exclusive => false, :block => false, :on_cancellation => nil }, &block) unless opts[:ack].nil? warn "[DEPRECATION] `:ack` is deprecated. Please use `:manual_ack` instead." opts[:manual_ack] = opts[:ack] end ctag = opts.fetch(:consumer_tag, @channel.generate_consumer_tag) consumer = Consumer.new(@channel, self, ctag, !opts[:manual_ack], opts[:exclusive], opts[:arguments]) consumer.on_delivery(&block) consumer.on_cancellation(&opts[:on_cancellation]) if opts[:on_cancellation] @channel.basic_consume_with(consumer) if opts[:block] # joins current thread with the consumers pool, will block # the current thread for as long as the consumer pool is active @channel.work_pool.join end consumer end |
#subscribe_with(consumer, opts = {:block => false}) ⇒ Object
Adds a consumer object to the queue (subscribes for message deliveries).
250 251 252 253 254 255 |
# File 'lib/bunny/queue.rb', line 250 def subscribe_with(consumer, opts = {:block => false}) @channel.basic_consume_with(consumer) @channel.work_pool.join if opts[:block] consumer end |
#to_s ⇒ Object
130 131 132 133 |
# File 'lib/bunny/queue.rb', line 130 def to_s oid = ("0x%x" % (self.object_id << 1)) "<#{self.class.name}:#{oid} @name=\"#{name}\" channel=#{@channel.to_s} @durable=#{@durable} @auto_delete=#{@auto_delete} @exclusive=#{@exclusive} @arguments=#{@arguments}>" end |
#unbind(exchange, opts = {}) ⇒ Object
Unbinds queue from an exchange
179 180 181 182 183 184 185 186 187 188 189 190 191 192 |
# File 'lib/bunny/queue.rb', line 179 def unbind(exchange, opts = {}) @channel.queue_unbind(@name, exchange, opts) exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end @bindings.delete_if { |b| b[:exchange] == exchange_name && b[:routing_key] == (opts[:routing_key] || opts[:key]) && b[:arguments] == opts[:arguments] } self end |
#verify_type!(args) ⇒ Object (protected)
406 407 408 |
# File 'lib/bunny/queue.rb', line 406 def verify_type!(args) self.class.verify_type!(args) end |