Module: AMQ::Settings

Defined in:
lib/amq/settings.rb

Constant Summary collapse

AMQP_PORTS =
{"amqp" => 5672, "amqps" => 5671}.freeze
AMQPS =
"amqps".freeze

Class Method Summary collapse

Class Method Details

.configure(settings = nil) ⇒ Hash

Merges given configuration parameters with defaults and returns the result.

Parameters:

  • Configuration (Hash)

    parameters to use.

  • settings (Hash) (defaults to: nil)

    a customizable set of options

Options Hash (settings):

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

    Hostname AMQ broker runs on.

  • :port (String) — default: 5672

    Port AMQ broker listens on.

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

    Virtual host to use.

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

    Username to use for authentication.

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

    Password to use for authentication.

  • :ssl (String) — default: false

    Should be use TLS (SSL) for connection?

  • :timeout (String) — default: nil

    Connection timeout.

  • :broker (String) — default: nil

    Broker name (use if you intend to use broker-specific features).

  • :frame_max (Fixnum) — default: 131072

    Maximum frame size to use. If broker cannot support frames this large, broker’s maximum value will be used instead.

Returns:

  • (Hash)

    Merged configuration parameters.



53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/amq/settings.rb', line 53

def self.configure(settings = nil)
  case settings
  when Hash then
    if username = (settings.delete(:username) || settings.delete(:user))
      settings[:user] ||= username
    end

    if password = (settings.delete(:password) || settings.delete(:pass))
      settings[:pass] ||= password
    end


    self.default.merge(settings)
  when String then
    settings = self.parse_amqp_url(settings)
    self.default.merge(settings)
  when NilClass then
    self.default
  end
end

.defaultObject

Default connection settings used by AMQ clients

See Also:

  • Client::Settings.configure


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/amq/settings.rb', line 17

def self.default
  @default ||= {
    # server
    :host  => "127.0.0.1",
    :port  => AMQ::Protocol::DEFAULT_PORT,

    # login
    :user  => "guest",
    :pass  => "guest",
    :vhost => "/",

    # ssl
    :ssl => false,

    :frame_max => (128 * 1024),
    :heartbeat => 0
  }
end

.parse_amqp_url(connection_string) ⇒ Hash

Parses AMQP connection URI and returns its components as a hash.

h2. vhost naming schemes

It is convenient to be able to specify the AMQP connection parameters as a URI string, and various “amqp” URI schemes exist. Unfortunately, there is no standard for these URIs, so while the schemes share the basic idea, they differ in some details. This implementation aims to encourage URIs that work as widely as possible.

The URI scheme should be “amqp”, or “amqps” if SSL is required.

The host, port, username and password are represented in the authority component of the URI in the same way as in http URIs.

The vhost is obtained from the first segment of the path, with the leading slash removed. The path should contain only a single segment (i.e, the only slash in it should be the leading one). If the vhost is to include slashes or other reserved URI characters, these should be percent-escaped.

Examples:

How vhost is parsed


AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com")            # => vhost is nil, so default (/) will be used
AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com/")           # => vhost is an empty string
AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com/%2Fvault")   # => vhost is /vault
AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com/production") # => vhost is production
AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com/a.b.c")      # => vhost is a.b.c
AMQ::Settings.parse_amqp_url("amqp://dev.rabbitmq.com/foo/bar")    # => ArgumentError

Parameters:

  • connection_string (String)

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

Returns:

  • (Hash)

    Connection parameters (:username, :password, :vhost, :host, :port, :ssl)

Raises:

  • (ArgumentError)

    When connection URI schema is not amqp or amqps, or the path contains multiple segments



112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/amq/settings.rb', line 112

def self.parse_amqp_url(connection_string)
  uri = URI.parse(connection_string)
  raise ArgumentError.new("Connection URI must use amqp or amqps schema (example: amqp://bus.megacorp.internal:5766), learn more at http://bit.ly/ks8MXK") unless %w{amqp amqps}.include?(uri.scheme)

  opts = {}

  opts[:scheme] = uri.scheme
  opts[:user]   = URI.unescape(uri.user) if uri.user
  opts[:pass]   = URI.unescape(uri.password) if uri.password
  opts[:host]   = uri.host if uri.host
  opts[:port]   = uri.port || AMQP_PORTS[uri.scheme]
  opts[:ssl]    = uri.scheme.to_s.downcase =~ /amqps/i
  if uri.path =~ %r{^/(.*)}
    raise ArgumentError.new("#{uri} has multiple-segment path; please percent-encode any slashes in the vhost name (e.g. /production => %2Fproduction). Learn more at http://bit.ly/amqp-gem-and-connection-uris") if $1.index('/')
    opts[:vhost] = URI.unescape($1)
  end

  opts
end