Class: Bunny::Exchange
- Inherits:
-
Object
- Object
- Bunny::Exchange
- Defined in:
- lib/bunny/exchange.rb
Overview
Represents AMQP 0.9.1 exchanges.
Constant Summary collapse
- TYPE_DIRECT =
Standard AMQP 0-9-1 exchange types
:direct- TYPE_FANOUT =
:fanout- TYPE_TOPIC =
:topic- TYPE_HEADERS =
:headers- TYPE_MODULUS_HASH =
In RabbitMQ core since 4.3.0
:"x-modulus-hash"- TYPE_LOCAL_RANDOM =
In RabbitMQ core since 4.2.0
:"x-local-random"- TYPE_CONSISTENT_HASH =
Provided by commonly used plugins
:"x-consistent-hash"- TYPE_RANDOM =
:"x-random"
Instance Attribute Summary collapse
- #channel ⇒ Bunny::Channel readonly
- #name ⇒ String readonly
-
#opts ⇒ Hash
Options hash this exchange instance was instantiated with.
- #status ⇒ Symbol readonly
-
#type ⇒ Symbol
readonly
Type of this exchange (e.g. :direct, :fanout, :topic, :headers, :"x-consistent-hash", :"x-modulus-hash").
Class Method Summary collapse
- .add_default_options(name, opts) ⇒ Object
-
.default(channel_or_connection) ⇒ Exchange
The default exchange.
Instance Method Summary collapse
-
#arguments ⇒ Hash
Additional optional arguments (typically used by RabbitMQ extensions and plugins).
-
#auto_delete? ⇒ Boolean
True if this exchange was declared as automatically deleted (deleted as soon as last consumer unbinds).
-
#bind(source, opts = {}) ⇒ Bunny::Exchange
Binds an exchange to another (source) exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
-
#delete(opts = {}) ⇒ Object
Deletes the exchange unless it is predeclared.
-
#durable? ⇒ Boolean
True if this exchange was declared as durable (will survive broker restart).
- #handle_return(basic_return, properties, content) ⇒ Object
-
#initialize(channel, type, name, opts = {}) ⇒ Exchange
constructor
A new instance of Exchange.
-
#internal? ⇒ Boolean
True if this exchange is internal (used solely for exchange-to-exchange bindings and cannot be published to by clients).
-
#on_return(&block) ⇒ Object
Defines a block that will handle returned messages.
-
#predefined? ⇒ Boolean
(also: #predeclared?)
True if this exchange is a pre-defined one (amq.direct, amq.fanout, amq.match and so on).
-
#publish(payload, opts = {}) ⇒ Bunny::Exchange
Publishes a message.
-
#unbind(source, opts = {}) ⇒ Bunny::Exchange
Unbinds an exchange from another (source) exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
-
#wait_for_confirms ⇒ Object
Waits until all outstanding publisher confirms on the channel arrive.
Constructor Details
#initialize(channel, type, name, opts = {}) ⇒ Exchange
Returns a new instance of Exchange.
94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/bunny/exchange.rb', line 94 def initialize(channel, type, name, opts = {}) @channel = channel @name = name @type = type @options = self.class.(name, opts) @durable = @options[:durable] @auto_delete = @options[:auto_delete] @internal = @options[:internal] @arguments = @options[:arguments] @bindings = Set.new declare! unless opts[:no_declare] || predeclared? || (@name == AMQ::Protocol::EMPTY_STRING) # for basic.return dispatch and such @channel.register_exchange(self) # for topology recovery @channel.record_exchange(self) end |
Instance Attribute Details
#channel ⇒ Bunny::Channel (readonly)
35 36 37 |
# File 'lib/bunny/exchange.rb', line 35 def channel @channel end |
#name ⇒ String (readonly)
38 39 40 |
# File 'lib/bunny/exchange.rb', line 38 def name @name end |
#opts ⇒ Hash
Options hash this exchange instance was instantiated with
50 51 52 |
# File 'lib/bunny/exchange.rb', line 50 def opts @opts end |
#status ⇒ Symbol (readonly)
46 47 48 |
# File 'lib/bunny/exchange.rb', line 46 def status @status end |
#type ⇒ Symbol (readonly)
Type of this exchange (e.g. :direct, :fanout, :topic, :headers, :"x-consistent-hash", :"x-modulus-hash").
42 43 44 |
# File 'lib/bunny/exchange.rb', line 42 def type @type end |
Class Method Details
.add_default_options(name, opts) ⇒ Object
272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 |
# File 'lib/bunny/exchange.rb', line 272 def self.(name, opts) # :nowait is always false for Bunny h = { queue: name, nowait: false }.merge(opts) if name.empty? { passive: false, durable: false, auto_delete: false, internal: false, arguments: nil }.merge(h) else h end end |
.default(channel_or_connection) ⇒ Exchange
Do not confuse the default exchange with amq.direct: amq.direct is a pre-defined direct exchange that doesn't have any special routing semantics.
The default exchange. This exchange is a direct exchange that is predefined by the broker and that cannot be removed. Every queue is bound to this exchange by default with the following routing semantics: messages will be routed to the queue with the same name as the message's routing key. In other words, if a message is published with a routing key of "weather.usa.ca.sandiego" and there is a queue with this name, the message will be routed to the queue.
74 75 76 |
# File 'lib/bunny/exchange.rb', line 74 def self.default(channel_or_connection) self.new(channel_or_connection, :direct, AMQ::Protocol::EMPTY_STRING, no_declare: true) end |
Instance Method Details
#arguments ⇒ Hash
Returns Additional optional arguments (typically used by RabbitMQ extensions and plugins).
135 136 137 |
# File 'lib/bunny/exchange.rb', line 135 def arguments @arguments end |
#auto_delete? ⇒ Boolean
Returns true if this exchange was declared as automatically deleted (deleted as soon as last consumer unbinds).
123 124 125 |
# File 'lib/bunny/exchange.rb', line 123 def auto_delete? @auto_delete end |
#bind(source, opts = {}) ⇒ Bunny::Exchange
Binds an exchange to another (source) exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
198 199 200 201 202 203 |
# File 'lib/bunny/exchange.rb', line 198 def bind(source, opts = {}) @channel.exchange_bind(source, self, opts) @bindings.add(source: source, opts: opts) self end |
#delete(opts = {}) ⇒ Object
Deletes the exchange unless it is predeclared
179 180 181 182 |
# File 'lib/bunny/exchange.rb', line 179 def delete(opts = {}) @channel.delete_recorded_exchange(self) @channel.exchange_delete(@name, opts) unless predeclared? end |
#durable? ⇒ Boolean
Returns true if this exchange was declared as durable (will survive broker restart).
117 118 119 |
# File 'lib/bunny/exchange.rb', line 117 def durable? @durable end |
#handle_return(basic_return, properties, content) ⇒ Object
250 251 252 253 254 255 256 |
# File 'lib/bunny/exchange.rb', line 250 def handle_return(basic_return, properties, content) if @on_return @on_return.call(basic_return, properties, content) else # TODO: log a warning end end |
#internal? ⇒ Boolean
Returns true if this exchange is internal (used solely for exchange-to-exchange bindings and cannot be published to by clients).
129 130 131 |
# File 'lib/bunny/exchange.rb', line 129 def internal? @internal end |
#on_return(&block) ⇒ Object
Defines a block that will handle returned messages
229 230 231 232 233 |
# File 'lib/bunny/exchange.rb', line 229 def on_return(&block) @on_return = block self end |
#predefined? ⇒ Boolean Also known as: predeclared?
Returns true if this exchange is a pre-defined one (amq.direct, amq.fanout, amq.match and so on).
259 260 261 |
# File 'lib/bunny/exchange.rb', line 259 def predefined? (@name == AMQ::Protocol::EMPTY_STRING) || !!(@name =~ /^amq\.(direct|fanout|topic|headers|match)/i) end |
#publish(payload, opts = {}) ⇒ Bunny::Exchange
Publishes a message
163 164 165 166 167 168 |
# File 'lib/bunny/exchange.rb', line 163 def publish(payload, opts = {}) rk = opts[:routing_key] || opts[:key] @channel.basic_publish(payload, self.name, rk, opts) self end |
#unbind(source, opts = {}) ⇒ Bunny::Exchange
Unbinds an exchange from another (source) exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
219 220 221 222 223 224 |
# File 'lib/bunny/exchange.rb', line 219 def unbind(source, opts = {}) @channel.exchange_unbind(source, self, opts) @bindings.delete(source: source, opts: opts) self end |
#wait_for_confirms ⇒ Object
Waits until all outstanding publisher confirms on the channel arrive.
This is a convenience method that delegates to Channel#wait_for_confirms
241 242 243 |
# File 'lib/bunny/exchange.rb', line 241 def wait_for_confirms @channel.wait_for_confirms end |