Class: AMQP::Session
- Inherits:
-
AMQ::Client::EventMachineClient
- Object
- AMQ::Client::EventMachineClient
- AMQP::Session
- 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
- Session#on_connection
- #on_open
- Session#on_disconnection
- #on_possible_authentication_failure
- #on_tcp_connection_failure
- #on_tcp_connection_loss
- #reconnect
- #connected?
Broker information collapse
-
#server_capabilities ⇒ Hash
readonly
Server capabilities (usually used to detect AMQP 0.9.1 extensions like basic.nack, publisher confirms and so on).
-
#server_locales ⇒ Object
readonly
Locales server supports.
-
#server_properties ⇒ Hash
readonly
Server properties (product information, platform, et cetera).
Connecting, reconnecting, disconnecting collapse
-
#broker_endpoint ⇒ String
Broker endpoint in the form of HOST:PORT/VHOST.
-
#connected? ⇒ Boolean
True if this AMQP connection is currently open.
-
#disconnect(reply_code = 200, reply_text = "Goodbye", &block) ⇒ Object
(also: #close)
Properly close connection with AMQ broker, as described in section 2.2.4 of the AMQP 0.9.1 specification.
-
#hostname ⇒ String
(also: #host)
Broker hostname this connection uses.
-
#initialize(*args, &block) ⇒ Session
constructor
A new instance of Session.
-
#port ⇒ String
Broker port this connection uses.
-
#reconnect(force = false, period = 2) ⇒ Object
Reconnect to the broker using current connection settings.
-
#reconnect_to(connection_string_or_options = {}, period = 2) ⇒ Object
A version of #reconnect that allows connecting to different endpoints (hosts).
-
#username ⇒ String
(also: #user)
Username used by this connection.
Broker information collapse
-
#broker ⇒ AMQP::Broker
Broker this connection is established with.
Error Handling and Recovery collapse
-
#auto_recover ⇒ Object
Performs recovery of channels that are in the automatic recovery mode.
-
#auto_recovering? ⇒ Boolean
(also: #auto_recovery?)
Whether connection is in the automatic recovery mode.
-
#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.
-
#on_closed(&block) ⇒ Object
Defines a callback that will be run when broker confirms connection termination (client receives connection.close-ok).
-
#on_connection_interruption(&block) ⇒ Object
(also: #after_connection_interruption)
Defines a callback that will be executed after TCP connection is interrupted (typically because of a network failure).
-
#on_error(&block) ⇒ Object
Defines a callback that will be executed when connection is closed after connection-level exception.
-
#on_possible_authentication_failure(&block) ⇒ Object
Defines a callback that will be run when TCP connection is closed before authentication finishes.
-
#on_recovery(&block) ⇒ Object
(also: #after_recovery)
Defines a callback that will be executed after AMQP connection has recovered after a network failure..
-
#on_tcp_connection_failure(&block) ⇒ Object
Defines a callback that will be run when initial TCP connection fails.
-
#on_tcp_connection_loss(&block) ⇒ Object
Defines a callback that will be run when initial TCP connection fails.
Instance Method Summary collapse
-
#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.
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_capabilities ⇒ Hash (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_locales ⇒ Object (readonly)
Locales server supports
145 146 147 |
# File 'lib/amqp/session.rb', line 145 def server_locales @server_locales end |
#server_properties ⇒ Hash (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_recover ⇒ Object
Performs recovery of channels that are in the automatic recovery mode.
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.
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 |
#broker ⇒ AMQP::Broker
Returns 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_endpoint ⇒ String
Returns 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.
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.
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 |
#hostname ⇒ String Also known as: host
Returns 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.
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.
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 |
#port ⇒ String
Returns 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.
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).
98 99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/amqp/session.rb', line 98 def reconnect_to( = {}, period = 2) opts = case when String then AMQP::Client.parse_connection_uri() when Hash then else Hash.new end super(opts, period) end |
#username ⇒ String Also known as: user
Returns Username used by this connection.
77 78 79 |
# File 'lib/amqp/session.rb', line 77 def username @settings[:user] end |