Class: AMQP::Session

Inherits:
AMQ::Client::EventMachineClient
  • Object
show all
Defined in:
lib/amqp/session.rb

Overview

AMQP session represents connection to the broker. Session objects let you define callbacks for various TCP connection lifecycle events, for instance:

  • Connection is established
  • Connection has failed
  • Authentication has failed
  • Connection is lost (there is a network failure)
  • AMQP connection is opened
  • AMQP connection parameters (tuning) are negotiated and accepted by the broker
  • AMQP connection is properly closed

Key methods

Broker information collapse

Connecting, reconnecting, disconnecting collapse

Broker information collapse

Error Handling and Recovery collapse

Instance Method Summary collapse

Constructor Details

#initialize(*args, &block) ⇒ Session

Returns a new instance of Session.



39
40
41
42
43
44
45
46
47
48
# File 'lib/amqp/session.rb', line 39

def initialize(*args, &block)
  super(*args, &block)

  @client_properties = {
    :platform    => ::RUBY_DESCRIPTION,
    :product     => "AMQP gem",
    :information => "http://github.com/ruby-amqp/amqp",
    :version     => AMQP::VERSION
  }
end

Instance Attribute Details

#server_capabilitiesHash (readonly)

Server capabilities (usually used to detect AMQP 0.9.1 extensions like basic.nack, publisher confirms and so on)



140
141
142
# File 'lib/amqp/session.rb', line 140

def server_capabilities
  @server_capabilities
end

#server_localesObject (readonly)

Locales server supports



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

def server_locales
  @server_locales
end

#server_propertiesHash (readonly)

Server properties (product information, platform, et cetera)



133
134
135
# File 'lib/amqp/session.rb', line 133

def server_properties
  @server_properties
end

Instance Method Details

#auto_recoverObject

Performs recovery of channels that are in the automatic recovery mode.

See Also:



268
269
270
# File 'lib/amqp/session.rb', line 268

def auto_recover
  super
end

#auto_recovering?Boolean Also known as: auto_recovery?

Returns whether connection is in the automatic recovery mode.

Returns:

  • (Boolean)

    whether connection is in the automatic recovery mode



256
257
258
# File 'lib/amqp/session.rb', line 256

def auto_recovering?
  super
end

#before_recovery(&block) ⇒ Object

Defines a callback that will be executed after TCP connection has recovered after a network failure but before AMQP connection is re-opened. Only one callback can be defined (the one defined last replaces previously added ones).



239
240
241
# File 'lib/amqp/session.rb', line 239

def before_recovery(&block)
  super(&block)
end

#brokerAMQP::Broker

Returns Broker this connection is established with.

Returns:

  • (AMQP::Broker)

    Broker this connection is established with



148
149
150
# File 'lib/amqp/session.rb', line 148

def broker
  @broker ||= AMQP::Broker.new(@server_properties)
end

#broker_endpointString

Returns Broker endpoint in the form of HOST:PORT/VHOST.

Returns:

  • (String)

    Broker endpoint in the form of HOST:PORT/VHOST



71
72
73
# File 'lib/amqp/session.rb', line 71

def broker_endpoint
  "#{self.hostname}:#{self.port}/#{self.vhost}"
end

#connected?Boolean

Returns true if this AMQP connection is currently open.

Returns:

  • (Boolean)

    true if this AMQP connection is currently open



52
53
54
# File 'lib/amqp/session.rb', line 52

def connected?
  self.opened?
end

#disconnect(reply_code = 200, reply_text = "Goodbye", &block) ⇒ Object Also known as: close

Properly close connection with AMQ broker, as described in section 2.2.4 of the AMQP 0.9.1 specification.

See Also:

  • #close_connection


117
118
119
120
# File 'lib/amqp/session.rb', line 117

def disconnect(reply_code = 200, reply_text = "Goodbye", &block)
  # defined here to make this method appear in YARD documentation. MK.
  super(reply_code, reply_text, &block)
end

#hostnameString Also known as: host

Returns Broker hostname this connection uses.

Returns:

  • (String)

    Broker hostname this connection uses



58
59
60
# File 'lib/amqp/session.rb', line 58

def hostname
  @settings[:host]
end

#on_closed(&block) ⇒ Object

Defines a callback that will be run when broker confirms connection termination (client receives connection.close-ok). You can define more than one callback.

See Also:



175
176
177
178
# File 'lib/amqp/session.rb', line 175

def on_closed(&block)
  # defined here to make this method appear in YARD documentation. MK.
  super(&block)
end

#on_connection_interruption(&block) ⇒ Object Also known as: after_connection_interruption

Defines a callback that will be executed after TCP connection is interrupted (typically because of a network failure). Only one callback can be defined (the one defined last replaces previously added ones).



211
212
213
# File 'lib/amqp/session.rb', line 211

def on_connection_interruption(&block)
  super(&block)
end

#on_error(&block) ⇒ Object

Defines a callback that will be executed when connection is closed after connection-level exception. Only one callback can be defined (the one defined last replaces previously added ones).



229
230
231
# File 'lib/amqp/session.rb', line 229

def on_error(&block)
  super(&block)
end

#on_open(&block) ⇒ Object

Defines a callback that will be executed when AMQP connection is considered open: after client and broker has agreed on max channel identifier and maximum allowed frame size and authentication succeeds. You can define more than one callback.

See Also:



162
163
164
165
# File 'lib/amqp/session.rb', line 162

def on_open(&block)
  # defined here to make this method appear in YARD documentation. MK.
  super(&block)
end

#on_possible_authentication_failure(&block) ⇒ Object

Defines a callback that will be run when TCP connection is closed before authentication finishes. Usually this means authentication failure. You can define only one callback.



202
203
204
205
# File 'lib/amqp/session.rb', line 202

def on_possible_authentication_failure(&block)
  # defined here to make this method appear in YARD documentation. MK.
  super(&block)
end

#on_recovery(&block) ⇒ Object Also known as: after_recovery

Defines a callback that will be executed after AMQP connection has recovered after a network failure.. Only one callback can be defined (the one defined last replaces previously added ones).



248
249
250
# File 'lib/amqp/session.rb', line 248

def on_recovery(&block)
  super(&block)
end

#on_tcp_connection_failure(&block) ⇒ Object

Defines a callback that will be run when initial TCP connection fails. You can define only one callback.



184
185
186
187
# File 'lib/amqp/session.rb', line 184

def on_tcp_connection_failure(&block)
  # defined here to make this method appear in YARD documentation. MK.
  super(&block)
end

#on_tcp_connection_loss(&block) ⇒ Object

Defines a callback that will be run when initial TCP connection fails. You can define only one callback.



193
194
195
196
# File 'lib/amqp/session.rb', line 193

def on_tcp_connection_loss(&block)
  # defined here to make this method appear in YARD documentation. MK.
  super(&block)
end

#portString

Returns Broker port this connection uses.

Returns:

  • (String)

    Broker port this connection uses



65
66
67
# File 'lib/amqp/session.rb', line 65

def port
  @settings[:port]
end

#reconnect(force = false, period = 2) ⇒ Object

Reconnect to the broker using current connection settings.

Parameters:

  • force (Boolean) (defaults to: false)

    Enforce immediate connection

  • period (Fixnum) (defaults to: 2)

    If given, reconnection will be delayed by this period, in seconds.



88
89
90
91
92
93
# File 'lib/amqp/session.rb', line 88

def reconnect(force = false, period = 2)
  # we do this to make sure this method shows up in our documentation
  # this method is too important to leave out and YARD currently does not
  # support cross-referencing to dependencies. MK.
  super(force, period)
end

#reconnect_to(connection_string_or_options = {}, period = 2) ⇒ Object

A version of #reconnect that allows connecting to different endpoints (hosts).

See Also:



98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/amqp/session.rb', line 98

def reconnect_to(connection_string_or_options = {}, period = 2)
  opts = case connection_string_or_options
         when String then
           AMQP::Client.parse_connection_uri(connection_string_or_options)
         when Hash then
           connection_string_or_options
         else
           Hash.new
         end

  super(opts, period)
end

#usernameString Also known as: user

Returns Username used by this connection.

Returns:

  • (String)

    Username used by this connection



77
78
79
# File 'lib/amqp/session.rb', line 77

def username
  @settings[:user]
end