Class: Bunny::Exchange

Inherits:
Object
  • Object
show all
Defined in:
lib/bunny/exchange.rb

Overview

Represents AMQP 0.9.1 exchanges.

See Also:

Instance Attribute Summary (collapse)

Class Method Summary (collapse)

Instance Method Summary (collapse)

Constructor Details

- (Exchange) initialize(channel, type, name, opts = {})

Returns a new instance of Exchange

Options Hash (opts):

  • :durable (Boolean) — default: false

    Should this exchange be durable?

  • :auto_delete (Boolean) — default: false

    Should this exchange be automatically deleted when it is no longer used?

  • :arguments (Boolean) — default: {}

    Additional optional arguments (typically used by RabbitMQ extensions and plugins)

See Also:



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/bunny/exchange.rb', line 74

def initialize(channel, type, name, opts = {})
  @channel          = channel
  @name             = name
  @type             = type
  @options          = self.class.add_default_options(name, opts)

  @durable          = @options[:durable]
  @auto_delete      = @options[:auto_delete]
  @internal         = @options[:internal]
  @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)



15
16
17
# File 'lib/bunny/exchange.rb', line 15

def channel
  @channel
end

- (String) name (readonly)



18
19
20
# File 'lib/bunny/exchange.rb', line 18

def name
  @name
end

- (Hash) opts

Options hash this exchange instance was instantiated with



30
31
32
# File 'lib/bunny/exchange.rb', line 30

def opts
  @opts
end

- (Symbol) status (readonly)



26
27
28
# File 'lib/bunny/exchange.rb', line 26

def status
  @status
end

- (Symbol) type (readonly)

Type of this exchange (one of: :direct, :fanout, :topic, :headers).



22
23
24
# File 'lib/bunny/exchange.rb', line 22

def type
  @type
end

Class Method Details

+ (Exchange) default(channel_or_connection)

Note:

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.

Examples:

Publishing a messages to the tasks queue

channel     = Bunny::Channel.new(connection)
tasks_queue = channel.queue("tasks")
Bunny::Exchange.default(channel).publish("make clean", :routing_key => "tasks")

See Also:



54
55
56
# File 'lib/bunny/exchange.rb', line 54

def self.default(channel_or_connection)
  self.new(channel_or_connection, :direct, AMQ::Protocol::EMPTY_STRING, :no_declare => true)
end

Instance Method Details

- (Hash) arguments

Returns Additional optional arguments (typically used by RabbitMQ extensions and plugins)



110
111
112
# File 'lib/bunny/exchange.rb', line 110

def arguments
  @arguments
end

- (Boolean) auto_delete?

Returns true if this exchange was declared as automatically deleted (deleted as soon as last consumer unbinds).



98
99
100
# File 'lib/bunny/exchange.rb', line 98

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.

Options Hash (opts):

  • routing_key (String) — default: nil

    Routing key used for binding

  • arguments (Hash) — default: {}

    Optional arguments

See Also:



172
173
174
175
176
# File 'lib/bunny/exchange.rb', line 172

def bind(source, opts = {})
  @channel.exchange_bind(source, self, opts)

  self
end

- (Object) delete(opts = {})

Deletes the exchange unless it is predeclared

Options Hash (opts):

  • if_unused (Boolean) — default: false

    Should this exchange be deleted only if it is no longer used

See Also:



153
154
155
156
# File 'lib/bunny/exchange.rb', line 153

def delete(opts = {})
  @channel.deregister_exchange(self)
  @channel.exchange_delete(@name, opts) unless predeclared?
end

- (Boolean) durable?

Returns true if this exchange was declared as durable (will survive broker restart).



92
93
94
# File 'lib/bunny/exchange.rb', line 92

def durable?
  @durable
end

- (Boolean) internal?



104
105
106
# File 'lib/bunny/exchange.rb', line 104

def internal?
  @internal
end

- (Object) on_return(&block)

Defines a block that will handle returned messages



201
202
203
204
205
# File 'lib/bunny/exchange.rb', line 201

def on_return(&block)
  @on_return = block

  self
end

- (Boolean) predefined? Also known as: predeclared?



238
239
240
# File 'lib/bunny/exchange.rb', line 238

def predefined?
  (@name == AMQ::Protocol::EMPTY_STRING) || !!(@name =~ /^amq\.(direct|fanout|topic|headers|match)/i)
end

- (Bunny::Exchange) publish(payload, opts = {})

Publishes a message

Options Hash (opts):

  • :routing_key (String)

    Routing key

  • :persistent (Boolean)

    Should the message be persisted to disk?

  • :mandatory (Boolean)

    Should the message be returned if it cannot be routed to any queue?

  • :timestamp (Integer)

    A timestamp associated with this message

  • :expiration (Integer)

    Expiration time after which the message will be deleted

  • :type (String)

    Message type, e.g. what type of event or command this message represents. Can be any string

  • :reply_to (String)

    Queue name other apps should send the response to

  • :content_type (String)

    Message content type (e.g. application/json)

  • :content_encoding (String)

    Message content encoding (e.g. gzip)

  • :correlation_id (String)

    Message correlated to this one, e.g. what request this message is a reply for

  • :priority (Integer)

    Message priority, 0 to 9. Not used by RabbitMQ, only applications

  • :message_id (String)

    Any message identifier

  • :user_id (String)

    Optional user ID. Verified by RabbitMQ against the actual connection username

  • :app_id (String)

    Optional application ID

See Also:



138
139
140
141
142
# File 'lib/bunny/exchange.rb', line 138

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.

Options Hash (opts):

  • routing_key (String) — default: nil

    Routing key used for binding

  • arguments (Hash) — default: {}

    Optional arguments

See Also:



192
193
194
195
196
# File 'lib/bunny/exchange.rb', line 192

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



213
214
215
# File 'lib/bunny/exchange.rb', line 213

def wait_for_confirms
  @channel.wait_for_confirms
end