Module: AMQP
- Defined in:
- lib/amqp/connection.rb,
lib/amqp/queue.rb,
lib/amqp/broker.rb,
lib/amqp/client.rb,
lib/amqp/header.rb,
lib/amqp/bit_set.rb,
lib/amqp/channel.rb,
lib/amqp/session.rb,
lib/amqp/version.rb,
lib/amqp/consumer.rb,
lib/amqp/exchange.rb,
lib/amqp/exceptions.rb,
lib/amqp/int_allocator.rb,
lib/amqp/deprecated/rpc.rb,
lib/amqp/deprecated/fork.rb,
lib/amqp/deprecated/logger.rb,
lib/amqp/integration/rails.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: Integration, Utilities Classes: BitSet, Broker, Channel, ChannelClosedError, Consumer, Error, Exchange, Header, IncompatibleOptionsError, IntAllocator, PossibleAuthenticationFailureError, Queue, Session, TCPConnectionFailed
Constant Summary collapse
- VERSION =
amqp gem version. Not to be confused with the AMQP protocol version it implements. For that, see AMQ::Protocol::VERSION
'0.8.2'
Class Method Summary collapse
-
.channel ⇒ Object
“Default channel”.
-
.channel=(value) ⇒ Object
A placeholder for applications that only need one channel.
-
.closing? ⇒ Boolean
Indicates that default connection is closing.
- .conn ⇒ Object deprecated Deprecated.
- .conn=(value) ⇒ Object deprecated Deprecated.
-
.connect(connection_options_or_string = {}, other_options = {}, &block) ⇒ AMQP::Session
Connects to AMQP broker and yields connection object to the block as soon as connection is considered open.
-
.connection ⇒ Object
Default connection.
-
.connection=(value) ⇒ Object
Sets global connection object.
-
.logging ⇒ Boolean
Current global logging value.
-
.logging=(value) ⇒ Boolean
Sets current global logging value.
-
.run(*args, &block) ⇒ Object
Alias for AMQP.start.
-
.settings ⇒ Hash
Default AMQP connection settings.
-
.start(connection_options_or_string = {}, other_options = {}, &block) ⇒ Object
Starts EventMachine event loop unless it is already running and connects to AMQP broker using AMQP.connect.
-
.stop(reply_code = 200, reply_text = "Goodbye", &block) ⇒ Object
Properly closes default AMQP connection and then underlying TCP connection.
Class Method Details
.channel ⇒ Object
“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.
113 114 115 |
# File 'lib/amqp/connection.rb', line 113 def self.channel @channel end |
.channel=(value) ⇒ Object
123 124 125 |
# File 'lib/amqp/connection.rb', line 123 def self.channel=(value) @channel = value end |
.closing? ⇒ Boolean
Indicates that default connection is closing.
83 84 85 |
# File 'lib/amqp/connection.rb', line 83 def self.closing? @connection.closing? end |
.conn ⇒ Object
Alias for connection
136 137 138 139 |
# File 'lib/amqp/connection.rb', line 136 def self.conn warn "AMQP.conn will be removed in 1.0. Please use AMQP.connection." @connection end |
.conn=(value) ⇒ Object
Alias for connection=
144 145 146 147 |
# File 'lib/amqp/connection.rb', line 144 def self.conn=(value) warn "AMQP.conn= will be removed in 1.0. Please use AMQP.connection=(connection)." self.connection = value end |
.connect(connection_string, options = {}) ⇒ AMQP::Session .connect(connection_options) ⇒ AMQP::Session
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.
213 214 215 |
# File 'lib/amqp/connection.rb', line 213 def self.connect( = {}, = {}, &block) Client.connect(, , &block) end |
.connection ⇒ Object
Default connection. When you do not pass connection instance to methods like AMQP::Channel#initialize, AMQP gem will use this default connection.
103 104 105 |
# File 'lib/amqp/connection.rb', line 103 def self.connection @connection end |
.connection=(value) ⇒ Object
Sets global connection object.
129 130 131 |
# File 'lib/amqp/connection.rb', line 129 def self.connection=(value) @connection = value end |
.logging ⇒ Boolean
Returns Current global logging value.
89 90 91 |
# File 'lib/amqp/connection.rb', line 89 def self.logging self.settings[:logging] end |
.logging=(value) ⇒ Boolean
Returns Sets current global logging value.
95 96 97 |
# File 'lib/amqp/connection.rb', line 95 def self.logging=(value) self.settings[:logging] = !!value end |
.run(*args, &block) ⇒ Object
Alias for start
49 50 51 |
# File 'lib/amqp/connection.rb', line 49 def self.run(*args, &block) self.start(*args, &block) end |
.settings ⇒ Hash
Returns Default AMQP connection settings. This hash may be modified.
219 220 221 |
# File 'lib/amqp/connection.rb', line 219 def self.settings @settings ||= AMQ::Client::Settings.default end |
.start(connection_options_or_string = {}, other_options = {}, &block) ⇒ Object
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 (for Web applications that do not use Thin or Goliath, it is the only option).
See connect for information about arguments this method takes and information about relevant topics such as authentication failure handling.
37 38 39 40 41 42 43 44 45 |
# File 'lib/amqp/connection.rb', line 37 def self.start( = {}, = {}, &block) EM.run do if !@connection || @connection.closed? || @connection.closing? @connection = connect(, , &block) end @channel = Channel.new(@connection) @connection end end |
.stop(reply_code = 200, reply_text = "Goodbye", &block) ⇒ Object
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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/amqp/connection.rb', line 60 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 |