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:
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.
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.
Instance Attribute Summary (collapse)
-
- (Bunny::Session) connection
readonly
AMQP connection this channel was opened on.
-
- (Hash<String, Bunny::Consumer>) consumers
readonly
Consumer instances declared on this channel.
-
- (Hash<String, Bunny::Exchange>) exchanges
readonly
Exchange instances declared on this channel.
-
- (Integer) id
Channel id.
-
- (Set<Integer>) nacked_set
readonly
Set of nacked message indexes that have been nacked.
-
- (Integer) next_publish_seq_no
readonly
Next publisher confirmations sequence index.
-
- (Hash<String, Bunny::Queue>) queues
readonly
Queue instances declared on this channel.
-
- (Symbol) status
readonly
Channel status (:opening, :open, :closed).
-
- (Set<Integer>) unconfirmed_set
readonly
Set of published message indexes that are currently unconfirmed.
-
- (Bunny::ConsumerWorkPool) work_pool
readonly
Thread pool delivered messages are dispatched to.
Backwards compatibility with 0.8.0 (collapse)
-
- (Boolean) active
True if this channel is open.
-
- (Bunny::Session) client
Connection this channel was opened on.
-
- (Integer) number
Channel id.
Higher-level API for exchange operations (collapse)
-
- (Object) default_exchange
Provides access to the default exchange.
-
- (Bunny::Exchange) direct(name, opts = {})
Declares a direct exchange or looks it up in the cache of previously declared exchanges.
-
- (Bunny::Exchange) exchange(name, opts = {})
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
-
- (Bunny::Exchange) fanout(name, opts = {})
Declares a fanout exchange or looks it up in the cache of previously declared exchanges.
-
- (Bunny::Exchange) headers(name, opts = {})
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
-
- (Bunny::Exchange) topic(name, opts = {})
Declares a topic exchange or looks it up in the cache of previously declared exchanges.
Higher-level API for queue operations (collapse)
-
- (Bunny::Queue) queue(name = AMQ::Protocol::EMPTY_STRING, opts = {})
Declares an exchange or looks it up in the per-channel cache.
QoS and Flow Control (collapse)
-
- (Object) flow(active)
Flow control.
-
- (Object) prefetch(prefetch_count)
Sets how many messages will be given to consumers on this channel before they have to acknowledge or reject one of the previously consumed messages.
-
- (Object) recover(ignored = true)
Tells RabbitMQ to redeliver unacknowledged messages.
Message acknowledgements (collapse)
-
- (Object) ack(delivery_tag, multiple = false)
(also: #acknowledge)
Acknowledges a message.
-
- (Object) nack(delivery_tag, multiple = false, requeue = false)
Rejects a message.
-
- (Object) reject(delivery_tag, requeue = false)
Rejects a message.
Consumer and Message operations (basic.*) (collapse)
-
- (Boolean) any_consumers?
True if there are consumers on this channel.
-
- (NilClass) basic_ack(delivery_tag, multiple)
Acknowledges a delivery (message).
-
- (AMQ::Protocol::Basic::CancelOk) basic_cancel(consumer_tag)
Removes a consumer.
-
- (AMQ::Protocol::Basic::ConsumeOk) basic_consume(queue, consumer_tag = generate_consumer_tag, no_ack = false, exclusive = false, arguments = nil, &block)
Registers a consumer for queue.
-
- (AMQ::Protocol::Basic::ConsumeOk) basic_consume_with(consumer)
Registers a consumer for queue as Consumer instance.
-
- (Array) basic_get(queue, opts = {:ack => true})
Synchronously fetches a message from the queue, if there are any.
-
- (NilClass) basic_nack(delivery_tag, multiple = false, requeue = false)
Rejects or requeues messages just like #basic_reject but can do so with multiple messages at once.
-
- (Bunny::Channel) basic_publish(payload, exchange, routing_key, opts = {})
Publishes a message using basic.publish AMQP 0.9.1 method.
-
- (AMQ::Protocol::Basic::QosOk) basic_qos(prefetch_count, global = false)
Controls message delivery rate using basic.qos AMQP 0.9.1 method.
-
- (AMQ::Protocol::Basic::RecoverOk) basic_recover(requeue)
Redeliver unacknowledged messages.
-
- (NilClass) basic_reject(delivery_tag, requeue)
Rejects or requeues a message.
Queue operations (queue.*) (collapse)
-
- (AMQ::Protocol::Queue::BindOk) queue_bind(name, exchange, opts = {})
Binds a queue to an exchange using queue.bind AMQP 0.9.1 method.
-
- (AMQ::Protocol::Queue::DeclareOk) queue_declare(name, opts = {})
Declares a queue using queue.declare AMQP 0.9.1 method.
-
- (AMQ::Protocol::Queue::DeleteOk) queue_delete(name, opts = {})
Deletes a queue using queue.delete AMQP 0.9.1 method.
-
- (AMQ::Protocol::Queue::PurgeOk) queue_purge(name, opts = {})
Purges a queue (removes all messages from it) using queue.purge AMQP 0.9.1 method.
-
- (AMQ::Protocol::Queue::UnbindOk) queue_unbind(name, exchange, opts = {})
Unbinds a queue from an exchange using queue.unbind AMQP 0.9.1 method.
Exchange operations (exchange.*) (collapse)
-
- (AMQ::Protocol::Exchange::BindOk) exchange_bind(source, destination, opts = {})
Binds an exchange to another exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
-
- (AMQ::Protocol::Exchange::DeclareOk) exchange_declare(name, type, opts = {})
Declares a echange using echange.declare AMQP 0.9.1 method.
-
- (AMQ::Protocol::Exchange::DeleteOk) exchange_delete(name, opts = {})
Deletes a exchange using exchange.delete AMQP 0.9.1 method.
-
- (AMQ::Protocol::Exchange::UnbindOk) exchange_unbind(source, destination, opts = {})
Unbinds an exchange from another exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
Flow control (channel.*) (collapse)
-
- (AMQ::Protocol::Channel::FlowOk) channel_flow(active)
Enables or disables message flow for the channel.
Transactions (tx.*) (collapse)
-
- (AMQ::Protocol::Tx::CommitOk) tx_commit
Commits current transaction.
-
- (AMQ::Protocol::Tx::RollbackOk) tx_rollback
Rolls back current transaction.
-
- (AMQ::Protocol::Tx::SelectOk) tx_select
Puts the channel into transaction mode (starts a transaction).
Publisher Confirms (confirm.*) (collapse)
-
- (AMQ::Protocol::Confirm::SelectOk) confirm_select(callback = nil)
Enables publisher confirms for the channel.
-
- (Boolean) using_publisher_confirmations?
True if this channel has Publisher Confirms enabled, false otherwise.
-
- (Boolean) wait_for_confirms
Blocks calling thread until confirms are received for all currently unacknowledged published messages.
Misc (collapse)
-
- (String) generate_consumer_tag(name = "bunny")
Unique string supposed to be used as a consumer tag.
-
- (Object) synchronize(&block)
Synchronizes given block using this channel's mutex.
Network Failure Recovery (collapse)
-
- (Object) recover_consumers
Recovers consumers.
-
- (Object) recover_exchanges
Recovers exchanges.
-
- (Object) recover_from_network_failure
Recovers basic.qos setting, exchanges, queues and consumers.
-
- (Object) recover_prefetch_setting
Recovers basic.qos setting.
-
- (Object) recover_queues
Recovers queues and bindings.
Instance Method Summary (collapse)
-
- (Object) close
Closes the channel.
-
- (Boolean) closed?
True if this channel is closed (manually or because of an exception), false otherwise.
-
- (Channel) initialize(connection = nil, id = nil, work_pool = ConsumerWorkPool.new(1))
constructor
A new instance of Channel.
-
- (Object) on_error(&block)
Defines a handler for errors that are not responses to a particular operations (e.g. basic.ack, basic.reject, basic.nack).
-
- (Bunny::Channel) open
Opens the channel and resets its internal state.
-
- (Boolean) open?
True if this channel is open, false otherwise.
-
- (String) to_s
Brief human-readable representation of the channel.
Constructor Details
- (Channel) initialize(connection = nil, id = nil, work_pool = ConsumerWorkPool.new(1))
A new instance of Channel
164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/bunny/channel.rb', line 164 def initialize(connection = nil, id = nil, work_pool = ConsumerWorkPool.new(1)) @connection = connection @logger = connection.logger @id = id || @connection.next_channel_id @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 = Mutex.new @consumer_mutex = Mutex.new @unconfirmed_set_mutex = Mutex.new 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 end |
Instance Attribute Details
- (Bunny::Session) connection (readonly)
AMQP connection this channel was opened on
142 143 144 |
# File 'lib/bunny/channel.rb', line 142 def connection @connection end |
- (Hash<String, Bunny::Consumer>) consumers (readonly)
Consumer instances declared on this channel
158 159 160 |
# File 'lib/bunny/channel.rb', line 158 def consumers @consumers end |
- (Hash<String, Bunny::Exchange>) exchanges (readonly)
Exchange instances declared on this channel
152 153 154 |
# File 'lib/bunny/channel.rb', line 152 def exchanges @exchanges end |
- (Integer) id
Channel id
140 141 142 |
# File 'lib/bunny/channel.rb', line 140 def id @id end |
- (Set<Integer>) nacked_set (readonly)
Set of nacked message indexes that have been nacked
156 157 158 |
# File 'lib/bunny/channel.rb', line 156 def nacked_set @nacked_set end |
- (Integer) next_publish_seq_no (readonly)
Next publisher confirmations sequence index
148 149 150 |
# File 'lib/bunny/channel.rb', line 148 def next_publish_seq_no @next_publish_seq_no end |
- (Hash<String, Bunny::Queue>) queues (readonly)
Queue instances declared on this channel
150 151 152 |
# File 'lib/bunny/channel.rb', line 150 def queues @queues end |
- (Symbol) status (readonly)
Channel status (:opening, :open, :closed)
144 145 146 |
# File 'lib/bunny/channel.rb', line 144 def status @status end |
- (Set<Integer>) unconfirmed_set (readonly)
Set of published message indexes that are currently unconfirmed
154 155 156 |
# File 'lib/bunny/channel.rb', line 154 def unconfirmed_set @unconfirmed_set end |
- (Bunny::ConsumerWorkPool) work_pool (readonly)
Thread pool delivered messages are dispatched to.
146 147 148 |
# File 'lib/bunny/channel.rb', line 146 def work_pool @work_pool end |
Instance Method Details
- (Object) ack(delivery_tag, multiple = false) Also known as: acknowledge
Acknowledges a message. Acknowledged messages are completely removed from the queue.
452 453 454 |
# File 'lib/bunny/channel.rb', line 452 def ack(delivery_tag, multiple = false) basic_ack(delivery_tag, multiple) end |
- (Boolean) active
True if this channel is open
249 250 251 |
# File 'lib/bunny/channel.rb', line 249 def active open? end |
- (Boolean) any_consumers?
True if there are consumers on this channel
901 902 903 |
# File 'lib/bunny/channel.rb', line 901 def any_consumers? @consumer_mutex.synchronize { @consumers.any? } end |
- (NilClass) basic_ack(delivery_tag, multiple)
Acknowledges a delivery (message).
706 707 708 709 710 711 |
# File 'lib/bunny/channel.rb', line 706 def basic_ack(delivery_tag, multiple) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Ack.encode(@id, delivery_tag, multiple)) nil end |
- (AMQ::Protocol::Basic::CancelOk) basic_cancel(consumer_tag)
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.
887 888 889 890 891 892 893 894 895 896 897 |
# File 'lib/bunny/channel.rb', line 887 def basic_cancel(consumer_tag) @connection.send_frame(AMQ::Protocol::Basic::Cancel.encode(@id, consumer_tag, false)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_basic_cancel_ok = wait_on_continuations end maybe_kill_consumer_work_pool! unless any_consumers? @last_basic_cancel_ok end |
- (AMQ::Protocol::Basic::ConsumeOk) basic_consume(queue, consumer_tag = generate_consumer_tag, no_ack = false, exclusive = false, arguments = nil, &block)
Registers a consumer for queue. Delivered messages will be handled with the block provided to this method.
789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 |
# File 'lib/bunny/channel.rb', line 789 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 Bunny::Timer.timeout(read_write_timeout, ClientTimeout) 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 # 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 |
- (AMQ::Protocol::Basic::ConsumeOk) basic_consume_with(consumer)
Registers a consumer for queue as Bunny::Consumer instance.
840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 |
# File 'lib/bunny/channel.rb', line 840 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 Bunny::Timer.timeout(read_write_timeout, ClientTimeout) 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 # covers server-generated consumer tags register_consumer(@last_basic_consume_ok.consumer_tag, consumer) raise_if_continuation_resulted_in_a_channel_error! @last_basic_consume_ok end |
- (Array) basic_get(queue, opts = {:ack => true})
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.
559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 |
# File 'lib/bunny/channel.rb', line 559 def basic_get(queue, opts = {:ack => true}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Get.encode(@id, queue, !(opts[: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? wait_on_basic_get_continuations else [nil, nil, nil] end raise_if_continuation_resulted_in_a_channel_error! @last_basic_get_response end |
- (NilClass) basic_nack(delivery_tag, multiple = false, requeue = false)
Rejects or requeues messages just like #basic_reject but can do so with multiple messages at once.
766 767 768 769 770 771 772 773 774 |
# File 'lib/bunny/channel.rb', line 766 def basic_nack(delivery_tag, multiple = false, requeue = false) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Nack.encode(@id, delivery_tag, multiple, requeue)) nil end |
- (Bunny::Channel) basic_publish(payload, exchange, routing_key, opts = {})
Publishes a message using basic.publish AMQP 0.9.1 method.
503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 |
# File 'lib/bunny/channel.rb', line 503 def basic_publish(payload, exchange, routing_key, opts = {}) raise_if_no_longer_open! exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end mode = if opts.fetch(:persistent, true) 2 else 1 end = { :priority => 0, :delivery_mode => mode, :content_type => "application/octet-stream" }. merge(opts) if @next_publish_seq_no > 0 @unconfirmed_set.add(@next_publish_seq_no) @next_publish_seq_no += 1 end m = AMQ::Protocol::Basic::Publish.encode(@id, payload, , exchange_name, routing_key, [:mandatory], false, @connection.frame_max) @connection.send_frameset_without_timeout(m, self) self end |
- (AMQ::Protocol::Basic::QosOk) basic_qos(prefetch_count, global = false)
Controls message delivery rate using basic.qos AMQP 0.9.1 method.
589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
# File 'lib/bunny/channel.rb', line 589 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_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Qos.encode(@id, 0, prefetch_count, global)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_basic_qos_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @prefetch_count = prefetch_count @last_basic_qos_ok end |
- (AMQ::Protocol::Basic::RecoverOk) basic_recover(requeue)
Redeliver unacknowledged messages
610 611 612 613 614 615 616 617 618 619 620 |
# File 'lib/bunny/channel.rb', line 610 def basic_recover(requeue) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Recover.encode(@id, requeue)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_basic_recover_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_basic_recover_ok end |
- (NilClass) basic_reject(delivery_tag, requeue)
Rejects or requeues a message.
660 661 662 663 664 665 |
# File 'lib/bunny/channel.rb', line 660 def basic_reject(delivery_tag, requeue) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Basic::Reject.encode(@id, delivery_tag, requeue)) nil end |
- (AMQ::Protocol::Channel::FlowOk) channel_flow(active)
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.
1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 |
# File 'lib/bunny/channel.rb', line 1231 def channel_flow(active) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Channel::Flow.encode(@id, active)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_channel_flow_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_channel_flow_ok end |
- (Bunny::Session) client
Connection this channel was opened on
254 255 256 |
# File 'lib/bunny/channel.rb', line 254 def client @connection end |
- (Object) close
Closes the channel. Closed channels can no longer be used (this includes associated Queue, Exchange and Bunny::Consumer instances.
220 221 222 223 224 |
# File 'lib/bunny/channel.rb', line 220 def close @connection.close_channel(self) closed! maybe_kill_consumer_work_pool! end |
- (Boolean) closed?
True if this channel is closed (manually or because of an exception), false otherwise
234 235 236 |
# File 'lib/bunny/channel.rb', line 234 def closed? @status == :closed end |
- (AMQ::Protocol::Confirm::SelectOk) confirm_select(callback = nil)
Enables publisher confirms for the channel.
1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 |
# File 'lib/bunny/channel.rb', line 1313 def confirm_select(callback=nil) raise_if_no_longer_open! if @next_publish_seq_no == 0 @confirms_continuations = new_continuation @unconfirmed_set = Set.new @nacked_set = Set.new @next_publish_seq_no = 1 end @confirms_callback = callback @connection.send_frame(AMQ::Protocol::Confirm::Select.encode(@id, false)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_confirm_select_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_confirm_select_ok end |
- (Object) default_exchange
Provides access to the default exchange
347 348 349 |
# File 'lib/bunny/channel.rb', line 347 def default_exchange self.direct(AMQ::Protocol::EMPTY_STRING, :no_declare => true) end |
- (Bunny::Exchange) direct(name, opts = {})
Declares a direct exchange or looks it up in the cache of previously declared exchanges.
304 305 306 |
# File 'lib/bunny/channel.rb', line 304 def direct(name, opts = {}) Exchange.new(self, :direct, name, opts) end |
- (Bunny::Exchange) exchange(name, opts = {})
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
365 366 367 |
# File 'lib/bunny/channel.rb', line 365 def exchange(name, opts = {}) Exchange.new(self, opts.fetch(:type, :direct), name, opts) end |
- (AMQ::Protocol::Exchange::BindOk) exchange_bind(source, destination, opts = {})
Binds an exchange to another exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
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 |
# File 'lib/bunny/channel.rb', line 1142 def exchange_bind(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 @connection.send_frame(AMQ::Protocol::Exchange::Bind.encode(@id, destination_name, source_name, opts[:routing_key], false, opts[:arguments])) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_exchange_bind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_exchange_bind_ok end |
- (AMQ::Protocol::Exchange::DeclareOk) exchange_declare(name, type, opts = {})
Declares a echange using echange.declare AMQP 0.9.1 method.
1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 |
# File 'lib/bunny/channel.rb', line 1082 def exchange_declare(name, type, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Exchange::Declare.encode(@id, name, type.to_s, opts.fetch(:passive, false), opts.fetch(:durable, false), opts.fetch(:auto_delete, false), false, false, opts[:arguments])) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_exchange_declare_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_exchange_declare_ok end |
- (AMQ::Protocol::Exchange::DeleteOk) exchange_delete(name, opts = {})
Deletes a exchange using exchange.delete AMQP 0.9.1 method
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 |
# File 'lib/bunny/channel.rb', line 1112 def exchange_delete(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Exchange::Delete.encode(@id, name, opts[:if_unused], false)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_exchange_delete_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_exchange_delete_ok end |
- (AMQ::Protocol::Exchange::UnbindOk) exchange_unbind(source, destination, opts = {})
Unbinds an exchange from another exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 |
# File 'lib/bunny/channel.rb', line 1186 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 @connection.send_frame(AMQ::Protocol::Exchange::Unbind.encode(@id, destination_name, source_name, opts[:routing_key], false, opts[:arguments])) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_exchange_unbind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_exchange_unbind_ok end |
- (Bunny::Exchange) fanout(name, opts = {})
Declares a fanout exchange or looks it up in the cache of previously declared exchanges.
286 287 288 |
# File 'lib/bunny/channel.rb', line 286 def fanout(name, opts = {}) Exchange.new(self, :fanout, name, opts) end |
- (Object) flow(active)
Flow control. When set to false, RabbitMQ will stop delivering messages on this channel.
415 416 417 |
# File 'lib/bunny/channel.rb', line 415 def flow(active) channel_flow(active) end |
- (String) generate_consumer_tag(name = "bunny")
Unique string supposed to be used as a consumer tag.
1364 1365 1366 |
# File 'lib/bunny/channel.rb', line 1364 def generate_consumer_tag(name = "bunny") "#{name}-#{Time.now.to_i * 1000}-#{Kernel.rand(999_999_999_999)}" end |
- (Bunny::Exchange) headers(name, opts = {})
Declares a headers exchange or looks it up in the cache of previously declared exchanges.
340 341 342 |
# File 'lib/bunny/channel.rb', line 340 def headers(name, opts = {}) Exchange.new(self, :headers, name, opts) end |
- (Object) nack(delivery_tag, multiple = false, requeue = false)
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.
467 468 469 |
# File 'lib/bunny/channel.rb', line 467 def nack(delivery_tag, multiple = false, requeue = false) basic_nack(delivery_tag, multiple, requeue) end |
- (Integer) number
Channel id
244 245 246 |
# File 'lib/bunny/channel.rb', line 244 def number self.id end |
- (Object) on_error(&block)
Defines a handler for errors that are not responses to a particular operations (e.g. basic.ack, basic.reject, basic.nack).
1379 1380 1381 |
# File 'lib/bunny/channel.rb', line 1379 def on_error(&block) @on_error = block end |
- (Bunny::Channel) open
Opens the channel and resets its internal state
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/bunny/channel.rb', line 203 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 |
- (Boolean) open?
True if this channel is open, false otherwise
228 229 230 |
# File 'lib/bunny/channel.rb', line 228 def open? @status == :open end |
- (Object) prefetch(prefetch_count)
Sets how many messages will be given to consumers on this channel before they have to acknowledge or reject one of the previously consumed messages
406 407 408 |
# File 'lib/bunny/channel.rb', line 406 def prefetch(prefetch_count) self.basic_qos(prefetch_count, false) end |
- (Bunny::Queue) queue(name = AMQ::Protocol::EMPTY_STRING, opts = {})
Declares an exchange or looks it up in the per-channel cache.
388 389 390 391 392 |
# File 'lib/bunny/channel.rb', line 388 def queue(name = AMQ::Protocol::EMPTY_STRING, opts = {}) q = find_queue(name) || Bunny::Queue.new(self, name, opts) register_queue(q) end |
- (AMQ::Protocol::Queue::BindOk) queue_bind(name, exchange, opts = {})
Binds a queue to an exchange using queue.bind AMQP 0.9.1 method
1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 |
# File 'lib/bunny/channel.rb', line 1005 def queue_bind(name, exchange, opts = {}) raise_if_no_longer_open! exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end @connection.send_frame(AMQ::Protocol::Queue::Bind.encode(@id, name, exchange_name, opts[:routing_key], false, opts[:arguments])) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_queue_bind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_queue_bind_ok end |
- (AMQ::Protocol::Queue::DeclareOk) queue_declare(name, opts = {})
Declares a queue using queue.declare AMQP 0.9.1 method.
927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 |
# File 'lib/bunny/channel.rb', line 927 def queue_declare(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Queue::Declare.encode(@id, name, opts.fetch(:passive, false), opts.fetch(:durable, false), opts.fetch(:exclusive, false), opts.fetch(:auto_delete, false), false, opts[:arguments])) @last_queue_declare_ok = wait_on_continuations raise_if_continuation_resulted_in_a_channel_error! @last_queue_declare_ok end |
- (AMQ::Protocol::Queue::DeleteOk) queue_delete(name, opts = {})
Deletes a queue using queue.delete AMQP 0.9.1 method
956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 |
# File 'lib/bunny/channel.rb', line 956 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)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_queue_delete_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_queue_delete_ok end |
- (AMQ::Protocol::Queue::PurgeOk) queue_purge(name, opts = {})
Purges a queue (removes all messages from it) using queue.purge AMQP 0.9.1 method.
979 980 981 982 983 984 985 986 987 988 989 990 |
# File 'lib/bunny/channel.rb', line 979 def queue_purge(name, opts = {}) raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Queue::Purge.encode(@id, name, false)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_queue_purge_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_queue_purge_ok end |
- (AMQ::Protocol::Queue::UnbindOk) queue_unbind(name, exchange, opts = {})
Unbinds a queue from an exchange using queue.unbind AMQP 0.9.1 method
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 |
# File 'lib/bunny/channel.rb', line 1041 def queue_unbind(name, exchange, opts = {}) raise_if_no_longer_open! exchange_name = if exchange.respond_to?(:name) exchange.name else exchange end @connection.send_frame(AMQ::Protocol::Queue::Unbind.encode(@id, name, exchange_name, opts[:routing_key], opts[:arguments])) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_queue_unbind_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_queue_unbind_ok end |
- (Object) recover(ignored = true)
Tells RabbitMQ to redeliver unacknowledged messages
421 422 423 424 |
# File 'lib/bunny/channel.rb', line 421 def recover(ignored = true) # RabbitMQ only supports basic.recover with requeue = true basic_recover(true) end |
- (Object) recover_consumers
Recovers consumers. Used by the Automatic Network Failure Recovery feature.
1438 1439 1440 1441 1442 1443 1444 1445 1446 |
# File 'lib/bunny/channel.rb', line 1438 def recover_consumers unless @consumers.empty? @work_pool = ConsumerWorkPool.new(@work_pool.size) @work_pool.start end @consumers.values.dup.each do |c| c.recover_from_network_failure end end |
- (Object) recover_exchanges
Recovers exchanges. Used by the Automatic Network Failure Recovery feature.
1417 1418 1419 1420 1421 |
# File 'lib/bunny/channel.rb', line 1417 def recover_exchanges @exchanges.values.dup.each do |x| x.recover_from_network_failure end end |
- (Object) recover_from_network_failure
Recovers basic.qos setting, exchanges, queues and consumers. Used by the Automatic Network Failure Recovery feature.
1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 |
# File 'lib/bunny/channel.rb', line 1394 def recover_from_network_failure @logger.debug "Recovering channel #{@id} after network failure" release_all_continuations recover_prefetch_setting recover_exchanges # this includes recovering bindings recover_queues recover_consumers end |
- (Object) recover_prefetch_setting
Recovers basic.qos setting. Used by the Automatic Network Failure Recovery feature.
1409 1410 1411 |
# File 'lib/bunny/channel.rb', line 1409 def recover_prefetch_setting basic_qos(@prefetch_count) if @prefetch_count end |
- (Object) recover_queues
Recovers queues and bindings. Used by the Automatic Network Failure Recovery feature.
1427 1428 1429 1430 1431 1432 |
# File 'lib/bunny/channel.rb', line 1427 def recover_queues @queues.values.dup.each do |q| @logger.debug "Recovering queue #{q.name}" q.recover_from_network_failure end end |
- (Object) reject(delivery_tag, requeue = false)
Rejects a message. A rejected message can be requeued or dropped by RabbitMQ.
441 442 443 |
# File 'lib/bunny/channel.rb', line 441 def reject(delivery_tag, requeue = false) basic_reject(delivery_tag, requeue) end |
- (Object) synchronize(&block)
Synchronizes given block using this channel's mutex.
1356 1357 1358 |
# File 'lib/bunny/channel.rb', line 1356 def synchronize(&block) @publishing_mutex.synchronize(&block) end |
- (String) to_s
Brief human-readable representation of the channel
1452 1453 1454 |
# File 'lib/bunny/channel.rb', line 1452 def to_s "#<#{self.class.name}:#{object_id} @id=#{self.number} @connection=#{@connection.to_s}>" end |
- (Bunny::Exchange) topic(name, opts = {})
Declares a topic exchange or looks it up in the cache of previously declared exchanges.
322 323 324 |
# File 'lib/bunny/channel.rb', line 322 def topic(name, opts = {}) Exchange.new(self, :topic, name, opts) end |
- (AMQ::Protocol::Tx::CommitOk) tx_commit
Commits current transaction
1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 |
# File 'lib/bunny/channel.rb', line 1267 def tx_commit raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Commit.encode(@id)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_tx_commit_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_tx_commit_ok end |
- (AMQ::Protocol::Tx::RollbackOk) tx_rollback
Rolls back current transaction
1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 |
# File 'lib/bunny/channel.rb', line 1282 def tx_rollback raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Rollback.encode(@id)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_tx_rollback_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_tx_rollback_ok end |
- (AMQ::Protocol::Tx::SelectOk) tx_select
Puts the channel into transaction mode (starts a transaction)
1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 |
# File 'lib/bunny/channel.rb', line 1252 def tx_select raise_if_no_longer_open! @connection.send_frame(AMQ::Protocol::Tx::Select.encode(@id)) Bunny::Timer.timeout(read_write_timeout, ClientTimeout) do @last_tx_select_ok = wait_on_continuations end raise_if_continuation_resulted_in_a_channel_error! @last_tx_select_ok end |
- (Boolean) using_publisher_confirmations?
True if this channel has Publisher Confirms enabled, false otherwise
1302 1303 1304 |
# File 'lib/bunny/channel.rb', line 1302 def using_publisher_confirmations? @next_publish_seq_no > 0 end |
- (Boolean) wait_for_confirms
Blocks calling thread until confirms are received for all currently unacknowledged published messages.
1342 1343 1344 1345 1346 1347 |
# File 'lib/bunny/channel.rb', line 1342 def wait_for_confirms @only_acks_received = true wait_on_confirms_continuations @only_acks_received end |