1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AMQP.connect

Posted at

Connects to AMQP broker and yields connection object to the block as
as connection is considered open.

Options

  • [String] :host ("localhost") Host to connect to.
  • [Integer] :port (5672) Port to connect to.
  • [String] :vhost ("/") Virtual host to connect to.
  • [String] :username ("guest") Username to use. Also can be specified as :user.
  • [String] :password ("guest") Password to use. Also can be specified as :pass.
  • [Hash] :ssl TLS (SSL) parameters to use.
  • [Fixnum] :heartbeat (0) Connection heartbeat, in seconds. 0 means no heartbeat. Can also be configured server-side starting with RabbitMQ 3.0.
  • [#call] :on_tcp_connection_failure A callable object that will be run if connection to server fails
  • [#call] :on_possible_authentication_failure A callable object that will be run if authentication fails (see Authentication failure section)

Handling authentication failures

  • 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.
  • 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.

- 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.`

### source code 

def self.connect(connection_options_or_string = {}, other_options = {}, &block)
opts = case connection_options_or_string
when String then
AMQP::Settings.parse_connection_uri(connection_options_or_string)
when Hash then
connection_options_or_string
else
Hash.new
end

AMQP::Session.connect(opts.merge(other_options), &block)

end



### examples
- 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

- 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

 
- 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

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?