Rabbit v0.3.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.
  • Easy runtime setup through an init/2 callback.

Example

# Connection module
defmodule MyConnection do
  use Rabbit.Connection

  def start_link(opts \\ []) do
    Rabbit.Connection.start_link(__MODULE__, opts, name: __MODULE__)
  end

  # Callbacks

  @impl Rabbit.Connection
  def init(_type, opts) do
    # Perform any runtime configuration
    {:ok, opts}
  end
end

# Start the connection
MyConnection.start_link()

# Subscribe to the connection
Rabbit.Connection.subscribe(MyConnection)

receive do
  {:connected, connection} -> "hello"
end

# Stop the connection
Rabbit.Connection.stop(MyConnection)

receive do
  {:disconnected, reason} -> "bye"
end

Link to this section Summary

Functions

Checks whether a connection is alive.

Fetches the raw AMQP.Connection struct from the process.

Stops a connection process.

Subscribes a process to the connection.

Callbacks

A callback executed when the connection is started.

Link to this section Types

Link to this type

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()}

Link to this type

options() View Source
options() :: uri() | [option()]

Link to this section Functions

Checks whether a connection is alive.

Link to this function

fetch(connection) View Source
fetch(Rabbit.Connection.t()) ::
  {:ok, AMQP.Connection.t()} | {:error, :not_connected}

Fetches the raw AMQP.Connection struct from the process.

Link to this function

start_link(module, opts \\ [], server_opts \\ []) View Source

Starts a 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 to 5672.
  • :channel_max - The channel_max handshake parameter - defaults to 0.
  • :frame_max - The frame_max handshake parameter - defaults to 0.
  • :heartbeat - The hearbeat interval in seconds - defaults to 10.
  • :connection_timeout - The connection timeout in milliseconds - defaults to 50000.
  • :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.

Server Options

You can also provide server options - which are simply the same ones available for GenServer.options/0.

Stops a connection process.

Link to this function

subscribe(connection, subscriber \\ nil) View Source
subscribe(Rabbit.Connection.t(), pid() | nil) :: :ok

Subscribes a process to the 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.

Link to this section Callbacks

Link to this callback

init(atom, options) View Source
init(:connection, options()) :: {:ok, options()} | :ignore

A callback executed when the connection is started.

Returning {:ok, opts} - where opts is a keyword list of option/0 will, cause start_link/3 to return {:ok, pid} and the process to enter its loop.

Returning :ignore will cause start_link/3 to return :ignore and the process will exit normally without entering the loop.