HareMq.Connection (hare_mq v1.4.0)

Copy Markdown

GenServer that manages a single AMQP connection.

Lifecycle

On start it immediately sends itself a :connect message. If the broker is unavailable or misconfigured it logs the error and retries after reconnect_interval_ms (from config :hare_mq, :configuration; default 10 000 ms). All configuration is read at runtime so changes via Application.put_env take effect on the next reconnect.

The underlying AMQP.Connection is monitored. On a clean broker-initiated close (status 200) the GenServer goes idle. On any unexpected drop it logs and schedules a reconnect.

Named instances (multi-vhost)

By default the process registers as {:global, HareMq.Connection}. Pass name: to start_link/1 to run multiple independent connections:

{HareMq.Connection, name: {:global, :conn_vhost_b}}

Consumers and publishers accept a matching connection_name: option.

Summary

Functions

Returns a specification to start this module under a supervisor.

Closes the current AMQP connection.

Returns the current AMQP connection.

Callback implementation for GenServer.init/1.

Functions

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

close_connection(name \\ {:global, __MODULE__})

Closes the current AMQP connection.

Accepts an optional name argument (default {:global, HareMq.Connection}). Sets internal state to nil so subsequent get_connection/1 calls return {:error, :not_connected} immediately.

Returns:

  • {:ok, %AMQP.Connection{}} when the connection was open and is now closed.
  • {:error, :not_connected} when there was no active connection.

get_connection(name \\ {:global, __MODULE__})

Returns the current AMQP connection.

Accepts an optional name argument (default {:global, HareMq.Connection}) to query a specific named instance.

Returns:

  • {:ok, %AMQP.Connection{}} when connected.
  • {:error, :not_connected} when the GenServer is not running or not yet connected.

init(name)

Callback implementation for GenServer.init/1.

start_link(opts \\ [])