Class: Bunny::Exchange

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

Overview

Represents AMQP 0.9.1 exchanges.

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:



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

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)



13
14
15
# File 'lib/bunny/exchange.rb', line 13

def channel
  @channel
end

- (String) name (readonly)



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

def name
  @name
end

- (Hash) opts

Options hash this exchange instance was instantiated with



28
29
30
# File 'lib/bunny/exchange.rb', line 28

def opts
  @opts
end

- (Symbol) status (readonly)



24
25
26
# File 'lib/bunny/exchange.rb', line 24

def status
  @status
end

- (Symbol) type (readonly)

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



20
21
22
# File 'lib/bunny/exchange.rb', line 20

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:



52
53
54
# File 'lib/bunny/exchange.rb', line 52

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)



108
109
110
# File 'lib/bunny/exchange.rb', line 108

def arguments
  @arguments
end

- (Boolean) auto_delete?

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



96
97
98
# File 'lib/bunny/exchange.rb', line 96

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:



170
171
172
173
174
# File 'lib/bunny/exchange.rb', line 170

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:



151
152
153
154
# File 'lib/bunny/exchange.rb', line 151

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).



90
91
92
# File 'lib/bunny/exchange.rb', line 90

def durable?
  @durable
end

- (Boolean) internal?



102
103
104
# File 'lib/bunny/exchange.rb', line 102

def internal?
  @internal
end

- (Object) on_return(&block)

Defines a block that will handle returned messages



199
200
201
202
203
# File 'lib/bunny/exchange.rb', line 199

def on_return(&block)
  @on_return = block

  self
end

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



236
237
238
# File 'lib/bunny/exchange.rb', line 236

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:



136
137
138
139
140
# File 'lib/bunny/exchange.rb', line 136

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:



190
191
192
193
194
# File 'lib/bunny/exchange.rb', line 190

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



211
212
213
# File 'lib/bunny/exchange.rb', line 211

def wait_for_confirms
  @channel.wait_for_confirms
end