Class: Bunny::Channel
- Inherits:
-
Object
- Object
- Bunny::Channel
- Defined in:
- lib/bunny/channel.rb
Overview
Channels in RabbitMQ
To quote AMQP 0.9.1 specification:
AMQP 0.9.1 is a multi-channelled protocol. Channels provide a way to multiplex a heavyweight TCP/IP connection into several light weight connections. This makes the protocol more “firewall friendly” since port usage is predictable. It also means that traffic shaping and other network QoS features can be easily employed. Channels are independent of each other and can perform different functions simultaneously with other channels, the available bandwidth being shared between the concurrent activities.
Opening Channels
Channels can be opened either via Bunny::Session#create_channel (sufficient in the majority
of cases) or by instantiating Bunny::Channel directly:
conn = Bunny.new
conn.start
ch = conn.create_channel
This will automatically allocate a channel id.
Closing Channels
Channels are closed via #close. Channels that get a channel-level exception are closed, too. Closed channels can no longer be used. Attempts to use them will raise ChannelAlreadyClosed.
ch = conn.create_channel
ch.close
Higher-level API
Bunny offers two sets of methods on Channel: known as higher-level and lower-level APIs, respectively. Higher-level API mimics amqp gem API where exchanges and queues are objects (instance of Exchange and Queue, respectively). Lower-level API is built around AMQP 0.9.1 methods (commands), where queues and exchanges are passed as strings (à la RabbitMQ Java client, Langohr and Pika).
Queue Operations In Higher-level API
Exchange Operations In Higher-level API
- #topic declares a topic exchange. The rest of the API is in Exchange.
- #direct declares a direct exchange.
- #fanout declares a fanout exchange.
- #headers declares a headers exchange.
- #default_exchange
- #exchange is used to declare exchanges with type specified as a symbol or string.
Channel Qos (Prefetch Level)
It is possible to control how many messages at most a consumer will be given (before it acknowledges or rejects previously consumed ones). This setting is per channel and controlled via #prefetch.
Channel IDs
Channels are identified by their ids which are integers. Bunny takes care of allocating and releasing them as channels are opened and closed. It is almost never necessary to specify channel ids explicitly.
There is a limit on the maximum number of channels per connection, usually 65536. Note that allocating channels is very cheap on both client and server so having tens, hundreds or even thousands of channels is not a problem.
Channels and Error Handling
Channel-level exceptions are more common than connection-level ones and often indicate issues applications can recover from (such as consuming from or trying to delete a queue that does not exist).
With Bunny, channel-level exceptions are raised as Ruby exceptions, for example,
NotFound, that provide access to the underlying channel.close method
information.
Consumer and Message operations (basic.*) collapse
- MAX_PREFETCH_COUNT =
prefetch_count is of type short in the protocol. MK.
(2 ** 16) - 1
Constant Summary collapse
- DEFAULT_CONTENT_TYPE =
"application/octet-stream".freeze
- DEFAULT_OUTSTANDING_CONFIRMS_LIMIT =
Default outstanding limit for publisher confirms with tracking. Batch size of 1000 provides optimal throughput per benchmarks.
1000- SHORTSTR_LIMIT =
255
Instance Attribute Summary collapse
-
#cancel_consumers_before_closing ⇒ Object
readonly
Returns the value of attribute cancel_consumers_before_closing.
-
#confirm_timeout ⇒ Integer?
readonly
Timeout in milliseconds for waiting on publisher confirms.
-
#confirms_tracking_enabled ⇒ Boolean
readonly
True if publisher confirm tracking is enabled.
-
#connection ⇒ Bunny::Session
readonly
AMQP connection this channel was opened on.
-
#consumers ⇒ Hash<String, Bunny::Consumer>
readonly
Consumer instances declared on this channel.
-
#delivery_tag_offset ⇒ Integer
readonly
This will be set to the current sequence index during automatic network failure recovery to keep the sequence monotonic for the user and abstract the reset from the protocol.
-
#exchanges ⇒ Hash<String, Bunny::Exchange>
readonly
Exchange instances declared on this channel.
-
#id ⇒ Integer
Channel id.
-
#nacked_set ⇒ Set<Integer>
readonly
Set of nacked message indexes that have been nacked.
-
#next_publish_seq_no ⇒ Integer
readonly
Next publisher confirmations sequence index.
-
#outstanding_limit ⇒ Integer?
readonly
Maximum outstanding unconfirmed messages before throttling.
-
#prefetch_count ⇒ Integer
readonly
Active basic.qos prefetch value.
-
#prefetch_global ⇒ Integer
readonly
Active basic.qos prefetch global mode.
-
#queues ⇒ Hash<String, Bunny::Queue>
readonly
Queue instances declared on this channel.
-
#status ⇒ Symbol
readonly
Channel status (:opening, :open, :closed).
-
#unconfirmed_set ⇒ Set<Integer>
readonly
Set of published message indexes that are currently unconfirmed.
-
#work_pool ⇒ Bunny::ConsumerWorkPool
readonly
Thread pool delivered messages are dispatched to.
Backwards compatibility with 0.8.0 collapse
-
#active ⇒ Boolean
True if this channel is open.
-
#client ⇒ Bunny::Session
Connection this channel was opened on.
-
#number ⇒ Integer
Channel id.
Other settings collapse
- #cancel_consumers_before_closing! ⇒ Object
- #cancel_consumers_before_closing? ⇒ Boolean
- #configure(&block) ⇒ Object
Higher-level API for exchange operations collapse
-
#default_exchange ⇒ Object
Provides access to the default exchange.
-
#direct(name, opts = {}) ⇒ Bunny::Exchange
Declares a direct exchange or looks it up in the cache of previously declared exchanges.
-
#exchange(name, opts = {}) ⇒ Bunny::Exchange
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
-
#fanout(name, opts = {}) ⇒ Bunny::Exchange
Declares a fanout exchange or looks it up in the cache of previously declared exchanges.
-
#headers(name, opts = {}) ⇒ Bunny::Exchange
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
-
#topic(name, opts = {}) ⇒ Bunny::Exchange
Declares a topic exchange or looks it up in the cache of previously declared exchanges.
Higher-level API for queue operations collapse
-
#durable_queue(name, type = "classic", opts = {}) ⇒ Bunny::Queue
Declares a new server-named queue that is automatically deleted when the connection is closed.
-
#queue(name = AMQ::Protocol::EMPTY_STRING, opts = {}) ⇒ Bunny::Queue
Declares a queue or looks it up in the per-channel cache.
-
#quorum_queue(name, opts = {}) ⇒ Bunny::Queue
Declares a new client-named quorum queue.
-
#stream(name, opts = {}) ⇒ Bunny::Queue
Declares a new client-named stream (that Bunny can use as if it was a queue).
-
#temporary_queue(opts = {}) ⇒ Bunny::Queue
Declares a new server-named queue that is automatically deleted when the connection is closed.
QoS and Flow Control collapse
-
#flow(active) ⇒ Object
Flow control.
-
#recover(ignored = true) ⇒ Object
Tells RabbitMQ to redeliver unacknowledged messages.
Message acknowledgements collapse
-
#ack(delivery_tag, multiple = false) ⇒ Object
(also: #acknowledge)
Acknowledges a message.
-
#nack(delivery_tag, multiple = false, requeue = false) ⇒ Object
Rejects a message.
-
#reject(delivery_tag, requeue = false) ⇒ Object
Rejects a message.
Consumer and Message operations (basic.*) collapse
-
#any_consumers? ⇒ Boolean
True if there are consumers on this channel.
-
#basic_ack(delivery_tag, multiple = false) ⇒ NilClass
Acknowledges a delivery (message).
-
#basic_cancel(consumer_tag, opts = {}) ⇒ AMQ::Protocol::Basic::CancelOk?
Removes a consumer.
-
#basic_consume(queue, consumer_tag = generate_consumer_tag, no_ack = false, exclusive = false, arguments = nil, &block) ⇒ AMQ::Protocol::Basic::ConsumeOk
(also: #consume)
Registers a consumer for queue.
-
#basic_consume_with(consumer) ⇒ AMQ::Protocol::Basic::ConsumeOk
(also: #consume_with)
Registers a consumer for queue as Consumer instance.
-
#basic_get(queue, opts = {:manual_ack => true}) ⇒ Array
Synchronously fetches a message from the queue, if there are any.
-
#basic_nack(delivery_tag, multiple = false, requeue = false) ⇒ NilClass
Rejects or requeues messages just like #basic_reject but can do so with multiple messages at once.
-
#basic_publish(payload, exchange, routing_key, opts = {}) ⇒ Bunny::Channel
Publishes a message using basic.publish AMQP 0.9.1 method.
-
#basic_publish_batch(payloads, exchange, routing_key, opts = {}) ⇒ self
Publishes multiple messages in a batch with a single mutex acquisition.
-
#basic_qos(prefetch_count, global = false) ⇒ AMQ::Protocol::Basic::QosOk
(also: #prefetch)
Controls message delivery rate using basic.qos AMQP 0.9.1 method.
-
#basic_recover(requeue) ⇒ AMQ::Protocol::Basic::RecoverOk
Redeliver unacknowledged messages.
-
#basic_reject(delivery_tag, requeue = false) ⇒ NilClass
Rejects or requeues a message.
Queue operations (queue.*) collapse
-
#queue_bind(name, exchange, opts = {}) ⇒ AMQ::Protocol::Queue::BindOk
Binds a queue to an exchange using queue.bind AMQP 0.9.1 method.
-
#queue_declare(name, opts = {}) ⇒ AMQ::Protocol::Queue::DeclareOk
Declares a queue using queue.declare AMQP 0.9.1 method.
-
#queue_delete(name, opts = {}) ⇒ AMQ::Protocol::Queue::DeleteOk
Deletes a queue using queue.delete AMQP 0.9.1 method.
-
#queue_purge(name, opts = {}) ⇒ AMQ::Protocol::Queue::PurgeOk
Purges a queue (removes all messages from it) using queue.purge AMQP 0.9.1 method.
-
#queue_unbind(name, exchange, opts = {}) ⇒ AMQ::Protocol::Queue::UnbindOk
Unbinds a queue from an exchange using queue.unbind AMQP 0.9.1 method.
Exchange operations (exchange.*) collapse
-
#exchange_bind(source, destination, opts = {}) ⇒ AMQ::Protocol::Exchange::BindOk
Binds an exchange to another exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
-
#exchange_declare(name, type, opts = {}) ⇒ AMQ::Protocol::Exchange::DeclareOk
Declares a exchange using exchange.declare AMQP 0.9.1 method.
-
#exchange_delete(name, opts = {}) ⇒ AMQ::Protocol::Exchange::DeleteOk
Deletes a exchange using exchange.delete AMQP 0.9.1 method.
-
#exchange_unbind(source, destination, opts = {}) ⇒ AMQ::Protocol::Exchange::UnbindOk
Unbinds an exchange from another exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
Flow control (channel.*) collapse
-
#channel_flow(active) ⇒ AMQ::Protocol::Channel::FlowOk
Enables or disables message flow for the channel.
Transactions (tx.*) collapse
-
#tx_commit ⇒ AMQ::Protocol::Tx::CommitOk
Commits current transaction.
-
#tx_rollback ⇒ AMQ::Protocol::Tx::RollbackOk
Rolls back current transaction.
-
#tx_select ⇒ AMQ::Protocol::Tx::SelectOk
Puts the channel into transaction mode (starts a transaction).
-
#using_tx? ⇒ Boolean
True if this channel has transactions enabled.
Publisher Confirms (confirm.*) collapse
-
#confirm_select(callback = nil, tracking: false, outstanding_limit: nil, confirm_timeout: nil) ⇒ AMQ::Protocol::Confirm::SelectOk
Enables publisher confirms for the channel.
-
#using_publisher_confirmations? ⇒ Boolean
(also: #using_publisher_confirms?)
True if this channel has Publisher Confirms enabled, false otherwise.
-
#wait_for_confirms ⇒ Boolean
Blocks calling thread until confirms are received for all currently unacknowledged published messages.
Misc collapse
-
#generate_consumer_tag(prefix = "bunny") ⇒ String
Unique string supposed to be used as a consumer tag.
-
#synchronize(&block) ⇒ Object
Synchronizes given block using this channel's mutex.
Network Failure Recovery collapse
-
#maybe_reinitialize_consumer_pool! ⇒ Object
private
Used by the Automatic Network Failure Recovery feature.
- #recover_cancelled_consumers! ⇒ Object
-
#recover_confirm_mode ⇒ Object
Recovers publisher confirms mode.
-
#recover_from_network_failure ⇒ Object
Recovers basic.qos setting, exchanges, queues and consumers.
-
#recover_prefetch_setting ⇒ Object
Recovers basic.qos setting.
-
#recover_tx_mode ⇒ Object
Recovers transaction mode.
- #recovers_cancelled_consumers? ⇒ Boolean
Instance Method Summary collapse
-
#close ⇒ Object
Closes the channel.
-
#closed? ⇒ Boolean
True if this channel is closed (manually or because of an exception), false otherwise.
-
#initialize(connection = nil, id = nil, opts = {}) ⇒ Channel
constructor
A new instance of Channel.
- #inspect ⇒ Object
-
#on_error(&block) ⇒ Object
Defines a handler for errors that are not responses to a particular operations (e.g. basic.ack, basic.reject, basic.nack).
-
#on_uncaught_exception(&block) ⇒ Object
Defines a handler for uncaught exceptions in consumers (e.g. delivered message handlers).
-
#open ⇒ Bunny::Channel
Opens the channel and resets its internal state.
-
#open? ⇒ Boolean
True if this channel is open, false otherwise.
- #record_exchange_with(ch, name, type, durable, auto_delete, arguments) ⇒ Object
- #record_queue_with(ch, name, server_named, durable, auto_delete, exclusive, arguments) ⇒ Object
-
#to_s ⇒ String
Brief human-readable representation of the channel.
Constructor Details
#initialize(connection = nil, id = nil, opts = {}) ⇒ Channel
Returns a new instance of Channel.
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 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 |
# File 'lib/bunny/channel.rb', line 183 def initialize(connection = nil, id = nil, opts = {}) work_pool = opts.fetch(:work_pool, ConsumerWorkPool.new(1)) @connection = connection @logger = connection.logger @id = id || @connection.next_channel_id # channel allocator is exhausted if @id < 0 msg = "Cannot open a channel: max number of channels on connection reached. Connection channel_max value: #{@connection.channel_max}" @logger.error(msg) raise msg else @logger.debug { "Allocated channel id: #{@id}" } end @status = :opening @connection.register_channel(self) @queues = Hash.new @exchanges = Hash.new @consumers = Hash.new @work_pool = work_pool # synchronizes frameset delivery. MK. @publishing_mutex = @connection.mutex_impl.new @consumer_mutex = @connection.mutex_impl.new @queue_mutex = @connection.mutex_impl.new @exchange_mutex = @connection.mutex_impl.new @unconfirmed_set_mutex = @connection.mutex_impl.new # Publisher confirm tracking (initialized before reset_continuations) @confirms_tracking_enabled = false @outstanding_limit = nil @confirm_timeout = nil @throttle_publishes = false = {} = @connection.mutex_impl.new @outstanding_limit_cond = nil self.reset_continuations # threads awaiting on continuations. Used to unblock # them when network connection goes down so that busy loops # that perform synchronous operations can work. MK. @threads_waiting_on_continuations = Set.new @threads_waiting_on_confirms_continuations = Set.new @threads_waiting_on_basic_get_continuations = Set.new @next_publish_seq_no = 0 @delivery_tag_offset = 0 @uncaught_exception_handler = Proc.new do |e, consumer| @logger.error "Uncaught exception from consumer #{consumer.to_s}: #{e.inspect} @ #{e.backtrace[0]}" end @cancel_consumers_before_closing = false @last_consumer_tag = nil @last_consumer = nil end |
Instance Attribute Details
#cancel_consumers_before_closing ⇒ Object (readonly)
Returns the value of attribute cancel_consumers_before_closing.
170 171 172 |
# File 'lib/bunny/channel.rb', line 170 def cancel_consumers_before_closing @cancel_consumers_before_closing end |
#confirm_timeout ⇒ Integer? (readonly)
Returns Timeout in milliseconds for waiting on publisher confirms.
161 162 163 |
# File 'lib/bunny/channel.rb', line 161 def confirm_timeout @confirm_timeout end |
#confirms_tracking_enabled ⇒ Boolean (readonly)
Returns true if publisher confirm tracking is enabled.
157 158 159 |
# File 'lib/bunny/channel.rb', line 157 def confirms_tracking_enabled @confirms_tracking_enabled end |
#connection ⇒ Bunny::Session (readonly)
Returns AMQP connection this channel was opened on.
137 138 139 |
# File 'lib/bunny/channel.rb', line 137 def connection @connection end |
#consumers ⇒ Hash<String, Bunny::Consumer> (readonly)
Returns Consumer instances declared on this channel.
163 164 165 |
# File 'lib/bunny/channel.rb', line 163 def consumers @consumers end |
#delivery_tag_offset ⇒ Integer (readonly)
This will be set to the current sequence index during automatic network failure recovery to keep the sequence monotonic for the user and abstract the reset from the protocol
147 148 149 |
# File 'lib/bunny/channel.rb', line 147 def delivery_tag_offset @delivery_tag_offset end |
#exchanges ⇒ Hash<String, Bunny::Exchange> (readonly)
Returns Exchange instances declared on this channel.
151 152 153 |
# File 'lib/bunny/channel.rb', line 151 def exchanges @exchanges end |
#id ⇒ Integer
Returns Channel id.
135 136 137 |
# File 'lib/bunny/channel.rb', line 135 def id @id end |
#nacked_set ⇒ Set<Integer> (readonly)
Returns Set of nacked message indexes that have been nacked.
155 156 157 |
# File 'lib/bunny/channel.rb', line 155 def nacked_set @nacked_set end |
#next_publish_seq_no ⇒ Integer (readonly)
Returns Next publisher confirmations sequence index.
143 144 145 |
# File 'lib/bunny/channel.rb', line 143 def next_publish_seq_no @next_publish_seq_no end |
#outstanding_limit ⇒ Integer? (readonly)
Returns Maximum outstanding unconfirmed messages before throttling.
159 160 161 |
# File 'lib/bunny/channel.rb', line 159 def outstanding_limit @outstanding_limit end |
#prefetch_count ⇒ Integer (readonly)
Returns active basic.qos prefetch value.
166 167 168 |
# File 'lib/bunny/channel.rb', line 166 def prefetch_count @prefetch_count end |
#prefetch_global ⇒ Integer (readonly)
Returns active basic.qos prefetch global mode.
168 169 170 |
# File 'lib/bunny/channel.rb', line 168 def prefetch_global @prefetch_global end |
#queues ⇒ Hash<String, Bunny::Queue> (readonly)
Returns Queue instances declared on this channel.
149 150 151 |
# File 'lib/bunny/channel.rb', line 149 def queues @queues end |
#status ⇒ Symbol (readonly)
Returns Channel status (:opening, :open, :closed).
139 140 141 |
# File 'lib/bunny/channel.rb', line 139 def status @status end |
#unconfirmed_set ⇒ Set<Integer> (readonly)
Returns Set of published message indexes that are currently unconfirmed.
153 154 155 |
# File 'lib/bunny/channel.rb', line 153 def unconfirmed_set @unconfirmed_set end |
#work_pool ⇒ Bunny::ConsumerWorkPool (readonly)
Returns Thread pool delivered messages are dispatched to.
141 142 143 |
# File 'lib/bunny/channel.rb', line 141 def work_pool @work_pool end |
Instance Method Details
#ack(delivery_tag, multiple = false) ⇒ Object Also known as: acknowledge
Acknowledges a message. Acknowledged messages are completely removed from the queue.
615 616 617 |
# File 'lib/bunny/channel.rb', line 615 def ack(delivery_tag, multiple = false) basic_ack(delivery_tag.to_i, multiple) end |
#active ⇒ Boolean
Returns true if this channel is open.
322 323 324 |
# File 'lib/bunny/channel.rb', line 322 def active open? end |
#any_consumers? ⇒ Boolean
Returns true if there are consumers on this channel.
1222 1223 1224 |
# File 'lib/bunny/channel.rb', line 1222 def any_consumers? @consumer_mutex.synchronize { @consumers.any? } end |
#basic_ack(delivery_tag, multiple = false) ⇒ NilClass
Acknowledges a delivery (message).
993 994 995 996 997 998 999 1000 |
# File 'lib/bunny/channel.rb', line 993 def basic_ack(delivery_tag, multiple = false) (delivery_tag) do raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Ack.encode(@id, delivery_tag, multiple)) nil end end |
#basic_cancel(consumer_tag, opts = {}) ⇒ AMQ::Protocol::Basic::CancelOk?
Removes a 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.
1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 |
# File 'lib/bunny/channel.rb', line 1200 def basic_cancel(consumer_tag, opts = {}) no_wait = opts.fetch(:no_wait, false) @connection.send_frame(AMQ::Protocol::Basic::Cancel.encode(@id, consumer_tag, no_wait)) if no_wait @last_basic_cancel_ok = nil else with_continuation_timeout do @last_basic_cancel_ok = wait_on_continuations end end # reduces thread usage for channels that don't have any # consumers @work_pool.shutdown(true) unless self.any_consumers? self.delete_recorded_consumer(consumer_tag) @last_basic_cancel_ok end |
#basic_consume(queue, consumer_tag = generate_consumer_tag, no_ack = false, exclusive = false, arguments = nil, &block) ⇒ AMQ::Protocol::Basic::ConsumeOk Also known as: consume
Registers a consumer for queue. Delivered messages will be handled with the block provided to this method.
1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 |
# File 'lib/bunny/channel.rb', line 1080 def basic_consume(queue, consumer_tag = generate_consumer_tag, no_ack = false, exclusive = false, arguments = nil, &block) raise_if_no_longer_open! maybe_start_consumer_work_pool! queue_name = if queue.respond_to?(:name) queue.name else queue end # helps avoid race condition between basic.consume-ok and basic.deliver if there are messages # in the queue already. MK. if consumer_tag && consumer_tag.strip != AMQ::Protocol::EMPTY_STRING add_consumer(queue_name, consumer_tag, no_ack, exclusive, arguments || {}, &block) end @connection.send_frame(AMQ::Protocol::Basic::Consume.encode(@id, queue_name, consumer_tag, false, no_ack, exclusive, false, arguments)) begin with_continuation_timeout do @last_basic_consume_ok = wait_on_continuations end rescue Exception => e # if basic.consume-ok never arrives, unregister the proactively # registered consumer. MK. unregister_consumer(@last_basic_consume_ok.consumer_tag) # #add_consumer records a consumer, make sure to undo it here. MK. delete_recorded_consumer(@last_basic_consume_ok.consumer_tag) raise e end # in case there is another exclusive consumer and we get a channel.close # response here. MK. raise_if_channel_close!(@last_basic_consume_ok) # covers server-generated consumer tags add_consumer(queue_name, @last_basic_consume_ok.consumer_tag, no_ack, exclusive, arguments || {}, &block) @last_basic_consume_ok end |
#basic_consume_with(consumer) ⇒ AMQ::Protocol::Basic::ConsumeOk Also known as: consume_with
Registers a consumer for queue as Bunny::Consumer instance.
1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 |
# File 'lib/bunny/channel.rb', line 1138 def basic_consume_with(consumer) raise_if_no_longer_open! maybe_start_consumer_work_pool! # helps avoid race condition between basic.consume-ok and basic.deliver if there are messages # in the queue already. MK. if consumer.consumer_tag && consumer.consumer_tag.strip != AMQ::Protocol::EMPTY_STRING register_consumer(consumer.consumer_tag, consumer) end @connection.send_frame(AMQ::Protocol::Basic::Consume.encode(@id, consumer.queue_name, consumer.consumer_tag, false, consumer.no_ack, consumer.exclusive, false, consumer.arguments)) begin with_continuation_timeout do @last_basic_consume_ok = wait_on_continuations end rescue Exception => e # if basic.consume-ok never arrives, unregister the proactively # registered consumer. MK. unregister_consumer(@last_basic_consume_ok.consumer_tag) raise e end # in case there is another exclusive consumer and we get a channel.close # response here. MK. raise_if_channel_close!(@last_basic_consume_ok) # covers server-generated consumer tags register_consumer(@last_basic_consume_ok.consumer_tag, consumer) record_consumer_with(self, @last_basic_consume_ok.consumer_tag, consumer.queue_name, consumer, consumer.manual_acknowledgement?, consumer.exclusive, consumer.arguments) raise_if_continuation_resulted_in_a_channel_error! @last_basic_consume_ok end |
#basic_get(queue, opts = {:manual_ack => true}) ⇒ Array
Synchronously fetches a message from the queue, if there are any. This method is for cases when the convenience of synchronous operations is more important than throughput.
822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 |
# File 'lib/bunny/channel.rb', line 822 def basic_get(queue, opts = {:manual_ack => true}) raise_if_no_longer_open! unless opts[:ack].nil? warn "[DEPRECATION] `:ack` is deprecated. Please use `:manual_ack` instead." opts[:manual_ack] = opts[:ack] end @connection.send_frame(AMQ::Protocol::Basic::Get.encode(@id, queue, !(opts[:manual_ack]))) # this is a workaround for the edge case when basic_get is called in a tight loop # and network goes down we need to perform recovery. The problem is, basic_get will # keep blocking the thread that calls it without clear way to constantly unblock it # from the network activity loop (where recovery happens) with the current continuations # implementation (and even more correct and convenient ones, such as wait/notify, should # we implement them). So we return a triple of nils immediately which apps should be # able to handle anyway as "got no message, no need to act". MK. last_basic_get_response = if @connection.open? begin wait_on_basic_get_continuations rescue Timeout::Error => e raise_if_continuation_resulted_in_a_channel_error! raise e end else [nil, nil, nil] end raise_if_continuation_resulted_in_a_channel_error! last_basic_get_response end |
#basic_nack(delivery_tag, multiple = false, requeue = false) ⇒ NilClass
Rejects or requeues messages just like #basic_reject but can do so with multiple messages at once.
1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 |
# File 'lib/bunny/channel.rb', line 1055 def basic_nack(delivery_tag, multiple = false, requeue = false) (delivery_tag) do raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Nack.encode(@id, delivery_tag, multiple, requeue)) nil end end |
#basic_publish(payload, exchange, routing_key, opts = {}) ⇒ Bunny::Channel
Publishes a message using basic.publish AMQP 0.9.1 method.
666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 |
# File 'lib/bunny/channel.rb', line 666 def basic_publish(payload, exchange, routing_key, opts = {}) raise_if_no_longer_open! raise ArgumentError, "routing key cannot be longer than #{SHORTSTR_LIMIT} characters" if routing_key && routing_key.size > SHORTSTR_LIMIT exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end mode = if opts.fetch(:persistent, true) 2 else 1 end opts[:delivery_mode] ||= mode opts[:content_type] ||= DEFAULT_CONTENT_TYPE opts[:priority] ||= 0 seq_no = nil continuation = nil if @next_publish_seq_no > 0 @unconfirmed_set_mutex.synchronize do # With outstanding_limit: wait for slot if at the limit wait_for_outstanding_slot_locked if @throttle_publishes seq_no = @next_publish_seq_no @unconfirmed_set.add(seq_no) @next_publish_seq_no += 1 # Only create per-message continuation when blocking individually (no limit) if @confirms_tracking_enabled && !@throttle_publishes continuation = new_continuation .synchronize do [seq_no] = continuation end end end end frames = AMQ::Protocol::Basic::Publish.encode(@id, payload, opts, exchange_name, routing_key, opts[:mandatory], false, @connection.frame_max) @connection.send_frameset(frames, self) wait_for_publish_confirm(seq_no, continuation) if continuation self end |
#basic_publish_batch(payloads, exchange, routing_key, opts = {}) ⇒ self
Publishes multiple messages in a batch with a single mutex acquisition. More efficient than calling basic_publish repeatedly when using publisher confirms with tracking. Recommended batch sizes: 500-3000.
739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 |
# File 'lib/bunny/channel.rb', line 739 def basic_publish_batch(payloads, exchange, routing_key, opts = {}) raise_if_no_longer_open! raise ArgumentError, "payloads must be an Array" unless payloads.is_a?(Array) raise ArgumentError, "routing key cannot be longer than #{SHORTSTR_LIMIT} characters" if routing_key && routing_key.size > SHORTSTR_LIMIT return self if payloads.empty? exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end mode = opts.fetch(:persistent, true) ? 2 : 1 opts = opts.dup opts[:delivery_mode] ||= mode opts[:content_type] ||= DEFAULT_CONTENT_TYPE opts[:priority] ||= 0 batch_size = payloads.size if @next_publish_seq_no > 0 @unconfirmed_set_mutex.synchronize do # With throttling: wait until we have room for the batch if @throttle_publishes limit = @outstanding_limit target = [limit - batch_size, 0].max timeout_sec = (@confirm_timeout || @connection.continuation_timeout) / 1000.0 deadline = nil while @unconfirmed_set.size > target raise_if_no_longer_open! deadline ||= Bunny::Timestamp.monotonic + timeout_sec remaining = deadline - Bunny::Timestamp.monotonic raise Timeout::Error, "Timed out waiting for publisher confirms (batch: #{batch_size}, limit: #{limit})" if remaining <= 0 @outstanding_limit_cond.wait(remaining) end end # Register all sequence numbers at once start_seq = @next_publish_seq_no batch_size.times { |i| @unconfirmed_set.add(start_seq + i) } @next_publish_seq_no = start_seq + batch_size end end # Send all frames (outside the mutex) payloads.each do |payload| frames = AMQ::Protocol::Basic::Publish.encode(@id, payload, opts, exchange_name, routing_key, opts[:mandatory], false, @connection.frame_max) @connection.send_frameset(frames, self) end self end |
#basic_qos(prefetch_count, global = false) ⇒ AMQ::Protocol::Basic::QosOk Also known as: prefetch
Controls message delivery rate using basic.qos AMQP 0.9.1 method.
873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 |
# File 'lib/bunny/channel.rb', line 873 def basic_qos(prefetch_count, global = false) raise ArgumentError.new("prefetch count must be a positive integer, given: #{prefetch_count}") if prefetch_count < 0 raise ArgumentError.new("prefetch count must be no greater than #{MAX_PREFETCH_COUNT}, given: #{prefetch_count}") if prefetch_count > MAX_PREFETCH_COUNT raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Qos.encode(@id, 0, prefetch_count, global)) with_continuation_timeout do @last_basic_qos_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @prefetch_count = prefetch_count @prefetch_global = global @last_basic_qos_ok end |
#basic_recover(requeue) ⇒ AMQ::Protocol::Basic::RecoverOk
Redeliver unacknowledged messages
897 898 899 900 901 902 903 904 905 906 907 |
# File 'lib/bunny/channel.rb', line 897 def basic_recover(requeue) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Recover.encode(@id, requeue)) with_continuation_timeout do @last_basic_recover_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_basic_recover_ok end |
#basic_reject(delivery_tag, requeue = false) ⇒ NilClass
Rejects or requeues a message.
947 948 949 950 951 952 |
# File 'lib/bunny/channel.rb', line 947 def basic_reject(delivery_tag, requeue = false) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Reject.encode(@id, delivery_tag, requeue)) nil end |
#cancel_consumers_before_closing! ⇒ Object
346 347 348 |
# File 'lib/bunny/channel.rb', line 346 def cancel_consumers_before_closing! @cancel_consumers_before_closing = true end |
#cancel_consumers_before_closing? ⇒ Boolean
350 351 352 |
# File 'lib/bunny/channel.rb', line 350 def cancel_consumers_before_closing? !!@cancel_consumers_before_closing end |
#channel_flow(active) ⇒ AMQ::Protocol::Channel::FlowOk
Recent (e.g. 2.8.x., 3.x) RabbitMQ will employ TCP/IP-level back pressure on publishers if it detects that consumers do not keep up with them.
Enables or disables message flow for the channel. When message flow is disabled, no new messages will be delivered to consumers on this channel. This is typically used by consumers that cannot keep up with the influx of messages.
1705 1706 1707 1708 1709 1710 1711 1712 1713 1714 1715 |
# File 'lib/bunny/channel.rb', line 1705 def channel_flow(active) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Channel::Flow.encode(@id, active)) with_continuation_timeout do @last_channel_flow_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_channel_flow_ok end |
#client ⇒ Bunny::Session
Returns Connection this channel was opened on.
327 328 329 |
# File 'lib/bunny/channel.rb', line 327 def client @connection end |
#close ⇒ Object
Closes the channel. Closed channels can no longer be used (this includes associated Queue, Exchange and Bunny::Consumer instances.
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 |
# File 'lib/bunny/channel.rb', line 273 def close # see bunny#528 raise_if_no_longer_open! # This is a best-effort attempt to cancel all consumers before closing the channel. # Retries are extremely unlikely to succeed, and the channel itself is about to be closed, # so we don't bother retrying. if self.cancel_consumers_before_closing? # cancelling a consumer involves using the same mutex, so avoid holding the lock keys = @consumer_mutex.synchronize { @consumers.keys } keys.each do |ctag| begin self.basic_cancel(ctag) rescue Bunny::Exception # ignore rescue Bunny::ClientTimeout # ignore end end end @connection.close_channel(self) @status = :closed @work_pool.shutdown maybe_kill_consumer_work_pool! end |
#closed? ⇒ Boolean
Returns true if this channel is closed (manually or because of an exception), false otherwise.
308 309 310 |
# File 'lib/bunny/channel.rb', line 308 def closed? @status == :closed end |
#configure(&block) ⇒ Object
340 341 342 343 344 |
# File 'lib/bunny/channel.rb', line 340 def configure(&block) block.call(self) if block_given? self end |
#confirm_select(callback = nil, tracking: false, outstanding_limit: nil, confirm_timeout: nil) ⇒ AMQ::Protocol::Confirm::SelectOk
Enables publisher confirms for the channel.
1802 1803 1804 1805 1806 1807 1808 1809 1810 1811 1812 1813 1814 1815 1816 1817 1818 1819 1820 1821 1822 1823 1824 1825 1826 1827 1828 1829 1830 1831 1832 1833 1834 1835 |
# File 'lib/bunny/channel.rb', line 1802 def confirm_select(callback = nil, tracking: false, outstanding_limit: nil, confirm_timeout: nil) raise_if_no_longer_open! raise ArgumentError, "outstanding_limit requires tracking: true" if outstanding_limit && !tracking raise ArgumentError, "outstanding_limit must be positive" if outstanding_limit && outstanding_limit < 1 raise ArgumentError, "confirm_timeout must be positive" if confirm_timeout && confirm_timeout < 1 if @next_publish_seq_no == 0 @confirms_continuations = new_continuation @unconfirmed_set = Set.new @nacked_set = Set.new @next_publish_seq_no = 1 @only_acks_received = true end @confirms_callback = callback @confirms_tracking_enabled = tracking # Default to optimal limit when tracking enabled (avoids per-message mutex) @outstanding_limit = outstanding_limit || (tracking ? DEFAULT_OUTSTANDING_CONFIRMS_LIMIT : nil) @confirm_timeout = confirm_timeout # Cache combined check for fast path in basic_publish @throttle_publishes = tracking && @outstanding_limit if @outstanding_limit && !@outstanding_limit_cond @outstanding_limit_cond = @unconfirmed_set_mutex.new_cond end @connection.send_frame(AMQ::Protocol::Confirm::Select.encode(@id, false)) with_continuation_timeout do @last_confirm_select_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_confirm_select_ok end |
#default_exchange ⇒ Object
Provides access to the default exchange
437 438 439 |
# File 'lib/bunny/channel.rb', line 437 def default_exchange @default_exchange ||= Exchange.default(self) end |
#direct(name, opts = {}) ⇒ Bunny::Exchange
Declares a direct exchange or looks it up in the cache of previously declared exchanges.
394 395 396 |
# File 'lib/bunny/channel.rb', line 394 def direct(name, opts = {}) find_exchange(name) || Exchange.new(self, :direct, name, opts) end |
#durable_queue(name, type = "classic", opts = {}) ⇒ Bunny::Queue
Declares a new server-named queue that is automatically deleted when the connection is closed.
538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 |
# File 'lib/bunny/channel.rb', line 538 def durable_queue(name, type = "classic", opts = {}) throw ArgumentError.new("queue name must not be nil") if name.nil? throw ArgumentError.new("queue name must not be empty (server-named durable queues do not make sense)") if name.empty? final_opts = opts.merge({ :type => type, :durable => true, # exclusive or auto-delete QQs do not make much sense :exclusive => false, :auto_delete => false }) q = find_queue(name) || Bunny::Queue.new(self, name, final_opts) record_queue(q) register_queue(q) end |
#exchange(name, opts = {}) ⇒ Bunny::Exchange
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
455 456 457 |
# File 'lib/bunny/channel.rb', line 455 def exchange(name, opts = {}) find_exchange(name) || Exchange.new(self, opts.fetch(:type, :direct), name, opts) end |
#exchange_bind(source, destination, opts = {}) ⇒ AMQ::Protocol::Exchange::BindOk
Binds an exchange to another exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
1585 1586 1587 1588 1589 1590 1591 1592 1593 1594 1595 1596 1597 1598 1599 1600 1601 1602 1603 |
# File 'lib/bunny/channel.rb', line 1585 def exchange_bind(source, destination, opts = {}) result = self.exchange_bind_without_recording_topology(source, destination, opts) source_name = if source.respond_to?(:name) source.name else source end destination_name = if destination.respond_to?(:name) destination.name else destination end rk = (opts[:routing_key] || opts[:key]) args = opts[:arguments] self.record_exchange_binding_with(self, source_name, destination_name, rk, args) result end |
#exchange_declare(name, type, opts = {}) ⇒ AMQ::Protocol::Exchange::DeclareOk
Declares a exchange using exchange.declare AMQP 0.9.1 method.
1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 |
# File 'lib/bunny/channel.rb', line 1479 def exchange_declare(name, type, opts = {}) result = self.exchange_declare_without_recording_topology(name, type, opts) # strip trailing new line and carriage returns # just like RabbitMQ does safe_name = name.gsub(/[\r\n]/, "") passive = opts.fetch(:passive, false) durable = opts.fetch(:durable, false) auto_delete = opts.fetch(:auto_delete, false) args = opts[:arguments] self.record_exchange_with(self, safe_name, type.to_s, durable, auto_delete, args) unless passive result end |
#exchange_delete(name, opts = {}) ⇒ AMQ::Protocol::Exchange::DeleteOk
Deletes a exchange using exchange.delete AMQP 0.9.1 method
1552 1553 1554 1555 1556 1557 1558 1559 1560 1561 1562 1563 1564 1565 1566 1567 1568 |
# File 'lib/bunny/channel.rb', line 1552 def exchange_delete(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Exchange::Delete.encode(@id, name, opts[:if_unused], false)) with_continuation_timeout do @last_exchange_delete_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! self.delete_recorded_exchange_named(name) self.deregister_exchange_named(name) @last_exchange_delete_ok end |
#exchange_unbind(source, destination, opts = {}) ⇒ AMQ::Protocol::Exchange::UnbindOk
Unbinds an exchange from another exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
1656 1657 1658 1659 1660 1661 1662 1663 1664 1665 1666 1667 1668 1669 1670 1671 1672 1673 1674 1675 1676 1677 1678 1679 1680 1681 1682 1683 1684 1685 1686 1687 |
# File 'lib/bunny/channel.rb', line 1656 def exchange_unbind(source, destination, opts = {}) raise_if_no_longer_open! source_name = if source.respond_to?(:name) source.name else source end destination_name = if destination.respond_to?(:name) destination.name else destination end rk = (opts[:routing_key] || opts[:key]) args = opts[:arguments] @connection.send_frame(AMQ::Protocol::Exchange::Unbind.encode(@id, destination_name, source_name, rk, false, args)) with_continuation_timeout do @last_exchange_unbind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! self.delete_recorded_exchange_binding(self, source_name, destination_name, rk, args) @last_exchange_unbind_ok end |
#fanout(name, opts = {}) ⇒ Bunny::Exchange
Declares a fanout exchange or looks it up in the cache of previously declared exchanges.
376 377 378 |
# File 'lib/bunny/channel.rb', line 376 def fanout(name, opts = {}) find_exchange(name) || Exchange.new(self, :fanout, name, opts) end |
#flow(active) ⇒ Object
Flow control. When set to false, RabbitMQ will stop delivering messages on this channel.
578 579 580 |
# File 'lib/bunny/channel.rb', line 578 def flow(active) channel_flow(active) end |
#generate_consumer_tag(prefix = "bunny") ⇒ String
Unique string supposed to be used as a consumer tag.
1867 1868 1869 1870 |
# File 'lib/bunny/channel.rb', line 1867 def generate_consumer_tag(prefix = "bunny") t = Bunny::Timestamp.now "#{prefix}-#{t.to_i * 1000}-#{Kernel.rand(999_999_999_999)}" end |
#headers(name, opts = {}) ⇒ Bunny::Exchange
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
430 431 432 |
# File 'lib/bunny/channel.rb', line 430 def headers(name, opts = {}) find_exchange(name) || Exchange.new(self, :headers, name, opts) end |
#inspect ⇒ Object
2005 2006 2007 |
# File 'lib/bunny/channel.rb', line 2005 def inspect to_s end |
#maybe_reinitialize_consumer_pool! ⇒ Object
This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.
Used by the Automatic Network Failure Recovery feature.
1980 1981 1982 1983 1984 1985 |
# File 'lib/bunny/channel.rb', line 1980 def maybe_reinitialize_consumer_pool! unless @consumers.empty? @work_pool = ConsumerWorkPool.new(@work_pool.size, @work_pool.abort_on_exception) @work_pool.start end end |
#nack(delivery_tag, multiple = false, requeue = false) ⇒ Object
Rejects a message. A rejected message can be requeued or dropped by RabbitMQ. This method is similar to #reject but supports rejecting multiple messages at once, and is usually preferred.
630 631 632 |
# File 'lib/bunny/channel.rb', line 630 def nack(delivery_tag, multiple = false, requeue = false) basic_nack(delivery_tag.to_i, multiple, requeue) end |
#number ⇒ Integer
Returns Channel id.
317 318 319 |
# File 'lib/bunny/channel.rb', line 317 def number self.id end |
#on_error(&block) ⇒ Object
Defines a handler for errors that are not responses to a particular operations (e.g. basic.ack, basic.reject, basic.nack).
1883 1884 1885 |
# File 'lib/bunny/channel.rb', line 1883 def on_error(&block) @on_error = block end |
#on_uncaught_exception(&block) ⇒ Object
Defines a handler for uncaught exceptions in consumers (e.g. delivered message handlers).
1891 1892 1893 |
# File 'lib/bunny/channel.rb', line 1891 def on_uncaught_exception(&block) @uncaught_exception_handler = block end |
#open ⇒ Bunny::Channel
Opens the channel and resets its internal state
256 257 258 259 260 261 262 263 264 265 266 267 268 |
# File 'lib/bunny/channel.rb', line 256 def open @threads_waiting_on_continuations = Set.new @threads_waiting_on_confirms_continuations = Set.new @threads_waiting_on_basic_get_continuations = Set.new @connection.open_channel(self) # clear last channel error @last_channel_error = nil @status = :open self end |
#open? ⇒ Boolean
Returns true if this channel is open, false otherwise.
302 303 304 |
# File 'lib/bunny/channel.rb', line 302 def open? @status == :open end |
#queue(name = AMQ::Protocol::EMPTY_STRING, opts = {}) ⇒ Bunny::Queue
Declares a queue or looks it up in the per-channel cache.
478 479 480 481 482 483 484 485 |
# File 'lib/bunny/channel.rb', line 478 def queue(name = AMQ::Protocol::EMPTY_STRING, opts = {}) throw ArgumentError.new("queue name must not be nil") if name.nil? q = find_queue(name) || Bunny::Queue.new(self, name, opts) record_queue(q) register_queue(q) end |
#queue_bind(name, exchange, opts = {}) ⇒ AMQ::Protocol::Queue::BindOk
Binds a queue to an exchange using queue.bind AMQP 0.9.1 method
1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 |
# File 'lib/bunny/channel.rb', line 1371 def queue_bind(name, exchange, opts = {}) raise_if_no_longer_open! exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end rk = (opts[:routing_key] || opts[:key]) args = opts[:arguments] result = self.queue_bind_without_recording_topology(name, exchange, opts) self.record_queue_binding_with(self, exchange_name, name, rk, args) result end |
#queue_declare(name, opts = {}) ⇒ AMQ::Protocol::Queue::DeclareOk
Declares a queue using queue.declare AMQP 0.9.1 method.
1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 |
# File 'lib/bunny/channel.rb', line 1249 def queue_declare(name, opts = {}) # strip trailing new line and carriage returns # just like RabbitMQ does safe_name = name.gsub(/[\r\n]/, "") is_server_named = (safe_name == AMQ::Protocol::EMPTY_STRING) passive = opts.fetch(:passive, false) durable = opts.fetch(:durable, false) exclusive = opts.fetch(:exclusive, false) auto_delete = opts.fetch(:auto_delete, false) args = opts[:arguments] result = self.queue_declare_without_recording_topology(name, opts) self.record_queue_with(self, result.queue, is_server_named, durable, exclusive, auto_delete, args) unless passive result end |
#queue_delete(name, opts = {}) ⇒ AMQ::Protocol::Queue::DeleteOk
Deletes a queue using queue.delete AMQP 0.9.1 method
1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 |
# File 'lib/bunny/channel.rb', line 1320 def queue_delete(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Queue::Delete.encode(@id, name, opts[:if_unused], opts[:if_empty], false)) with_continuation_timeout do @last_queue_delete_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! self.delete_recorded_queue_named(name) self.deregister_queue_named(name) @last_queue_delete_ok end |
#queue_purge(name, opts = {}) ⇒ AMQ::Protocol::Queue::PurgeOk
Purges a queue (removes all messages from it) using queue.purge AMQP 0.9.1 method.
1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 |
# File 'lib/bunny/channel.rb', line 1345 def queue_purge(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Queue::Purge.encode(@id, name, false)) with_continuation_timeout do @last_queue_purge_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_queue_purge_ok end |
#queue_unbind(name, exchange, opts = {}) ⇒ AMQ::Protocol::Queue::UnbindOk
Unbinds a queue from an exchange using queue.unbind AMQP 0.9.1 method
1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 |
# File 'lib/bunny/channel.rb', line 1432 def queue_unbind(name, exchange, opts = {}) raise_if_no_longer_open! exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end rk = (opts[:routing_key] || opts[:key]) args = opts[:arguments] @connection.send_frame(AMQ::Protocol::Queue::Unbind.encode(@id, name, exchange_name, rk, args)) with_continuation_timeout do @last_queue_unbind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! self.delete_recorded_queue_binding(self, exchange_name, name, rk, args) @last_queue_unbind_ok end |
#quorum_queue(name, opts = {}) ⇒ Bunny::Queue
Declares a new client-named quorum queue.
498 499 500 501 502 503 |
# File 'lib/bunny/channel.rb', line 498 def quorum_queue(name, opts = {}) throw ArgumentError.new("quorum queue name must not be nil") if name.nil? throw ArgumentError.new("quorum queue name must not be empty (server-named QQs do not make sense)") if name.empty? durable_queue(name, Bunny::Queue::Types::QUORUM, opts) end |
#record_exchange_with(ch, name, type, durable, auto_delete, arguments) ⇒ Object
2521 2522 2523 |
# File 'lib/bunny/channel.rb', line 2521 def record_exchange_with(ch, name, type, durable, auto_delete, arguments) @connection.record_exchange_with(ch, name, type, durable, auto_delete, arguments) end |
#record_queue_with(ch, name, server_named, durable, auto_delete, exclusive, arguments) ⇒ Object
2475 2476 2477 |
# File 'lib/bunny/channel.rb', line 2475 def record_queue_with(ch, name, server_named, durable, auto_delete, exclusive, arguments) @connection.record_queue_with(ch, name, server_named, durable, auto_delete, exclusive, arguments) end |
#recover(ignored = true) ⇒ Object
Tells RabbitMQ to redeliver unacknowledged messages
584 585 586 587 |
# File 'lib/bunny/channel.rb', line 584 def recover(ignored = true) # RabbitMQ only supports basic.recover with requeue = true basic_recover(true) end |
#recover_cancelled_consumers! ⇒ Object
1988 1989 1990 |
# File 'lib/bunny/channel.rb', line 1988 def recover_cancelled_consumers! @recover_cancelled_consumers = true end |
#recover_confirm_mode ⇒ Object
Recovers publisher confirms mode. Used by the Automatic Network Failure Recovery feature. Set the offset to the previous publish sequence index as the protocol will reset the index to after recovery.
1929 1930 1931 1932 1933 1934 1935 1936 1937 1938 1939 1940 1941 1942 1943 1944 1945 1946 1947 1948 1949 1950 |
# File 'lib/bunny/channel.rb', line 1929 def recover_confirm_mode return unless using_publisher_confirmations? @unconfirmed_set_mutex.synchronize do @unconfirmed_set.clear @delivery_tag_offset = @next_publish_seq_no - 1 if @confirms_tracking_enabled .synchronize do .each_value { |c| c.push(:network_error) } .clear end end @outstanding_limit_cond&.broadcast end confirm_select(@confirms_callback, tracking: @confirms_tracking_enabled, outstanding_limit: @outstanding_limit, confirm_timeout: @confirm_timeout) end |
#recover_from_network_failure ⇒ Object
Recovers basic.qos setting, exchanges, queues and consumers. Used by the Automatic Network Failure Recovery feature.
1905 1906 1907 1908 1909 1910 1911 1912 1913 1914 |
# File 'lib/bunny/channel.rb', line 1905 def recover_from_network_failure @logger.debug { "Recovering channel #{@id} after network failure" } release_all_continuations recover_prefetch_setting recover_confirm_mode recover_tx_mode # Topology is now recovered by [Bunny::Session] via the data in [Bunny::TopologyRegistry]. end |
#recover_prefetch_setting ⇒ Object
Recovers basic.qos setting. Used by the Automatic Network Failure Recovery feature.
1920 1921 1922 |
# File 'lib/bunny/channel.rb', line 1920 def recover_prefetch_setting basic_qos(@prefetch_count, @prefetch_global) if @prefetch_count end |
#recover_tx_mode ⇒ Object
Recovers transaction mode. Used by the Automatic Network Failure Recovery feature.
1956 1957 1958 |
# File 'lib/bunny/channel.rb', line 1956 def recover_tx_mode tx_select if @tx_mode end |
#recovers_cancelled_consumers? ⇒ Boolean
1993 1994 1995 |
# File 'lib/bunny/channel.rb', line 1993 def recovers_cancelled_consumers? !!@recover_cancelled_consumers end |
#reject(delivery_tag, requeue = false) ⇒ Object
Rejects a message. A rejected message can be requeued or dropped by RabbitMQ.
604 605 606 |
# File 'lib/bunny/channel.rb', line 604 def reject(delivery_tag, requeue = false) basic_reject(delivery_tag.to_i, requeue) end |
#stream(name, opts = {}) ⇒ Bunny::Queue
Declares a new client-named stream (that Bunny can use as if it was a queue). Note that Bunny would still use AMQP 0-9-1 to perform operations on this "queue". To use stream-specific operations and to gain from stream protocol efficiency and partitioning, use a Ruby client for the RabbitMQ stream protocol.
520 521 522 523 524 525 |
# File 'lib/bunny/channel.rb', line 520 def stream(name, opts = {}) throw ArgumentError.new("stream name must not be nil") if name.nil? throw ArgumentError.new("stream name must not be empty (server-named QQs do not make sense)") if name.empty? durable_queue(name, Bunny::Queue::Types::STREAM, opts) end |
#synchronize(&block) ⇒ Object
Synchronizes given block using this channel's mutex.
1859 1860 1861 |
# File 'lib/bunny/channel.rb', line 1859 def synchronize(&block) @publishing_mutex.synchronize(&block) end |
#temporary_queue(opts = {}) ⇒ Bunny::Queue
Declares a new server-named queue that is automatically deleted when the connection is closed.
561 562 563 564 565 566 |
# File 'lib/bunny/channel.rb', line 561 def temporary_queue(opts = {}) temporary_queue_opts = { :exclusive => true } queue("", opts.merge(temporary_queue_opts)) end |
#to_s ⇒ String
Returns Brief human-readable representation of the channel.
2001 2002 2003 |
# File 'lib/bunny/channel.rb', line 2001 def to_s "#<#{self.class.name}:#{object_id} @id=#{self.number} @connection=#{@connection.to_s} @open=#{open?}>" end |
#topic(name, opts = {}) ⇒ Bunny::Exchange
Declares a topic exchange or looks it up in the cache of previously declared exchanges.
412 413 414 |
# File 'lib/bunny/channel.rb', line 412 def topic(name, opts = {}) find_exchange(name) || Exchange.new(self, :topic, name, opts) end |
#tx_commit ⇒ AMQ::Protocol::Tx::CommitOk
Commits current transaction
1742 1743 1744 1745 1746 1747 1748 1749 1750 1751 1752 |
# File 'lib/bunny/channel.rb', line 1742 def tx_commit raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Commit.encode(@id)) with_continuation_timeout do @last_tx_commit_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_tx_commit_ok end |
#tx_rollback ⇒ AMQ::Protocol::Tx::RollbackOk
Rolls back current transaction
1757 1758 1759 1760 1761 1762 1763 1764 1765 1766 1767 |
# File 'lib/bunny/channel.rb', line 1757 def tx_rollback raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Rollback.encode(@id)) with_continuation_timeout do @last_tx_rollback_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_tx_rollback_ok end |
#tx_select ⇒ AMQ::Protocol::Tx::SelectOk
Puts the channel into transaction mode (starts a transaction)
1726 1727 1728 1729 1730 1731 1732 1733 1734 1735 1736 1737 |
# File 'lib/bunny/channel.rb', line 1726 def tx_select raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Select.encode(@id)) with_continuation_timeout do @last_tx_select_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @tx_mode = true @last_tx_select_ok end |
#using_publisher_confirmations? ⇒ Boolean Also known as: using_publisher_confirms?
Returns true if this channel has Publisher Confirms enabled, false otherwise.
1782 1783 1784 |
# File 'lib/bunny/channel.rb', line 1782 def using_publisher_confirmations? @next_publish_seq_no > 0 end |
#using_tx? ⇒ Boolean
Returns true if this channel has transactions enabled.
1770 1771 1772 |
# File 'lib/bunny/channel.rb', line 1770 def using_tx? !!@tx_mode end |
#wait_for_confirms ⇒ Boolean
Blocks calling thread until confirms are received for all currently unacknowledged published messages. Returns immediately if there are no outstanding confirms.
1847 1848 1849 1850 |
# File 'lib/bunny/channel.rb', line 1847 def wait_for_confirms wait_on_confirms_continuations read_and_reset_only_acks_received end |