RabbitMQPoolEx.Ports.RabbitMQ behaviour (RabbitMQ Pool Ex v1.0.0)

View Source

A behavior module defining the contract for interacting with RabbitMQ connections and channels.

This module acts as a port, abstracting the interaction with the RabbitMQ server using the AMQP library. It provides callbacks that define the expected behavior for opening and closing both connections and channels.

Implementations of this module should adhere to these callbacks to ensure consistent communication with RabbitMQ.

Callbacks

  • open_connection/1: Opens a connection to the RabbitMQ server using either a keyword list or a connection string. Returns {:ok, Connection.t()} on success or {:error, reason} on failure.

  • close_connection/1: Closes an active RabbitMQ connection. Returns :ok on success or {:error, reason} on failure.

  • open_channel/1: Opens a new channel within an existing RabbitMQ connection. Returns {:ok, Channel.t()} on success or {:error, reason} on failure.

  • close_channel/1: Closes an active RabbitMQ channel. Returns :ok on success or {:error, AMQP.Basic.error()} on failure.

Example

To implement this behavior in a module:

  defmodule MyApp.RabbitMQ do
    @behaviour RabbitMQPoolEx.Ports.RabbitMQ

    def open_connection(config) do
      AMQP.Connection.open(config)
    end

    def close_connection(connection) do
      AMQP.Connection.close(connection)
    end

    def open_channel(connection) do
      AMQP.Channel.open(connection)
    end

    def close_channel(channel) do
      AMQP.Channel.close(channel)
    end
  end

This design allows for easy mocking and testing by providing alternative implementations as needed.

Summary

Callbacks

close_channel(t)

@callback close_channel(AMQP.Channel.t()) :: :ok | {:error, AMQP.Basic.error()}

close_connection(t)

@callback close_connection(AMQP.Connection.t()) :: :ok | {:error, any()}

open_channel(t)

@callback open_channel(AMQP.Connection.t()) :: {:ok, AMQP.Channel.t()} | {:error, any()}

open_connection(arg1)

@callback open_connection(keyword() | String.t()) ::
  {:ok, AMQP.Connection.t()} | {:error, atom()} | {:error, any()}