Class: AMQP::Queue
- Inherits:
-
AMQ::Client::Queue
- Object
- AMQ::Client::Queue
- AMQP::Queue
- Defined in:
- lib/amqp/queue.rb
Overview
Please make sure you read http://rubyamqp.info/articles/durability/ that covers exchanges durability vs. messages persistence.
What are AMQP queues?
Queues store and forward messages to consumers. They are similar to mailboxes in SMTP. Messages flow from producing applications to exchanges that route them to queues and finally queues deliver them to consumer applications (or consumer applications fetch messages as needed).
Note that unlike some other messaging protocols/systems, messages are not delivered directly to queues. They are delivered to exchanges that route messages to queues using rules knows as bindings.
Concept of bindings
Binding is an association between a queue and an exchange. Queues must be bound to at least one exchange in order to receive messages from publishers. Learn more about bindings in Exchange class documentation.
Key methods
Key methods of Queue class are
Queue names. Server-named queues. Predefined queues.
Every queue has a name that identifies it. Queue names often contain several segments separated by a dot (.), similarly to how URI path segments are separated by a slash (/), although it may be almost any string, with some limitations (see below). Applications may pick queue names or ask broker to generate a name for them. To do so, pass empty string as queue name argument.
Here is an example:
If you want to declare a queue with a particular name, for example, “images.resize”, pass it to Queue class constructor:
Queue names starting with ‘amq.’ are reserved for internal use by the broker. Attempts to declare queue with a name that violates this rule will result in AMQP::IncompatibleOptionsError to be thrown (when queue is re-declared on the same channel object) or channel-level exception (when originally queue was declared on one channel and re-declaration with different attributes happens on another channel). Learn more in Queues guide and Error Handling guide.
Queue life-cycles. When use of server-named queues is optimal and when it isn’t.
To quote AMQP 0.9.1 spec, there are two common message queue life-cycles:
- Durable message queues that are shared by many consumers and have an independent existence: i.e. they will continue to exist and collect messages whether or not there are consumers to receive them.
- Temporary message queues that are private to one consumer and are tied to that consumer. When the consumer disconnects, the message queue is deleted.
There are some variations on these, such as shared message queues that are deleted when the last of many consumers disconnects.
One example of durable message queues is well-known services like event collectors (event loggers). They are usually up whether there are services to log anything or not. Other applications know what queues they use and can rely on those queues being around all the time, survive broker restarts and in general be available should an application in the network need to use them. In this case, explicitly named durable queues are optimal and coupling it creates between applications is not an issue. Another scenario of a well-known long-lived service is distributed metadata/directory/locking server like Apache Zookeeper, Google’s Chubby or DNS. Services like this benefit from using well-known, not generated queue names, and so do other applications that use them.
Different scenario is in “a cloud settings” when some kind of workers/instances may come online and go down basically any time and other applications cannot rely on them being available. Using well-known queue names in this case is possible but server-generated, short-lived queues that are bound to topic or fanout exchanges to receive relevant messages is a better idea.
Imagine a service that processes an endless stream of events (Twitter is one example). When traffic goes up, development operations may spin up additional applications instances in the cloud to handle the load. Those new instances want to subscribe to receive messages to process but the rest of the system doesn’t know anything about them, rely on them being online or try to address them directly: they process events from a shared stream and are not different from their peers. In a case like this, there is no reason for message consumers to not use queue names generated by the broker.
In general, use of explicitly named or server-named queues depends on messaging pattern your application needs. Enterprise Integration Patters discusses many messaging patterns in depth. RabbitMQ FAQ also has a section on use cases.
Queue durability and persistence of messages.
Learn more in our http://rubyamqp.info/articles/durability/.
Message ordering
RabbitMQ FAQ explains ordering of messages in AMQP queues
Error handling
When channel-level error occurs, queues associated with that channel are reset: internal state and callbacks are cleared. Recommended strategy is to open a new channel and re-declare all the entities you need. Learn more in Error Handling guide.
Instance Attribute Summary (collapse)
-
- (Object) name
readonly
Name of this queue.
-
- (Object) opts
Options this queue object was instantiated with.
Error Handling and Recovery (collapse)
-
- (Object) auto_recover
Called by associated connection object when AMQP connection has been re-established (for example, after a network failure).
Instance Method Summary (collapse)
-
- (Queue) bind(exchange, opts = {}) { ... }
This method binds a queue to an exchange.
- - (Object) callback deprecated Deprecated.
-
- (String) consumer_tag
Consumer tag of the default consumer associated with this queue (if any), or nil.
-
- (AMQP::Consumer) default_consumer
Default consumer associated with this queue (if any), or nil.
-
- (NilClass) delete(opts = {}) {|delete_ok| ... }
This method deletes a queue.
- - (Object) handle_declare_ok(method)
-
- (Queue) initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) {|queue, declare_ok| ... }
constructor
A new instance of Queue.
-
- (Object) once_declared(&block)
Defines a callback that will be executed once queue is declared.
- - (Object) once_name_is_available(&block) protected
-
- (Qeueue) pop(opts = {}) {|headers, payload| ... }
This method provides a direct access to the messages in a queue using a synchronous dialogue that is designed for specific types of application where synchronous functionality is more important than performance.
- - (Object) publish(data, opts = {}) deprecated Deprecated.
-
- (NilClass) purge(opts = {}) {|purge_ok| ... }
This method removes all messages from a queue which are not awaiting acknowledgment.
-
- (Object) reset
Resets queue state.
-
- (Boolean) server_named?
True if this queue is server-named.
-
- (Object) status(opts = {}) {|number_of_messages, number_of_active_consumers| ... }
Get the number of messages and active consumers (with active channel flow) on a queue.
-
- (Queue) subscribe(opts = {}) {|headers, payload| ... }
Subscribes to asynchronous message delivery.
- - (Boolean) subscribed? deprecated Deprecated.
-
- (Object) unbind(exchange, opts = {}) { ... }
Remove the binding between the queue and exchange.
-
- (Object) unsubscribe(opts = {}) {|cancel_ok| ... }
Removes the subscription from the queue and cancels the consumer.
Constructor Details
- (Queue) initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) {|queue, declare_ok| ... }
A new instance of Queue
173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 |
# File 'lib/amqp/queue.rb', line 173 def initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}, &block) raise ArgumentError.new("queue name must not be nil; if you want broker to generate queue name for you, pass an empty string") if name.nil? @channel = channel @name = name unless name.empty? @server_named = name.empty? @opts = self.class.(name, opts, block) raise ArgumentError.new("server-named queues (name = '') declaration with :nowait => true makes no sense. If you are not sure what that means, simply drop :nowait => true from opts.") if @server_named && @opts[:nowait] # a deferrable that we use to delay operations until this queue is actually declared. # one reason for this is to support a case when a server-named queue is immediately bound. # it's crazy, but 0.7.x supports it, so... MK. @declaration_deferrable = AMQ::Client::EventMachineClient::Deferrable.new super(channel.connection, channel, name) shim = Proc.new do |q, declare_ok| case block.arity when 1 then block.call(q) else block.call(q, declare_ok) end end @channel.once_open do if @opts[:nowait] @declaration_deferrable.succeed block.call(self) if block end if block self.declare(@opts[:passive], @opts[:durable], @opts[:exclusive], @opts[:auto_delete], @opts[:nowait], @opts[:arguments], &shim) else # we cannot pass :nowait as true here, AMQ::Client::Queue will (rightfully) raise an exception because # it has no idea about crazy edge cases we are trying to support for sake of backwards compatibility. MK. self.declare(@opts[:passive], @opts[:durable], @opts[:exclusive], @opts[:auto_delete], false, @opts[:arguments]) end end end |
Instance Attribute Details
- (Object) name (readonly)
Name of this queue
128 129 130 |
# File 'lib/amqp/queue.rb', line 128 def name @name end |
- (Object) opts
Options this queue object was instantiated with
130 131 132 |
# File 'lib/amqp/queue.rb', line 130 def opts @opts end |
Instance Method Details
- (Object) auto_recover
Called by associated connection object when AMQP connection has been re-established (for example, after a network failure).
306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 |
# File 'lib/amqp/queue.rb', line 306 def auto_recover self.exec_callback_yielding_self(:before_recovery) if self.server_named? old_name = @name.dup @name = AMQ::Protocol::EMPTY_STRING @channel.queues.delete(old_name) end self.redeclare do self.rebind @consumers.each { |tag, consumer| consumer.auto_recover } self.exec_callback_yielding_self(:after_recovery) end end |
- (Queue) bind(exchange, opts = {}) { ... }
This method binds a queue to an exchange. Until a queue is bound it will not receive any messages. In a classic messaging model, store-and-forward queues are bound to a dest exchange and subscription queues are bound to a dest_wild exchange.
A valid exchange name (or reference) must be passed as the first parameter. Note that if your producer application knows consumer queue name and wants to deliver a message there, direct exchange may be sufficient (in other words, if your code declares an exchange with the same name as a queue and binds it to that queue, consider using the default exchange and routing key on publishing).
282 283 284 285 286 287 288 289 290 |
# File 'lib/amqp/queue.rb', line 282 def bind(exchange, opts = {}, &block) @channel.once_open do self.once_name_is_available do super(exchange, (opts[:key] || opts[:routing_key] || AMQ::Protocol::EMPTY_STRING), (opts[:nowait] || block.nil?), opts[:arguments], &block) end end self end |
- (Object) callback
Compatibility alias for #on_declare.
854 855 856 857 858 |
# File 'lib/amqp/queue.rb', line 854 def callback return nil if !subscribed? @default_consumer.callback end |
- (String) consumer_tag
Default consumer is the one registered with the convenience #subscribe method. It has no special properties of any kind.
Consumer tag of the default consumer associated with this queue (if any), or nil
744 745 746 747 748 749 750 |
# File 'lib/amqp/queue.rb', line 744 def consumer_tag if @default_consumer @default_consumer.consumer_tag else nil end end |
- (AMQP::Consumer) default_consumer
Default consumer is the one registered with the convenience #subscribe method. It has no special properties of any kind.
Default consumer associated with this queue (if any), or nil
757 758 759 |
# File 'lib/amqp/queue.rb', line 757 def default_consumer @default_consumer end |
- (NilClass) delete(opts = {}) {|delete_ok| ... }
This method deletes a queue. When a queue is deleted any pending messages are sent to a dead-letter queue if this is defined in the server configuration, and all consumers on the queue are cancelled.
387 388 389 390 391 392 393 394 395 396 |
# File 'lib/amqp/queue.rb', line 387 def delete(opts = {}, &block) @channel.once_open do self.once_name_is_available do super(opts.fetch(:if_unused, false), opts.fetch(:if_empty, false), opts.fetch(:nowait, false), &block) end end # backwards compatibility nil end |
- (Object) handle_declare_ok(method)
888 889 890 891 |
# File 'lib/amqp/queue.rb', line 888 def handle_declare_ok(method) super(method) @declaration_deferrable.succeed end |
- (Object) once_declared(&block)
Defines a callback that will be executed once queue is declared. More than one callback can be defined. if queue is already declared, given callback is executed immediately.
218 219 220 221 222 223 224 |
# File 'lib/amqp/queue.rb', line 218 def once_declared(&block) @declaration_deferrable.callback do # guards against cases when deferred operations # don't complete before the channel is closed block.call if @channel.open? end end |
- (Object) once_name_is_available(&block) (protected)
901 902 903 904 905 906 907 908 909 |
# File 'lib/amqp/queue.rb', line 901 def once_name_is_available(&block) if server_named? self.once_declared do block.call end else block.call end end |
- (Qeueue) pop(opts = {}) {|headers, payload| ... }
This method provides a direct access to the messages in a queue using a synchronous dialogue that is designed for specific types of application where synchronous functionality is more important than performance.
If queue is empty, `payload` callback argument will be nil, otherwise arguments are identical to those of #subscribe callback.
460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 |
# File 'lib/amqp/queue.rb', line 460 def pop(opts = {}, &block) if block # We have to maintain this multiple arities jazz # because older versions this gem are used in examples in at least 3 # books published by O'Reilly :(. MK. shim = Proc.new { |method, headers, payload| case block.arity when 1 then block.call(payload) when 2 then h = Header.new(@channel, method, headers ? headers.decode_payload : nil) block.call(h, payload) else h = Header.new(@channel, method, headers ? headers.decode_payload : nil) block.call(h, payload, method.delivery_tag, method.redelivered, method.exchange, method.routing_key) end } @channel.once_open do self.once_name_is_available do # see AMQ::Client::Queue#get in amq-client self.get(!opts.fetch(:ack, false), &shim) end end else @channel.once_open do self.once_name_is_available do self.get(!opts.fetch(:ack, false)) end end end end |
- (Object) publish(data, opts = {})
This method will be removed before 1.0 release
Don’t use this method. It is a leftover from very early days and it ruins the whole point of exchanges/queue separation.
869 870 871 |
# File 'lib/amqp/queue.rb', line 869 def publish(data, opts = {}) exchange.publish(data, opts.merge(:routing_key => self.name)) end |
- (NilClass) purge(opts = {}) {|purge_ok| ... }
This method removes all messages from a queue which are not awaiting acknowledgment.
414 415 416 417 418 419 420 421 422 423 |
# File 'lib/amqp/queue.rb', line 414 def purge(opts = {}, &block) @channel.once_open do self.once_declared do super(opts.fetch(:nowait, false), &block) end end # backwards compatibility nil end |
- (Object) reset
Resets queue state. Useful for error handling.
875 876 877 |
# File 'lib/amqp/queue.rb', line 875 def reset initialize(@channel, @name, @opts) end |
- (Boolean) server_named?
True if this queue is server-named
228 229 230 |
# File 'lib/amqp/queue.rb', line 228 def server_named? @server_named end |
- (Object) status(opts = {}) {|number_of_messages, number_of_active_consumers| ... }
Get the number of messages and active consumers (with active channel flow) on a queue.
816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 |
# File 'lib/amqp/queue.rb', line 816 def status(opts = {}, &block) raise ArgumentError, "AMQP::Queue#status does not make any sense without a block" unless block shim = Proc.new { |q, declare_ok| block.call(declare_ok., declare_ok.consumer_count) } @channel.once_open do self.once_name_is_available do # we do not use self.declare here to avoid caching of @passive since that will cause unexpected side-effects during automatic # recovery process. MK. @connection.send_frame(AMQ::Protocol::Queue::Declare.encode(@channel.id, @name, true, @opts[:durable], @opts[:exclusive], @opts[:auto_delete], false, @opts[:arguments])) self.append_callback(:declare, &shim) @channel.queues_awaiting_declare_ok.push(self) end end self end |
- (Queue) subscribe(opts = {}) {|headers, payload| ... }
Subscribes to asynchronous message delivery.
The provided block is passed a single message each time the exchange matches a message to this queue.
Attempts to #subscribe multiple times to the same exchange will raise an Exception. If you need more than one consumer per queue, use Consumer instead. Documentation guide on queues explains this and other topics in great detail.
If the block takes 2 parameters, both the header and the body will be passed in for processing.
721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 |
# File 'lib/amqp/queue.rb', line 721 def subscribe(opts = {}, &block) raise RuntimeError.new("This queue already has default consumer. Please instantiate AMQP::Consumer directly to register additional consumers.") if @default_consumer opts[:nowait] = false if (@on_confirm_subscribe = opts[:confirm]) @channel.once_open do self.once_name_is_available do # guards against a pathological case race condition when a channel # is opened and closed before delayed operations are completed. self.consume(!opts[:ack], opts[:exclusive], (opts[:nowait] || block.nil?), opts[:no_local], nil, &opts[:confirm]) self.on_delivery(&block) end end self end |
- (Boolean) subscribed?
Boolean check to see if the current queue has already subscribed to messages delivery (has default consumer).
Attempts to #subscribe multiple times to the same exchange will raise an Exception. If you need more than one consumer per queue, use Consumer instead.
845 846 847 |
# File 'lib/amqp/queue.rb', line 845 def subscribed? @default_consumer && @default_consumer.subscribed? end |
- (Object) unbind(exchange, opts = {}) { ... }
Remove the binding between the queue and exchange. The queue will not receive any more messages until it is bound to another exchange.
Due to the asynchronous nature of the protocol, it is possible for “in flight” messages to be received after this call completes. Those messages will be serviced by the last block used in a #subscribe or #pop call.
351 352 353 354 355 356 357 |
# File 'lib/amqp/queue.rb', line 351 def unbind(exchange, opts = {}, &block) @channel.once_open do self.once_name_is_available do super(exchange, (opts[:key] || opts[:routing_key] || AMQ::Protocol::EMPTY_STRING), opts[:arguments], &block) end end end |
- (Object) unsubscribe(opts = {}) {|cancel_ok| ... }
Removes the subscription from the queue and cancels the consumer. Once consumer is cancelled, messages will no longer be delivered to it, however, due to the asynchronous nature of the protocol, it is possible for “in flight” messages to be received after this call completes. Those messages will be serviced by the last block used in a #subscribe or #pop call.
Fetching messages with #pop is still possible even after consumer is cancelled.
Additionally, if the queue was created with autodelete set to true, the server will delete the queue after its wait period has expired unless the queue is bound to an active exchange.
The method accepts a block which will be executed when the unsubscription request is acknowledged as complete by the server.
793 794 795 796 797 798 799 800 801 |
# File 'lib/amqp/queue.rb', line 793 def unsubscribe(opts = {}, &block) @channel.once_open do self.once_name_is_available do if @default_consumer @default_consumer.cancel(opts.fetch(:nowait, true), &block); @default_consumer = nil end end end end |