rabbit_mq v0.0.0-alpha-4 RabbitMQ.Producer

This module can be used to establish a pool of Producer workers.

Example usage

rabbit_mq allows you to design consistent, SDK-like Producers.

ℹ️ The following example assumes that the "customer" exchange already exists.

First, define your (ideally domain-specific) Producer:

defmodule CustomerProducer do
  use RabbitMQ.Producer, exchange: "customer", worker_count: 3

  def customer_updated(updated_customer) when is_map(updated_customer) do
    # See https://hexdocs.pm/amqp/AMQP.Basic.html#publish/5 for all available options.
    opts = [
      content_type: "application/json",
      correlation_id: UUID.uuid4(),
      mandatory: true
    ]

    payload = Jason.encode!(updated_customer)

    publish(payload, "customer.updated", opts)
  end
end

Then, start as normal under your existing supervision tree:

children = [
  Topology,
  CustomerConsumer,
  CustomerProducer,
  # ...and more
]

Supervisor.start_link(children, strategy: :one_for_one)

Finally, call the exposed methods from your application:

CustomerProducer.customer_updated(updated_customer)

Configuration

The following options can be used with RabbitMQ.Producer;

  • :confirm_type; publisher acknowledgement mode. Only :async is supported for now. Please consult the Publisher Confirms section for more details. Defaults to :async.
  • :exchange; the name of the exchange onto which the producer workers will publish. Defaults to "".
  • :worker_count; number of workers to be spawned. Cannot be greater than :max_channels_per_connection set in config. Defaults to 3.

When you use RabbitMQ.Consumer, a few things happen;

  1. The module turns into a GenServer.
  2. The server starts and supervises the desired number of workers.
  3. publish/3 becomes available in your module.

publish/3 is a private function with the following signature;

@type payload :: String.t()
@type routing_key :: String.t()
@type opts :: keyword()
@type result :: :ok | {:error, :correlation_id_missing}

publish(payload(), routing_key(), opts()) :: result()

⚠️ Please note that correlation_id is always required and failing to provide it will result in an exception.

ℹ️ To see which options can be passed as opts to publish/3, visit https://hexdocs.pm/amqp/AMQP.Basic.html#publish/5.

Link to this section Summary

Functions

Returns a specification to start this module under a supervisor.

Starts this module as a process via GenServer.start_link/3.

Invoked when the server is about to exit. It should do any cleanup required. See https://hexdocs.pm/elixir/GenServer.html#c:terminate/2 for more details.

Link to this section Functions

Link to this function

child_spec(init_arg)

Returns a specification to start this module under a supervisor.

See Supervisor.

Link to this function

start_link(config, opts)

start_link(map(), keyword()) :: GenServer.on_start()

Starts this module as a process via GenServer.start_link/3.

Only used by the module's child_spec.

Link to this function

terminate(reason, state)

Invoked when the server is about to exit. It should do any cleanup required. See https://hexdocs.pm/elixir/GenServer.html#c:terminate/2 for more details.