Rabbit v0.1.0 Rabbit.Connection behaviour View Source
A RabbitMQ connection process.
This wraps around the standard AMQP.Connection
. It provides the following
benefits:
- Durability during connection failures through use of expotential backoff.
- Subscriptions that assist connection status monitoring.
- Ability to create module-based connections that permit easy runtime setup
through an
init/1
callback.
Example
# This is a connection
defmodule MyConnection do
use Rabbit.Connection
# Callbacks
# Perform any runtime configuration
def init(opts) do
opts = Keyword.put(opts, :uri, System.get_env("RABBIT_URI"))
{:ok, opts}
end
end
# Start the connection
MyConnection.start_link()
# Subscribe to the connection
MyConnection.subscribe()
receive do
{:connected, connection} -> # Do stuff with AMQP.Connection
end
# Stop the connection
MyConnection.stop()
receive do
{:disconnected, reason} -> # Do stuff with disconnect
end
Link to this section Summary
Callbacks
Checks whether a connection is alive.
Fetches the raw AMQP.Connection
struct from the process.
Starts a RabbitMQ connection process.
Stops a RabbitMQ connection process.
Subscribes a process to the RabbitMQ connection.
Link to this section Types
option()
View Source
option() ::
{:module, module()}
| {:uri, String.t()}
| {:username, String.t()}
| {:password, String.t()}
| {:virtual_host, String.t()}
| {:host, String.t()}
| {:port, integer()}
| {:channel_max, integer()}
| {:frame_max, integer()}
| {:heartbeat, integer()}
| {:connection_timeout, integer()}
| {:ssl_options, atom() | Keyword.t()}
| {:socket_options, Keyword.t()}
| {:retry_backoff, non_neg_integer()}
| {:retry_max_delay, non_neg_integer()}
option() :: {:module, module()} | {:uri, String.t()} | {:username, String.t()} | {:password, String.t()} | {:virtual_host, String.t()} | {:host, String.t()} | {:port, integer()} | {:channel_max, integer()} | {:frame_max, integer()} | {:heartbeat, integer()} | {:connection_timeout, integer()} | {:ssl_options, atom() | Keyword.t()} | {:socket_options, Keyword.t()} | {:retry_backoff, non_neg_integer()} | {:retry_max_delay, non_neg_integer()}
options() View Source
t()
View Source
t() :: GenServer.name()
t() :: GenServer.name()
uri()
View Source
uri() :: String.t()
uri() :: String.t()
Link to this section Callbacks
alive?()
View Source
alive?() :: boolean()
alive?() :: boolean()
Checks whether a connection is alive.
fetch()
View Source
fetch() :: {:ok, Rabbit.Connection.t()} | {:error, :not_connected}
fetch() :: {:ok, Rabbit.Connection.t()} | {:error, :not_connected}
Fetches the raw AMQP.Connection
struct from the process.
init(options) View Source (optional)
start_link(options)
View Source
start_link(options()) :: GenServer.on_start()
start_link(options()) :: GenServer.on_start()
Starts a RabbitMQ connection process.
Options
:uri
- The connection URI. This takes priority over other connection attributes.: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.
stop()
View Source
stop() :: :ok | {:error, any()}
stop() :: :ok | {:error, any()}
Stops a RabbitMQ connection process.
subscribe(subscriber)
View Source
subscribe(subscriber :: pid() | nil) :: :ok
subscribe(subscriber :: pid() | nil) :: :ok
Subscribes a process to the RabbitMQ connection.
A subscribed process can receive the following messages:
{:connected, connection}
- where connection is an AMQP.Connection
struct.
During the subscription process, if the connection is alive, this message will immediately be sent. If the connection goes down, and manages to reconnect, this message will be sent.
{:disconnected, reason}
- where reason can be any value.
If the connection goes down, all subscribing processes are sent this message. The connection process will then go through an exponential backoff period until connection is achieved again.