Flux AMQP v0.0.1 FluxAMQP View Source

Interface to connect to AMQP broker, sending and retrieving messages.

It uses AMQP, check their documentation to understand how this library perform the connection, configuration, consumption and delivery of AMQP messages.

Check FluxAMQP.Consumer to read about how to consume AMQP messages.

Check send/3 to read about how to send AMQP message.

Configuration

import Config

# Default values
config :flux_amqp,
  broker: [uri: "amqp://guest:guest@rabbitmq"],
  reconnection: [
    maximum_attempts: -1,
    waiting_ms: 10_000
  ],
  routing_keys: []

Configuration Options

  • broker - Set the broker which will be consumed and delivered messages. Options are:

    • uri - The AMQP Broker URI.
  • reconnection - A keyword list with reconnection options. Options are:

    • maximum_attempts - How many attempts should be performed in case of connection failure. If set as -1, it will try until connection succeed.

    • waiting_ms - How many milliseconds should wait before reattempting connection. If set as -1, it will not reattempt connection.

  • routing_keys - The AMQP routing keys which the messages will be consumed.

Link to this section Summary

Functions

Closes an open Connection.

Open a Connection and configure it.

Link to this section Functions

Link to this function

close_connection(connection)

View Source
close_connection(AMQP.Connection.t()) :: :ok | {:error, any()}

Closes an open Connection.

Parameters

Examples

iex> {:ok, connection} = FluxAMQP.connect(__MODULE__, only_connect?: true)
...> FluxAMQP.close_connection(connection)
:ok
Link to this function

connect(consumer, opts \\ [])

View Source
connect(atom(), keyword()) ::
  {:ok, AMQP.Channel.t()} | {:ok, AMQP.Connection.t()} | {:error, any()}

Open a Connection and configure it.

Parameters

  • consumer - The module which will consume AMQP messages.

  • opts - Keyword list with options (merged with application configuration).

Examples

iex> result = FluxAMQP.connect(__MODULE__, only_connect?: true)
...> with {:ok, %AMQP.Connection{}} <- result, do: :passed
:passed

iex> result = FluxAMQP.connect(__MODULE__)
...> with {:ok, %AMQP.Channel{}} <- result, do: :passed
:passed

Options

  • broker_uri - The AMQP Broker URI. Defaults to application configuration.

  • reconnection - A keyword list with reconnection options. Options are:

    • maximum_attempts - How many attempts should be performed in case of connection failure. Defaults to application configuration. If set as -1, it will try until connection succeed.

    • waiting_ms - How many milliseconds should wait before reattempting connection. Defaults to application configuration. If set as -1, it will not reattempt connection.

  • routing_keys - The AMQP routing keys which the messages will be consumed. Defaults to application configuration.

Link to this function

send(payload, routing_key, opts \\ [])

View Source
send(binary(), binary(), keyword()) :: :ok | {:error, any()}

Send AMQP message.

Parameters

  • payload: The message string.

  • routing_key: The AMQP routing key which the message will be sent.

  • opts: Keyword list with options (merged with application configuration).

Examples

iex> FluxAMQP.Sender.send(
...>  "Hello, World!",
...>  "some.amqp.route",
...>  broker_uri: "amqp://guest:guest@rabbitmq",
...>  close_connection?: true,
...>  declare_exchange?: true,
...>  reconnection: [maximum_attempts: 3, waiting_ms: 5_000]
...>)
:ok

Options

  • broker_uri: The AMQP Broker URI. Defaults to application configuration.

  • channel: A valid AMQP.Channel struct. If not set, a new channel will be created.

  • close_connection?: If true, it will close the connection. Defaults to true.

  • connection: A valid AMQP.Connection.

  • declare_exchange?: If true, it will declare the AMQP exchange into channel. Defaults to true.

  • exchange: A string with valid AMQP exchange definition. If not set, amq.topic will be used.

  • publish_options: A Keyword list with valid AMQP.Basic.publish/5 options. Defaults to empty Keyword list.

  • reconnection: A Keyword list with reconnection options. See connect/2 for more information.