Amqpx.Connection (amqpx v6.0.2)
Functions to operate on Connections.
Link to this section Summary
Functions
Closes an open Connection.
Opens a new connection without a name.
Opens an new Connection to an Amqpx broker.
Opens a new connection with a name, merging the given Amqpx URI and options.
Link to this section Types
@type t() :: %Amqpx.Connection{pid: pid()}
Link to this section Functions
close(conn)
Closes an open Connection.
open(options_or_uri \\ [])
Opens a new connection without a name.
Behaves exactly like open(options_or_uri, :undefined)
. See open/2
.
open(options_or_uri, options_or_name)
@spec open(keyword() | String.t(), String.t() | :undefined | keyword()) :: {:ok, t()} | {:error, atom()} | {:error, any()}
Opens an new Connection to an Amqpx broker.
The connections created by this module are supervised under amqp_client's supervision tree. Please note that connections do not get restarted automatically by the supervision tree in case of a failure. If you need robust connections and channels, use monitors on the returned connection PID.
This function can be called in three ways:
- with a list of options and a name
- with an Amqpx URI and a name
- with an Amqpx URI and a list of options (in this case, the options are merged with the Amqpx URI, taking precedence on same keys)
options
Options
:username
- The name of a user registered with the broker (defaults to"guest"
);:password
- The password of user (defaults to"guest"
);:virtual_host
- The name of a virtual host in the broker (defaults to"/"
);:host
- The hostname of the broker (defaults to"localhost"
);:port
- The port the broker is listening on (defaults to5672
);:channel_max
- The channel_max handshake parameter (defaults to0
);:frame_max
- The frame_max handshake parameter (defaults to0
);:heartbeat
- The hearbeat interval in seconds (defaults to10
);:connection_timeout
- The connection timeout in milliseconds (defaults to50000
);:ssl_options
- Enable SSL by setting the location to cert files (defaults to:none
);:client_properties
- A list of extra client properties to be sent to the server, defaults to[]
;:socket_options
- Extra socket options. These are appended to the default options. See http://www.erlang.org/doc/man/inet.html#setopts-2 and http://www.erlang.org/doc/man/gen_tcp.html#connect-4 for descriptions of the available options.
enabling-ssl
Enabling SSL
To enable SSL, supply the following in the ssl_options
field:
cacertfile
- Specifies the certificates of the root Certificate Authorities that we wish to implicitly trust;certfile
- The client's own certificate in PEM format;keyfile
- The client's private key in PEM format;
example
Example
Amqpx.Connection.open port: 5671,
ssl_options: [cacertfile: '/path/to/testca/cacert.pem',
certfile: '/path/to/client/cert.pem',
keyfile: '/path/to/client/key.pem',
# only necessary with intermediate CAs
# depth: 2,
verify: :verify_peer,
fail_if_no_peer_cert: true]
connection-name
Connection name
RabbitMQ supports user-specified connection names since version 3.6.2.
Connection names are human-readable strings that will be displayed in the management UI. Connection names do not have to be unique and cannot be used as connection identifiers.
If the name is :undefined
, the connection is not registered with any name.
examples
Examples
iex> options = [host: "localhost", port: 5672, virtual_host: "/", username: "guest", password: "guest"]
iex> Amqpx.Connection.open(options, :undefined)
{:ok, %Amqpx.Connection{}}
iex> Amqpx.Connection.open("amqp://guest:guest@localhost", port: 5673)
{:ok, %Amqpx.Connection{}}
iex> Amqpx.Connection.open("amqp://guest:guest@localhost", "a-connection-with-a-name")
{:ok, %Amqpx.Connection{}}
open(uri, name, options)
@spec open(String.t(), String.t() | :undefined, keyword()) :: {:ok, t()} | {:error, atom()} | {:error, any()}
Opens a new connection with a name, merging the given Amqpx URI and options.
This function opens a new connection by merging the given Amqpx URI uri
and
the given options
and assigns it the name name
.
The options in options
take precedence over the values in uri
.
See open/2
for options.
examples
Examples
iex> Amqpx.Connection.open("amqp://guest:guest@localhost", "a-connection-name", port: 5673)
{:ok, %Amqpx.Connection{}}