Rabbit v0.4.0 Rabbit.Producer behaviour View Source
A RabbitMQ producer process.
This wraps around the standard AMQP.Channel
. It provides the following benefits:
- Durability during connection and channel failures through use of expotential backoff.
- Channel pooling for increased publishing performance.
- Easy runtime setup through an
init/2
callback. - Simplification of standard publishing options.
- Automatic payload encoding based on available serializers and message content type.
Example
# This is a connection
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
{:ok, opts}
end
end
# This is a producer
defmodule MyProducer do
use Rabbit.Producer
def start_link(opts \\ []) do
Rabbit.Producer.start_link(__MODULE__, opts, name: __MODULE__)
end
# Callbacks
@impl Rabbit.Producer
def init(_type, opts) do
# Perform any runtime configuration...
{:ok, opts}
end
end
# Start the connection
MyConnection.start_link()
# Start the producer
MyProducer.start_link(connection: MyConnection, publish_opts: [content_type: "application/json"])
# Publish a message
Rabbit.Producer.publish(MyProducer, "", "my_queue", %{foo: "bar"})
Serializers
When a message is published, its content type is compared to the list of available serializers. If a serializer matches the content type, the message will be automatically encoded.
You can find out more about serializers at Rabbit.Serializer
.
Link to this section Summary
Functions
Publishes a message using the provided producer.
Starts a producer process.
Stops a producer process.
Callbacks
A callback executed when the producer is started.
Link to this section Types
exchange()
View Source
exchange() :: String.t()
exchange() :: String.t()
message()
View Source
message() :: term()
message() :: term()
option()
View Source
option() ::
{:connection, Rabbit.Connection.t()}
| {:pool_size, non_neg_integer()}
| {:max_overflow, non_neg_integer()}
| {:publish_opts, publish_options()}
option() :: {:connection, Rabbit.Connection.t()} | {:pool_size, non_neg_integer()} | {:max_overflow, non_neg_integer()} | {:publish_opts, publish_options()}
options()
View Source
options() :: [option()]
options() :: [option()]
publish_option()
View Source
publish_option() ::
{:mandatory, boolean()}
| {:immediate, boolean()}
| {:content_type, String.t()}
| {:content_encoding, String.t()}
| {:headers, [{String.t(), String.t()}]}
| {:persistent, boolean()}
| {:correlation_id, String.t()}
| {:priority, 1..9}
| {:reply_to, String.t()}
| {:expiration, non_neg_integer()}
| {:message_id, String.t()}
| {:timestamp, non_neg_integer()}
| {:type, String.t()}
| {:user_id, String.t()}
| {:app_id, String.t()}
publish_option() :: {:mandatory, boolean()} | {:immediate, boolean()} | {:content_type, String.t()} | {:content_encoding, String.t()} | {:headers, [{String.t(), String.t()}]} | {:persistent, boolean()} | {:correlation_id, String.t()} | {:priority, 1..9} | {:reply_to, String.t()} | {:expiration, non_neg_integer()} | {:message_id, String.t()} | {:timestamp, non_neg_integer()} | {:type, String.t()} | {:user_id, String.t()} | {:app_id, String.t()}
publish_options()
View Source
publish_options() :: [publish_option()]
publish_options() :: [publish_option()]
routing_key()
View Source
routing_key() :: String.t()
routing_key() :: String.t()
t()
View Source
t() :: GenServer.name()
t() :: GenServer.name()
Link to this section Functions
publish(producer, exchange, routing_key, payload, opts \\ [], timeout \\ 5000)
View Source
publish(
Rabbit.Producer.t(),
exchange(),
routing_key(),
message(),
publish_options(),
timeout()
) :: :ok | {:error, any()}
publish( Rabbit.Producer.t(), exchange(), routing_key(), message(), publish_options(), timeout() ) :: :ok | {:error, any()}
Publishes a message using the provided producer.
All publishing options can be provided to the producer during process start. They would then be used as defaults during publishing. Options provided to this function would overwrite any defaults the producer has.
Serializers
If a content_type
is provided as an option - and it matches one of the available
serializers, the payload will be automatically encoded using that serializer.
For example, we could automatically encode our payload to json if we do the following:
Rabbit.Producer.publish(MyProducer, "", "my_queue", %{foo: "bar"}, content_type: "application/json")
Please see the documention at Rabbit.Serializer
for more information.
Options
:mandatory
- If set, returns an error if the broker can't route the message to a queue - defaults tofalse
.:immediate
- If set, returns an error if the broker can't deliver te message to a consumer immediately - defaults tofalse
.:content_type
- MIME Content type.:content_encoding
- MIME Content encoding.:headers
- Message headers. Can be used with headers Exchanges.:persistent
- If set, uses persistent delivery mode. Messages marked as persistent that are delivered to durable queues will be logged to disk.:correlation_id
- Application correlation identifier:priority
- Message priority, ranging from 0 to 9.:reply_to
- Name of the reply queue.:expiration
- How long the message is valid (in milliseconds).:message_id
- Message identifier.:timestamp
- Timestamp associated with this message (epoch time).:type
- Message type as a string.:user_id
- Creating user ID. RabbitMQ will validate this against the active connection user.:app_id
- Publishing application ID.
start_link(module, opts \\ [], server_opts \\ [])
View Source
start_link(module(), options(), GenServer.options()) :: Supervisor.on_start()
start_link(module(), options(), GenServer.options()) :: Supervisor.on_start()
Starts a producer process.
Options
:connection
- ARabbit.Connection
process.:pool_size
- The number of processes to create for publishing - defaults to1
. Each process consumes a RabbitMQ channel.:max_overflow
- Maximum number of temporary workers created when the pool is empty - defaults to0
.:publish_opts
- Anypublish_option/0
that is automatically set as a default option value when callingpublish/6
.
Server Options
You can also provide server options - which are simply the same ones available
for GenServer.options/0
.
stop(producer)
View Source
stop(Rabbit.Producer.t()) :: :ok
stop(Rabbit.Producer.t()) :: :ok
Stops a producer process.
Link to this section Callbacks
init(atom, options) View Source
A callback executed when the producer is started.
Returning {:ok, opts}
- where opts
is a keyword list of t:option()
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