encoding: utf-8
encoding: utf-8
Top-level namespace of amqp gem. Please refer to “See also” section below.
@see AMQP.connect @see AMQP.start @see AMQP::Channel @see AMQP::Exchange @see AMQP::Queue
encoding: utf-8
encoding: utf-8
encoding: utf-8
encoding: utf-8
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.
encoding: utf-8
“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.
@api public
# File lib/amqp/connection.rb, line 113 113: def self.channel 114: @channel 115: end
A placeholder for applications that only need one channel. If you use {AMQP.start} to set up default connection, {AMQP.channel} is open on that connection, but can be replaced by your application.
@see AMQP.channel @api public
# File lib/amqp/connection.rb, line 123 123: def self.channel=(value) 124: @channel = value 125: end
@private
# File lib/amqp/client.rb, line 95 95: def self.client 96: @client_implementation ||= AMQP::Session 97: end
@private
# File lib/amqp/client.rb, line 100 100: def self.client=(value) 101: @client_implementation = value 102: end
Indicates that default connection is closing.
@return [Boolean] @api public
# File lib/amqp/connection.rb, line 83 83: def self.closing? 84: @connection.closing? 85: end
Alias for {AMQP.connection} @deprecated @api public
# File lib/amqp/connection.rb, line 136 136: def self.conn 137: warn "AMQP.conn will be removed in 1.0. Please use AMQP.connection." 138: @connection 139: end
Alias for {AMQP.connection=} @deprecated @api public
# File lib/amqp/connection.rb, line 144 144: def self.conn=(value) 145: warn "AMQP.conn= will be removed in 1.0. Please use AMQP.connection=(connection)." 146: self.connection = value 147: end
Connects to AMQP broker and yields connection object to the block as soon as connection is considered open.
@example 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
@example 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
@example 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
@overload connect(connection_string, options = {})
Used to pass connection parameters as a connection string @param [String] :connection_string AMQP connection URI, à la JDBC connection string. For example: amqp://bus.megacorp.internal:5877/qa
@overload connect(connection_options)
Used to pass connection options as a Hash. @param [Hash] :connection_options AMQP connection options (:host, :port, :username, :vhost, :password)
@option connection_options_or_string [String] :host (“localhost”) Host to connect to. @option connection_options_or_string [Integer] :port (5672) Port to connect to. @option connection_options_or_string [String] :vhost (“/”) Virtual host to connect to. @option connection_options_or_string [String] :username (“guest”) Username to use. Also can be specified as :user. @option connection_options_or_string [String] :password (“guest”) Password to use. Also can be specified as :pass. @option connection_options_or_string [Hash] :ssl TLS (SSL) parameters to use. @option connection_options_or_string [#] :on_tcp_connection_failure A callable object that will be run if connection to server fails @option connection_options_or_string [#] :on_possible_authentication_failure A callable object that will be run if authentication fails (see Authentication failure section)
h2. 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 #) with :on_possible_authentication_failure option.
@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 {AMQP.start} instead.
It takes exactly the same parameters.
@return [AMQP::Session] @api public
# File lib/amqp/connection.rb, line 213 213: def self.connect(connection_options_or_string = {}, other_options = {}, &block) 214: Client.connect(connection_options_or_string, other_options, &block) 215: end
Default connection. When you do not pass connection instance to methods like {Channel#initialize}, AMQP gem will use this default connection.
@api public
# File lib/amqp/connection.rb, line 103 103: def self.connection 104: @connection 105: end
Sets global connection object. @api public
# File lib/amqp/connection.rb, line 129 129: def self.connection=(value) 130: @connection = value 131: end
@deprecated @private
# File lib/amqp/deprecated/fork.rb, line 8 8: def self.fork(workers) 9: EM.fork(workers) do 10: # clean up globals in the fork 11: Thread.current[:mq] = nil 12: AMQP.instance_variable_set('@conn', nil) 13: 14: yield 15: end 16: end
@return [Boolean] Current global logging value @api public
# File lib/amqp/connection.rb, line 89 89: def self.logging 90: self.settings[:logging] 91: end
@return [Boolean] Sets current global logging value @api public
# File lib/amqp/connection.rb, line 95 95: def self.logging=(value) 96: self.settings[:logging] = !!value 97: end
Alias for {AMQP.start} @api public
# File lib/amqp/connection.rb, line 49 49: def self.run(*args, &block) 50: self.start(*args, &block) 51: end
@return [Hash] Default AMQP connection settings. This hash may be modified. @api public
# File lib/amqp/connection.rb, line 219 219: def self.settings 220: @settings ||= AMQ::Client::Settings.default 221: end
Starts EventMachine event loop unless it is already running and connects to AMQP broker using {AMQP.connect}. It is generally a good idea to start EventMachine event loop in a separate thread and use {AMQP.connect} (for Web applications that do not use Thin or Goliath, it is the only option).
See {AMQP.connect} for information about arguments this method takes and information about relevant topics such as authentication failure handling.
@example 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")).subscribe do |headers, payload| # handle deliveries here end end end
@api public
# File lib/amqp/connection.rb, line 37 37: def self.start(connection_options_or_string = {}, other_options = {}, &block) 38: EM.run do 39: if !@connection || @connection.closed? || @connection.closing? 40: @connection = connect(connection_options_or_string, other_options, &block) 41: end 42: @channel = Channel.new(@connection) 43: @connection 44: end 45: end
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.
@note If default connection was never estabilished or is in the closing state already,
this method has no effect.
@api public
# File lib/amqp/connection.rb, line 60 60: def self.stop(reply_code = 200, reply_text = "Goodbye", &block) 61: return if @connection.nil? || self.closing? 62: 63: EM.next_tick do 64: if AMQP.channel and AMQP.channel.open? and AMQP.channel.connection.open? 65: AMQP.channel.close 66: end 67: AMQP.channel = nil 68: 69: 70: shim = Proc.new { 71: block.call 72: 73: AMQP.connection = nil 74: } 75: @connection.disconnect(reply_code, reply_text, &shim) 76: end 77: end
Disabled; run with --debug to generate this.
Generated with the Darkfish Rdoc Generator 1.1.6.