ExRabbitMQ v2.10.0 ExRabbitMQ.Producer behaviour View Source
A behaviour module that abstracts away the handling of RabbitMQ connections and channels.
It also provides hooks to allow the programmer to publish a message without having to directly access the AMPQ interfaces.
For a connection configuration example see ExRabbitMQ.ConnectionConfig
.
Example usage for a producer implementing a GenServer
defmodule MyExRabbitMQProducer do
@module __MODULE__
use GenServer
use ExRabbitMQ.Producer
def start_link() do
GenServer.start_link(@module, :ok)
end
def init(state) do
new_state =
xrmq_init(:my_connection_config, state)
|> xrmq_extract_state()
{:ok, new_state}
end
def handle_cast({:publish, something}, state) do
xrmq_basic_publish(something, "", "my_queue")
{:noreply, state}
end
# optional override when there is a need to do setup the channel right after the connection has been established.
def xrmq_channel_setup(channel, state) do
# any other channel setup goes here...
{:ok, state}
end
end
Link to this section Summary
Callbacks
This overridable function publishes the payload
to the exchange
using the provided routing_key
This hook is called when a connection has been established and a new channel has been opened
Helper function that extracts the state
argument from the passed in tuple
Returns the connection configuration as it was passed to xrmq_init/2
Returns a part of the :exrabbitmq
configuration section, specified with the
key
argument
Initiates a connection or reuses an existing one
Link to this section Callbacks
This overridable function publishes the payload
to the exchange
using the provided routing_key
.
The wrapper process’s state is passed in to allow the callback to mutate it if overriden.
xrmq_channel_setup(channel :: term, state :: term) :: {:ok, new_state :: term} | {:error, reason :: term, new_state :: term}
This hook is called when a connection has been established and a new channel has been opened.
The wrapper process’s state is passed in to allow the callback to mutate it if overriden.
xrmq_extract_state({:ok, state :: term} | {:error, reason :: term, state :: term}) :: state :: term
Helper function that extracts the state
argument from the passed in tuple.
Returns the connection configuration as it was passed to xrmq_init/2
.
This configuration is set in the wrapper process’s dictionary.
For the configuration format see the top section of ExRabbitMQ.Producer
.
xrmq_get_env_config(key :: atom) :: keyword
Returns a part of the :exrabbitmq
configuration section, specified with the
key
argument.
For the configuration format see the top section of ExRabbitMQ.Producer
.
xrmq_init(connection_config :: struct, state :: term) :: {:ok, new_state :: term} | {:error, reason :: term, new_state :: term}
xrmq_init(connection_key :: atom, state :: term) :: {:ok, new_state :: term} | {:error, reason :: term, new_state :: term}
Initiates a connection or reuses an existing one.
When a connection is established then a new channel is opened.
Next, xrmq_channel_setup/2
is called to do any extra work on the opened channel.
This variant accepts a ExRabbitMQ.Connection
struct as the argument for the connection_config
parameter.
The wrapper process’s state is passed in to allow the callback to mutate it if overriden.
For the configuration format see the top section of ExRabbitMQ.Producer
.