Module: AMQP

Defined in:
lib/amqp/connection.rb,
lib/amqp/queue.rb,
lib/amqp/client.rb,
lib/amqp/header.rb,
lib/amqp/version.rb,
lib/amqp/session.rb,
lib/amqp/channel.rb,
lib/amqp/exchange.rb,
lib/amqp/exceptions.rb,
lib/amqp/deprecated/rpc.rb,
lib/amqp/deprecated/fork.rb,
lib/amqp/deprecated/logger.rb,
lib/amqp/utilities/server_type.rb,
lib/amqp/utilities/event_loop_helper.rb

Overview

Original version is from Qusion project by Daniel DeLeo.

Copyright © 2009 Daniel DeLeo Copyright © 2011 Michael Klishin

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.

Defined Under Namespace

Modules: Utilities Classes: Channel, ChannelClosedError, Error, Exchange, Header, IncompatibleOptionsError, PossibleAuthenticationFailureError, Queue, Session, TCPConnectionFailed

Constant Summary

VERSION =

AMQP gem version. Not to be confused with AMQP protocol version it implements. For that, see AMQ::Protocol::VERSION

Returns:

  • (String)

    AMQP gem version

See Also:

  • AMQ::Protocol::VERSION
'0.8.0.rc14.pre'

Class Method Summary (collapse)

Class Method Details

+ (Object) channel

“Default channel”. A placeholder for apps that only want to use one channel. This channel is not global, not used under the hood by methods like AMQP::Exchange#initialize and only shared by exchanges/queues you decide on. To reiterate: this is only a conventience accessor, since many apps (especially Web apps) can get by with just one connection and one channel.



114
115
116
# File 'lib/amqp/connection.rb', line 114

def self.channel
  @channel
end

+ (Object) channel=(value)

A placeholder for applications that only need one channel. If you use start to set up default connection, channel is open on that connection, but can be replaced by your application.

See Also:



124
125
126
# File 'lib/amqp/connection.rb', line 124

def self.channel=(value)
  @channel = value
end

+ (Boolean) closing?

Indicates that default connection is closing.

Returns:

  • (Boolean)


84
85
86
# File 'lib/amqp/connection.rb', line 84

def self.closing?
  @connection.closing?
end

+ (Object) conn

Deprecated.

Alias for connection



137
138
139
140
# File 'lib/amqp/connection.rb', line 137

def self.conn
  warn "AMQP.conn will be removed in 1.0. Please use AMQP.connection."
  @connection
end

+ (Object) conn=(value)

Deprecated.

Alias for connection=



145
146
147
148
# File 'lib/amqp/connection.rb', line 145

def self.conn=(value)
  warn "AMQP.conn= will be removed in 1.0. Please use AMQP.connection=(connection)."
  self.connection = value
end

+ (Object) connect(connection_string, options = {}) + (Object) connect(connection_options)

Note:

This method assumes that EventMachine even loop is already running. If it is not the case or you are not sure, we recommend you use start instead. It takes exactly the same parameters.

Connects to AMQP broker and yields connection object to the block as soon as connection is considered open.

Handling authentication failures

AMQP 0.9.1 specification dictates that broker closes TCP connection when it detects that authentication has failed. However, broker does exactly the same thing when other connection-level exception occurs so there is no way to guarantee that connection was closed because of authentication failure.

Because of that, AMQP gem follows Java client example and hints at possibility of authentication failure. To handle it, pass a callable object (a proc, a lambda, an instance of a class that responds to #call) with :on_possible_authentication_failure option.

Examples:

Using AMQP.connect with default connection settings

AMQP.connect do |connection|
  AMQP::Channel.new(connection) do |channel|
    # channel is ready: set up your messaging flow by creating exchanges,
    # queues, binding them together and so on.
  end
end

Using AMQP.connect to connect to a public RabbitMQ instance with connection settings given as a hash

AMQP.connect(:host => "dev.rabbitmq.com", :username => "guest", :password => "guest") do |connection|
  AMQP::Channel.new(connection) do |channel|
    # ...
  end
end

Using AMQP.connect to connect to a public RabbitMQ instance with connection settings given as a URI

AMQP.connect "amqp://guest:guest@dev.rabbitmq.com:5672", :on_possible_authentication_failure => Proc.new { puts("Looks like authentication has failed") } do |connection|
  AMQP::Channel.new(connection) do |channel|
    # ...
  end
end

Overloads:

  • + (Object) connect(connection_string, options = {})

    Used to pass connection parameters as a connection string

    Parameters:

    • :connection_string (String)

      AMQP connection URI, à la JDBC connection string. For example: amqp://bus.megacorp.internal:5877/qa

  • + (Object) connect(connection_options)

    Used to pass connection options as a Hash.

    Parameters:

    • :connection_options (Hash)

      AMQP connection options (:host, :port, :username, :vhost, :password)

Parameters:

  • connection_options_or_string (Hash) (defaults to: {})

    a customizable set of options

Options Hash (connection_options_or_string):

  • :host (String) — default: "localhost"

    Host to connect to.

  • :port (Integer) — default: 5672

    Port to connect to.

  • :vhost (String) — default: "/"

    Virtual host to connect to.

  • :username (String) — default: "guest"

    Username to use. Also can be specified as :user.

  • :password (String) — default: "guest"

    Password to use. Also can be specified as :pass.

  • :ssl (Hash)

    TLS (SSL) parameters to use.

  • :on_tcp_connection_failure (#call)

    A callable object that will be run if connection to server fails

  • :on_possible_authentication_failure (#call)

    A callable object that will be run if authentication fails (see Authentication failure section)

Returns:



214
215
216
# File 'lib/amqp/connection.rb', line 214

def self.connect(connection_options_or_string = {}, other_options = {}, &block)
  Client.connect(connection_options_or_string, other_options, &block)
end

+ (Object) connection

Default connection. When you do not pass connection instance to methods like AMQP::Channel#initialize, AMQP gem will use this default connection.



104
105
106
# File 'lib/amqp/connection.rb', line 104

def self.connection
  @connection
end

+ (Object) connection=(value)

Sets global connection object.



130
131
132
# File 'lib/amqp/connection.rb', line 130

def self.connection=(value)
  @connection = value
end

+ (Boolean) logging

Current global logging value

Returns:

  • (Boolean)

    Current global logging value



90
91
92
# File 'lib/amqp/connection.rb', line 90

def self.logging
  self.settings[:logging]
end

+ (Boolean) logging=(value)

Sets current global logging value

Returns:

  • (Boolean)

    Sets current global logging value



96
97
98
# File 'lib/amqp/connection.rb', line 96

def self.logging=(value)
  self.settings[:logging] = !!value
end

+ (Object) run(*args, &block)

Alias for start



50
51
52
# File 'lib/amqp/connection.rb', line 50

def self.run(*args, &block)
  self.start(*args, &block)
end

+ (Hash) settings

Default AMQP connection settings. This hash may be modified.

Returns:

  • (Hash)

    Default AMQP connection settings. This hash may be modified.



220
221
222
# File 'lib/amqp/connection.rb', line 220

def self.settings
  @settings ||= AMQ::Client::Settings.default
end

+ (Object) start(connection_options_or_string = {}, other_options = {}, &block)

Starts EventMachine event loop unless it is already running and connects to AMQP broker using connect. It is generally a good idea to start EventMachine event loop in a separate thread and use connect. That said, for applications that do not publish or handle large volumes of data start works very well and its use is not discouraged.

See connect for information about arguments this method takes and information about relevant topics such as authentication failure handling.

Examples:

Using AMQP.start to connect to AMQP broker, EventMachine loop isn’t yet running

AMQP.start do |connection|
  # default is to connect to localhost:5672, to root ("/") vhost as guest/guest

  # this block never exits unless either AMQP.stop or EM.stop
  # is called.

  AMQP::Channel(connection) do |channel|
    channel.queue("", :auto_delete => true).bind(channel.fanout("amq.fanout")). do |headers, payload|
      # handle deliveries here
    end
  end
end


38
39
40
41
42
43
44
45
46
# File 'lib/amqp/connection.rb', line 38

def self.start(connection_options_or_string = {}, other_options = {}, &block)
  EM.run do
    if !@connection || @connection.closed? || @connection.closing?
      @connection   = connect(connection_options_or_string, other_options, &block)
    end
    @channel      = Channel.new(@connection)
    @connection
  end
end

+ (Object) stop(reply_code = 200, reply_text = "Goodbye", &block)

Note:

If default connection was never estabilished or is in the closing state already, this method has no effect.

Properly closes default AMQP connection and then underlying TCP connection. Pass it a block if you want a piece of code to be run once default connection is successfully closed.



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/amqp/connection.rb', line 61

def self.stop(reply_code = 200, reply_text = "Goodbye", &block)
  return if @connection.nil? || self.closing?

  EM.next_tick do
    if AMQP.channel and AMQP.channel.open? and AMQP.channel.connection.open?
      AMQP.channel.close
    end
    AMQP.channel = nil


    shim = Proc.new {
      block.call

      AMQP.connection = nil
    }
    @connection.disconnect(reply_code, reply_text, &shim)
  end
end