Class: Bunny::Exchange
- Inherits:
-
Object
- Object
- Bunny::Exchange
- Defined in:
- lib/bunny/exchange.rb
Overview
Represents AMQP 0.9.1 exchanges.
Instance Attribute Summary (collapse)
- - (Bunny::Channel) channel readonly
- - (String) name readonly
-
- (Hash) opts
Options hash this exchange instance was instantiated with.
- - (Symbol) status readonly
-
- (Symbol) type
readonly
Type of this exchange (one of: :direct, :fanout, :topic, :headers).
Class Method Summary (collapse)
-
+ (Exchange) default(channel_or_connection)
The default exchange.
Instance Method Summary (collapse)
-
- (Hash) arguments
Additional optional arguments (typically used by RabbitMQ extensions and plugins).
-
- (Boolean) auto_delete?
True if this exchange was declared as automatically deleted (deleted as soon as last consumer unbinds).
-
- (Bunny::Exchange) bind(source, opts = {})
Binds an exchange to another (source) exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
-
- (Object) delete(opts = {})
Deletes the exchange unless it is a default exchange.
-
- (Boolean) durable?
True if this exchange was declared as durable (will survive broker restart).
-
- (Exchange) initialize(channel_or_connection, type, name, opts = {})
constructor
A new instance of Exchange.
-
- (Object) on_return(&block)
Defines a block that will handle returned messages.
-
- (Boolean) predefined?
(also: #predeclared?)
True if this exchange is a pre-defined one (amq.direct, amq.fanout, amq.match and so on).
-
- (Bunny::Exchange) publish(payload, opts = {})
Publishes a message.
-
- (Bunny::Exchange) unbind(source, opts = {})
Unbinds an exchange from another (source) exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
-
- (Object) wait_for_confirms
Waits until all outstanding publisher confirms on the channel arrive.
Constructor Details
- (Exchange) initialize(channel_or_connection, type, name, opts = {})
A new instance of Exchange
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 |
# File 'lib/bunny/exchange.rb', line 78 def initialize(channel_or_connection, type, name, opts = {}) # old Bunny versions pass a connection here. In that case, # we just use default channel from it. MK. @channel = channel_from(channel_or_connection) @name = name @type = type @options = self.class.(name, opts) @durable = @options[:durable] @auto_delete = @options[:auto_delete] @arguments = @options[:arguments] declare! unless opts[:no_declare] || predeclared? || (@name == AMQ::Protocol::EMPTY_STRING) @channel.register_exchange(self) end |
Instance Attribute Details
- (Bunny::Channel) channel (readonly)
18 19 20 |
# File 'lib/bunny/exchange.rb', line 18 def channel @channel end |
- (String) name (readonly)
21 22 23 |
# File 'lib/bunny/exchange.rb', line 21 def name @name end |
- (Hash) opts
Options hash this exchange instance was instantiated with
33 34 35 |
# File 'lib/bunny/exchange.rb', line 33 def opts @opts end |
- (Symbol) status (readonly)
29 30 31 |
# File 'lib/bunny/exchange.rb', line 29 def status @status end |
- (Symbol) type (readonly)
Type of this exchange (one of: :direct, :fanout, :topic, :headers).
25 26 27 |
# File 'lib/bunny/exchange.rb', line 25 def type @type end |
Class Method Details
+ (Exchange) default(channel_or_connection)
Do not confuse default exchange with amq.direct: amq.direct is a pre-defined direct exchange that doesn't have any special routing semantics.
The default exchange. Default exchange is a direct exchange that is predefined. It cannot be removed. Every queue is bind to this (direct) exchange by default with the following routing semantics: messages will be routed to the queue withe same same name as 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 Q with this name, that message will be routed to Q.
57 58 59 |
# File 'lib/bunny/exchange.rb', line 57 def self.default(channel_or_connection) self.new(channel_from(channel_or_connection), :direct, AMQ::Protocol::EMPTY_STRING, :no_declare => true) end |
Instance Method Details
- (Hash) arguments
Additional optional arguments (typically used by RabbitMQ extensions and plugins)
109 110 111 |
# File 'lib/bunny/exchange.rb', line 109 def arguments @arguments end |
- (Boolean) auto_delete?
True if this exchange was declared as automatically deleted (deleted as soon as last consumer unbinds).
103 104 105 |
# File 'lib/bunny/exchange.rb', line 103 def auto_delete? @auto_delete end |
- (Bunny::Exchange) bind(source, opts = {})
Binds an exchange to another (source) exchange using exchange.bind AMQP 0.9.1 extension that RabbitMQ provides.
171 172 173 174 175 |
# File 'lib/bunny/exchange.rb', line 171 def bind(source, opts = {}) @channel.exchange_bind(source, self, opts) self end |
- (Object) delete(opts = {})
Deletes the exchange unless it is a default exchange
152 153 154 155 |
# File 'lib/bunny/exchange.rb', line 152 def delete(opts = {}) @channel.deregister_exchange(self) @channel.exchange_delete(@name, opts) unless predeclared? end |
- (Boolean) durable?
True if this exchange was declared as durable (will survive broker restart).
97 98 99 |
# File 'lib/bunny/exchange.rb', line 97 def durable? @durable end |
- (Object) on_return(&block)
Defines a block that will handle returned messages
200 201 202 203 204 |
# File 'lib/bunny/exchange.rb', line 200 def on_return(&block) @on_return = block self end |
- (Boolean) predefined? Also known as: predeclared?
True if this exchange is a pre-defined one (amq.direct, amq.fanout, amq.match and so on)
237 238 239 |
# File 'lib/bunny/exchange.rb', line 237 def predefined? (@name == AMQ::Protocol::EMPTY_STRING) || !!(@name =~ /^amq\.(direct|fanout|topic|headers|match)/i) end |
- (Bunny::Exchange) publish(payload, opts = {})
Publishes a message
137 138 139 140 141 |
# File 'lib/bunny/exchange.rb', line 137 def publish(payload, opts = {}) @channel.basic_publish(payload, self.name, (opts.delete(:routing_key) || opts.delete(:key)), opts) self end |
- (Bunny::Exchange) unbind(source, opts = {})
Unbinds an exchange from another (source) exchange using exchange.unbind AMQP 0.9.1 extension that RabbitMQ provides.
191 192 193 194 195 |
# File 'lib/bunny/exchange.rb', line 191 def unbind(source, opts = {}) @channel.exchange_unbind(source, self, opts) self end |
- (Object) wait_for_confirms
Waits until all outstanding publisher confirms on the channel arrive.
This is a convenience method that delegates to Channel#wait_for_confirms
212 213 214 |
# File 'lib/bunny/exchange.rb', line 212 def wait_for_confirms @channel.wait_for_confirms end |