Class: AMQP::Queue
- Inherits:
-
Object
- Object
- AMQP::Queue
- Extended by:
- ProtocolMethodHandlers
- Includes:
- Entity, ServerNamedEntity
- 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.
Constant Summary
Constants included from Openable
Instance Attribute Summary collapse
-
#arguments ⇒ Hash
readonly
Additional arguments given on queue declaration.
- #bindings ⇒ Array<Hash> readonly
-
#channel ⇒ AMQP::Channel
readonly
Channel this queue belongs to.
-
#consumers ⇒ Array<Hash>
readonly
All consumers on this queue.
-
#default_consumer ⇒ AMQP::Consumer
readonly
Default consumer associated with this queue (if any), or nil.
-
#name ⇒ Object
readonly
Name of this queue.
-
#opts ⇒ Object
Options this queue object was instantiated with.
Attributes included from Entity
Declaration collapse
-
#queue_declare(passive = false, durable = false, exclusive = false, auto_delete = false, nowait = false, arguments = nil, &block) ⇒ Queue
Declares this queue.
-
#redeclare(&block) ⇒ Object
Re-declares queue with the same attributes.
Binding collapse
-
#queue_bind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, nowait = false, arguments = nil, &block) ⇒ Queue
Self.
-
#queue_unbind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, arguments = nil, &block) ⇒ Queue
Self.
Consuming messages collapse
-
#basic_consume(no_ack = false, exclusive = false, nowait = false, no_local = false, arguments = nil, &block) ⇒ Queue
Self.
-
#cancel(nowait = false, &block) ⇒ Queue
Unsubscribes from message delivery.
- #on_cancel(&block) ⇒ Object
Working With Messages collapse
-
#get(no_ack = false, &block) ⇒ Queue
Fetches messages from the queue.
-
#queue_purge(nowait = false, &block) ⇒ Queue
Purges (removes all messagse from) the queue.
Acknowledging & Rejecting Messages collapse
-
#acknowledge(delivery_tag) ⇒ Queue
Acknowledge a delivery tag.
-
#reject(delivery_tag, requeue = true) ⇒ Queue
Self.
Error Handling & Recovery collapse
-
#auto_recover ⇒ Object
Called by associated connection object when AMQP connection has been re-established (for example, after a network failure).
-
#before_recovery(&block) ⇒ Object
Defines a callback that will be executed after TCP connection is recovered after a network failure but before AMQP connection is re-opened.
-
#on_connection_interruption(&block) ⇒ Object
(also: #after_connection_interruption)
Defines a callback that will be executed after TCP connection is interrupted (typically because of a network failure).
-
#on_recovery(&block) ⇒ Object
(also: #after_recovery)
Defines a callback that will be executed when AMQP connection is recovered after a network failure..
Instance Method Summary collapse
-
#auto_delete? ⇒ Boolean
True if this queue was declared as automatically deleted (deleted as soon as last consumer unbinds).
-
#bind(exchange, opts = {}) { ... } ⇒ Queue
This method binds a queue to an exchange.
- #callback ⇒ Object deprecated Deprecated.
-
#consumer_tag ⇒ String
Consumer tag of the default consumer associated with this queue (if any), or nil.
-
#delete(opts = {}) {|delete_ok| ... } ⇒ NilClass
This method deletes a 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).
-
#generate_consumer_tag(name) ⇒ String
Unique string supposed to be used as a consumer tag.
-
#handle_bind_ok(method) ⇒ Object
handle_purge_ok(method).
- #handle_declare_ok(method) ⇒ Object
- #handle_delete_ok(method) ⇒ Object
-
#handle_get_empty(method) ⇒ Object
handle_get_ok(method, header, payload).
-
#handle_get_ok(method, header, payload) ⇒ Object
handle_unbind_ok(method).
-
#handle_purge_ok(method) ⇒ Object
handle_delete_ok(method).
-
#handle_unbind_ok(method) ⇒ Object
handle_bind_ok(method).
-
#initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) {|queue, declare_ok| ... } ⇒ Queue
constructor
A new instance of Queue.
- #on_delivery(&block) ⇒ Object
-
#once_declared(&block) ⇒ Object
Defines a callback that will be executed once queue is declared.
- #once_name_is_available(&block) ⇒ Object protected
-
#pop(opts = {}) {|headers, payload| ... } ⇒ Qeueue
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.
- #publish(data, opts = {}) ⇒ Object deprecated Deprecated.
-
#purge(opts = {}) {|purge_ok| ... } ⇒ NilClass
This method removes all messages from a queue which are not awaiting acknowledgment.
-
#queue_delete(if_unused = false, if_empty = false, nowait = false, &block) ⇒ Queue
Deletes this queue.
-
#reset ⇒ Object
Resets queue state.
-
#server_named? ⇒ Boolean
True if this queue is server-named.
-
#status(opts = {}) {|number_of_messages, number_of_active_consumers| ... } ⇒ Object
Get the number of messages and active consumers (with active channel flow) on a queue.
-
#subscribe(opts = {}) {|headers, payload| ... } ⇒ Queue
Subscribes to asynchronous message delivery.
- #subscribed? ⇒ Boolean deprecated Deprecated.
-
#unbind(exchange, opts = {}) { ... } ⇒ Object
Remove the binding between the queue and exchange.
-
#unsubscribe(opts = {}) {|cancel_ok| ... } ⇒ Object
Removes the subscription from the queue and cancels the consumer.
Methods included from ProtocolMethodHandlers
Methods included from Callbacks
#clear_callbacks, #define_callback, #exec_callback, #exec_callback_once, #exec_callback_once_yielding_self, #exec_callback_yielding_self, #has_callback?, #prepend_callback, #redefine_callback
Methods included from Openable
#closed!, #closed?, #closing!, #closing?, #opened!, #opened?, #opening!, #opening?
Constructor Details
#initialize(channel, name = AMQ::Protocol::EMPTY_STRING, opts = {}) {|queue, declare_ok| ... } ⇒ Queue
Returns a new instance of Queue.
201 202 203 204 205 206 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 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 |
# File 'lib/amqp/queue.rb', line 201 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 = AMQP::Deferrable.new super(channel.connection) @name = name # this has to stay true even after queue.declare-ok arrives. MK. @server_named = @name.empty? if @server_named self.on_connection_interruption do # server-named queue need to get new names after recovery. MK. @name = AMQ::Protocol::EMPTY_STRING end end @channel = channel # primarily for autorecovery. MK. @bindings = Array.new @consumers = Hash.new 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.queue_declare(@opts[:passive], @opts[:durable], @opts[:exclusive], @opts[:auto_delete], @opts[:nowait], @opts[:arguments], &shim) else # we cannot pass :nowait as true here, AMQP::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.queue_declare(@opts[:passive], @opts[:durable], @opts[:exclusive], @opts[:auto_delete], false, @opts[:arguments]) end end end |
Instance Attribute Details
#arguments ⇒ Hash (readonly)
Returns Additional arguments given on queue declaration. Typically used by AMQP extensions.
155 156 157 |
# File 'lib/amqp/queue.rb', line 155 def arguments @arguments end |
#bindings ⇒ Array<Hash> (readonly)
158 159 160 |
# File 'lib/amqp/queue.rb', line 158 def bindings @bindings end |
#channel ⇒ AMQP::Channel (readonly)
Channel this queue belongs to.
146 147 148 |
# File 'lib/amqp/queue.rb', line 146 def channel @channel end |
#consumers ⇒ Array<Hash> (readonly)
Returns All consumers on this queue.
149 150 151 |
# File 'lib/amqp/queue.rb', line 149 def consumers @consumers end |
#default_consumer ⇒ AMQP::Consumer (readonly)
Default consumer is the one registered with the convenience #subscribe method. It has no special properties of any kind.
Returns Default consumer associated with this queue (if any), or nil.
152 153 154 |
# File 'lib/amqp/queue.rb', line 152 def default_consumer @default_consumer end |
#name ⇒ Object (readonly)
Name of this queue
140 141 142 |
# File 'lib/amqp/queue.rb', line 140 def name @name end |
#opts ⇒ Object
Options this queue object was instantiated with
142 143 144 |
# File 'lib/amqp/queue.rb', line 142 def opts @opts end |
Instance Method Details
#acknowledge(delivery_tag) ⇒ Queue
Acknowledge a delivery tag.
1168 1169 1170 1171 1172 |
# File 'lib/amqp/queue.rb', line 1168 def acknowledge(delivery_tag) @channel.acknowledge(delivery_tag) self end |
#auto_delete? ⇒ Boolean
Returns true if this queue was declared as automatically deleted (deleted as soon as last consumer unbinds).
286 287 288 |
# File 'lib/amqp/queue.rb', line 286 def auto_delete? @auto_delete end |
#auto_recover ⇒ Object
Called by associated connection object when AMQP connection has been re-established (for example, after a network failure).
1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 |
# File 'lib/amqp/queue.rb', line 1202 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 |
#basic_consume(no_ack = false, exclusive = false, nowait = false, no_local = false, arguments = nil, &block) ⇒ Queue
Returns self.
1083 1084 1085 1086 1087 1088 1089 1090 1091 |
# File 'lib/amqp/queue.rb', line 1083 def basic_consume(no_ack = false, exclusive = false, nowait = false, no_local = false, arguments = nil, &block) raise RuntimeError.new("This queue already has default consumer. Please instantiate AMQP::Consumer directly to register additional consumers.") if @default_consumer nowait = true unless block @default_consumer = self.class.consumer_class.new(@channel, self, generate_consumer_tag(@name), exclusive, no_ack, arguments, no_local, &block) @default_consumer.consume(nowait, &block) self end |
#before_recovery(&block) ⇒ Object
Defines a callback that will be executed after TCP connection is recovered after a network failure but before AMQP connection is re-opened. Only one callback can be defined (the one defined last replaces previously added ones).
1235 1236 1237 |
# File 'lib/amqp/queue.rb', line 1235 def before_recovery(&block) self.redefine_callback(:before_recovery, &block) end |
#bind(exchange, opts = {}) { ... } ⇒ Queue
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).
346 347 348 349 350 351 352 353 354 |
# File 'lib/amqp/queue.rb', line 346 def bind(exchange, opts = {}, &block) @channel.once_open do self.once_name_is_available do queue_bind(exchange, (opts[:key] || opts[:routing_key] || AMQ::Protocol::EMPTY_STRING), (opts[:nowait] || block.nil?), opts[:arguments], &block) end end self end |
#callback ⇒ Object
Compatibility alias for #on_declare.
888 889 890 891 892 |
# File 'lib/amqp/queue.rb', line 888 def callback return nil if !subscribed? @default_consumer.callback end |
#cancel(nowait = false, &block) ⇒ Queue
Unsubscribes from message delivery.
1098 1099 1100 1101 1102 1103 1104 |
# File 'lib/amqp/queue.rb', line 1098 def cancel(nowait = false, &block) raise "There is no default consumer for this queue. This usually means that you are trying to unsubscribe a queue that never was subscribed for messages in the first place." if @default_consumer.nil? @default_consumer.cancel(nowait, &block) self end |
#consumer_tag ⇒ String
Default consumer is the one registered with the convenience #subscribe method. It has no special properties of any kind.
Returns Consumer tag of the default consumer associated with this queue (if any), or nil.
778 779 780 781 782 783 784 |
# File 'lib/amqp/queue.rb', line 778 def consumer_tag if @default_consumer @default_consumer.consumer_tag else nil end end |
#delete(opts = {}) {|delete_ok| ... } ⇒ NilClass
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.
414 415 416 417 418 419 420 421 422 423 |
# File 'lib/amqp/queue.rb', line 414 def delete(opts = {}, &block) @channel.once_open do self.once_name_is_available do queue_delete(opts.fetch(:if_unused, false), opts.fetch(:if_empty, false), opts.fetch(:nowait, false), &block) end end # backwards compatibility nil end |
#durable? ⇒ Boolean
Returns true if this queue was declared as durable (will survive broker restart).
274 275 276 |
# File 'lib/amqp/queue.rb', line 274 def durable? @durable end |
#exclusive? ⇒ Boolean
Returns true if this queue was declared as exclusive (limited to just one consumer).
280 281 282 |
# File 'lib/amqp/queue.rb', line 280 def exclusive? @exclusive end |
#generate_consumer_tag(name) ⇒ String
Unique string supposed to be used as a consumer tag.
1275 1276 1277 |
# File 'lib/amqp/queue.rb', line 1275 def generate_consumer_tag(name) "#{name}-#{Time.now.to_i * 1000}-#{Kernel.rand(999_999_999_999)}" end |
#get(no_ack = false, &block) ⇒ Queue
Fetches messages from the queue.
1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 |
# File 'lib/amqp/queue.rb', line 1123 def get(no_ack = false, &block) @connection.send_frame(AMQ::Protocol::Basic::Get.encode(@channel.id, @name, no_ack)) # most people only want one callback per #get call. Consider the following example: # # 100.times { queue.get { ... } } # # most likely you won't expect 100 callback runs per message here. MK. self.redefine_callback(:get, &block) @channel.queues_awaiting_get_response.push(self) self end |
#handle_bind_ok(method) ⇒ Object
handle_purge_ok(method)
1293 1294 1295 |
# File 'lib/amqp/queue.rb', line 1293 def handle_bind_ok(method) self.exec_callback_once(:bind, method) end |
#handle_declare_ok(method) ⇒ Object
924 925 926 927 928 929 930 931 |
# File 'lib/amqp/queue.rb', line 924 def handle_declare_ok(method) @name = method.queue if @name.empty? @channel.register_queue(self) self.exec_callback_once_yielding_self(:declare, method) @declaration_deferrable.succeed end |
#handle_delete_ok(method) ⇒ Object
1285 1286 1287 |
# File 'lib/amqp/queue.rb', line 1285 def handle_delete_ok(method) self.exec_callback_once(:delete, method) end |
#handle_get_empty(method) ⇒ Object
handle_get_ok(method, header, payload)
1306 1307 1308 1309 |
# File 'lib/amqp/queue.rb', line 1306 def handle_get_empty(method) method = AMQ::Protocol::GetResponse.new(method) self.exec_callback(:get, method) end |
#handle_get_ok(method, header, payload) ⇒ Object
handle_unbind_ok(method)
1301 1302 1303 1304 |
# File 'lib/amqp/queue.rb', line 1301 def handle_get_ok(method, header, payload) method = AMQ::Protocol::GetResponse.new(method) self.exec_callback(:get, method, header, payload) end |
#handle_purge_ok(method) ⇒ Object
handle_delete_ok(method)
1289 1290 1291 |
# File 'lib/amqp/queue.rb', line 1289 def handle_purge_ok(method) self.exec_callback_once(:purge, method) end |
#handle_unbind_ok(method) ⇒ Object
handle_bind_ok(method)
1297 1298 1299 |
# File 'lib/amqp/queue.rb', line 1297 def handle_unbind_ok(method) self.exec_callback_once(:unbind, method) end |
#on_cancel(&block) ⇒ Object
1107 1108 1109 |
# File 'lib/amqp/queue.rb', line 1107 def on_cancel(&block) @default_consumer.on_cancel(&block) end |
#on_connection_interruption(&block) ⇒ Object Also known as: after_connection_interruption
Defines a callback that will be executed after TCP connection is interrupted (typically because of a network failure). Only one callback can be defined (the one defined last replaces previously added ones).
1225 1226 1227 |
# File 'lib/amqp/queue.rb', line 1225 def on_connection_interruption(&block) self.redefine_callback(:after_connection_interruption, &block) end |
#on_delivery(&block) ⇒ Object
768 769 770 |
# File 'lib/amqp/queue.rb', line 768 def on_delivery(&block) @default_consumer.on_delivery(&block) end |
#on_recovery(&block) ⇒ Object Also known as: after_recovery
Defines a callback that will be executed when AMQP connection is recovered after a network failure.. Only one callback can be defined (the one defined last replaces previously added ones).
1251 1252 1253 |
# File 'lib/amqp/queue.rb', line 1251 def on_recovery(&block) self.redefine_callback(:after_recovery, &block) end |
#once_declared(&block) ⇒ Object
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.
263 264 265 266 267 268 269 |
# File 'lib/amqp/queue.rb', line 263 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 |
#once_name_is_available(&block) ⇒ Object (protected)
1383 1384 1385 1386 1387 1388 1389 1390 1391 |
# File 'lib/amqp/queue.rb', line 1383 def once_name_is_available(&block) if server_named? self.once_declared do block.call end else block.call end end |
#pop(opts = {}) {|headers, payload| ... } ⇒ Qeueue
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.
487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 |
# File 'lib/amqp/queue.rb', line 487 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 AMQP::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 |
#publish(data, opts = {}) ⇒ Object
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.
903 904 905 |
# File 'lib/amqp/queue.rb', line 903 def publish(data, opts = {}) exchange.publish(data, opts.merge(:routing_key => self.name)) end |
#purge(opts = {}) {|purge_ok| ... } ⇒ NilClass
This method removes all messages from a queue which are not awaiting acknowledgment.
441 442 443 444 445 446 447 448 449 450 |
# File 'lib/amqp/queue.rb', line 441 def purge(opts = {}, &block) @channel.once_open do self.once_declared do queue_purge(opts.fetch(:nowait, false), &block) end end # backwards compatibility nil end |
#queue_bind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, nowait = false, arguments = nil, &block) ⇒ Queue
Returns self.
1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 |
# File 'lib/amqp/queue.rb', line 1023 def queue_bind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, nowait = false, arguments = nil, &block) nowait = true unless block exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end @connection.send_frame(AMQ::Protocol::Queue::Bind.encode(@channel.id, @name, exchange_name, routing_key, nowait, arguments)) if !nowait self.append_callback(:bind, &block) @channel.queues_awaiting_bind_ok.push(self) end # store bindings for automatic recovery, but BE VERY CAREFUL to # not cause an infinite rebinding loop here when we recover. MK. binding = { :exchange => exchange_name, :routing_key => routing_key, :arguments => arguments } @bindings.push(binding) unless @bindings.include?(binding) self end |
#queue_declare(passive = false, durable = false, exclusive = false, auto_delete = false, nowait = false, arguments = nil, &block) ⇒ Queue
Declares this queue.
943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 |
# File 'lib/amqp/queue.rb', line 943 def queue_declare(passive = false, durable = false, exclusive = false, auto_delete = false, nowait = false, arguments = nil, &block) raise ArgumentError, "declaration with nowait does not make sense for server-named queues! Either specify name other than empty string or use #declare without nowait" if nowait && self.anonymous? # these two are for autorecovery. MK. @passive = passive @server_named = @name.empty? @durable = durable @exclusive = exclusive @auto_delete = auto_delete @arguments = arguments nowait = true if !block && !@name.empty? && nowait.nil? @connection.send_frame(AMQ::Protocol::Queue::Declare.encode(@channel.id, @name, passive, durable, exclusive, auto_delete, nowait, arguments)) if !nowait self.append_callback(:declare, &block) @channel.queues_awaiting_declare_ok.push(self) end self end |
#queue_delete(if_unused = false, if_empty = false, nowait = false, &block) ⇒ Queue
Deletes this queue.
1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 |
# File 'lib/amqp/queue.rb', line 1000 def queue_delete(if_unused = false, if_empty = false, nowait = false, &block) nowait = true unless block @connection.send_frame(AMQ::Protocol::Queue::Delete.encode(@channel.id, @name, if_unused, if_empty, nowait)) if !nowait self.append_callback(:delete, &block) # TODO: delete itself from queues cache @channel.queues_awaiting_delete_ok.push(self) end self end |
#queue_purge(nowait = false, &block) ⇒ Queue
Purges (removes all messagse from) the queue.
1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 |
# File 'lib/amqp/queue.rb', line 1144 def queue_purge(nowait = false, &block) nowait = true unless block @connection.send_frame(AMQ::Protocol::Queue::Purge.encode(@channel.id, @name, nowait)) if !nowait self.redefine_callback(:purge, &block) # TODO: handle channel & connection-level exceptions @channel.queues_awaiting_purge_ok.push(self) end self end |
#queue_unbind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, arguments = nil, &block) ⇒ Queue
Returns self.
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 |
# File 'lib/amqp/queue.rb', line 1052 def queue_unbind(exchange, routing_key = AMQ::Protocol::EMPTY_STRING, arguments = nil, &block) exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end @connection.send_frame(AMQ::Protocol::Queue::Unbind.encode(@channel.id, @name, exchange_name, routing_key, arguments)) self.append_callback(:unbind, &block) @channel.queues_awaiting_unbind_ok.push(self) @bindings.delete_if { |b| b[:exchange] == exchange_name } self end |
#redeclare(&block) ⇒ Object
Re-declares queue with the same attributes
968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 |
# File 'lib/amqp/queue.rb', line 968 def redeclare(&block) nowait = true if !block && !@name.empty? # server-named queues get their new generated names. new_name = if @server_named AMQ::Protocol::EMPTY_STRING else @name end @connection.send_frame(AMQ::Protocol::Queue::Declare.encode(@channel.id, new_name, @passive, @durable, @exclusive, @auto_delete, false, @arguments)) if !nowait self.append_callback(:declare, &block) @channel.queues_awaiting_declare_ok.push(self) end self end |
#reject(delivery_tag, requeue = true) ⇒ Queue
Returns self.
1179 1180 1181 1182 1183 |
# File 'lib/amqp/queue.rb', line 1179 def reject(delivery_tag, requeue = true) @channel.reject(delivery_tag, requeue) self end |
#reset ⇒ Object
Resets queue state. Useful for error handling.
909 910 911 |
# File 'lib/amqp/queue.rb', line 909 def reset initialize(@channel, @name, @opts) end |
#server_named? ⇒ Boolean
Returns true if this queue is server-named.
292 293 294 |
# File 'lib/amqp/queue.rb', line 292 def server_named? @server_named end |
#status(opts = {}) {|number_of_messages, number_of_active_consumers| ... } ⇒ Object
Get the number of messages and active consumers (with active channel flow) on a queue.
850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 |
# File 'lib/amqp/queue.rb', line 850 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 |
#subscribe(opts = {}) {|headers, payload| ... } ⇒ Queue
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.
748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 |
# File 'lib/amqp/queue.rb', line 748 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.basic_consume(!opts[:ack], opts[:exclusive], (opts[:nowait] || block.nil?), opts[:no_local], nil, &opts[:confirm]) self.on_delivery(&block) end end self end |
#subscribed? ⇒ Boolean
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.
879 880 881 |
# File 'lib/amqp/queue.rb', line 879 def subscribed? @default_consumer && @default_consumer.subscribed? end |
#unbind(exchange, opts = {}) { ... } ⇒ Object
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.
378 379 380 381 382 383 384 |
# File 'lib/amqp/queue.rb', line 378 def unbind(exchange, opts = {}, &block) @channel.once_open do self.once_name_is_available do queue_unbind(exchange, (opts[:key] || opts[:routing_key] || AMQ::Protocol::EMPTY_STRING), opts[:arguments], &block) end end end |
#unsubscribe(opts = {}) {|cancel_ok| ... } ⇒ Object
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.
827 828 829 830 831 832 833 834 835 |
# File 'lib/amqp/queue.rb', line 827 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 |