RMQ v0.1.0-beta.1 RMQ.Consumer behaviour View Source

RabbitMQ Consumer.

Keep in mind that the consumed message needs to be explicitly acknowledged via AMQP.Basic.ack/3 or rejected via AMQP.Basic.reject/3. For convenience, these functions are imported and are available directly.

AMQP.Basic.publish/3 is imported as well which is convenient for the case when the consumer implements RPC.

Configuration

  • :connection - the connection module which implements RMQ.Connection behaviour;
  • :queue - the name of the queue to consume. Will be created if does not exist;
  • :exchange - the name of the exchange to which queue should be bound. Also accepts two-element tuple {type, name}. Defaults to "";
  • :routing_key - queue binding key. Defaults to queue; Will be created if does not exist. Defaults to "";
  • :dead_letter - defines if the consumer should setup deadletter exchange and queue. Defaults to true;
  • :dead_letter_queue - the name of dead letter queue. Defaults to "#{queue}_error";
  • :dead_letter_exchange - the name of the exchange to which dead_letter_queue should be bound. Also accepts two-element tuple {type, name}. Defaults to "#{exchange}.dead-letter";
  • :dead_letter_routing_key - routing key for dead letter messages. Defaults to queue;
  • :concurrency - defines if consume/3 callback will be called in a separate process. Defaults to true;
  • :prefetch_count - sets the message prefetch count. Defaults to 10;
  • :consumer_tag - consumer tag. Defaults to a current module name;
  • :restart_delay - restart delay. Defaults to 5000.

Example

defmodule MyApp.Consumer do
  use RMQ.Consumer,
    connection: MyApp.RabbitConnection,
    queue: "my-app-consumer-queue"

  @impl RMQ.Consumer
  def process(message, %{content_type: "application/json"}), do: Jason.decode!(message)
  def process(message, _meta), do: message

  @impl RMQ.Consumer
  def consume(chan, payload, meta) do
    # handle message here
    ack(chan, meta.delivery_tag)
  end
end

Link to this section Summary

Callbacks

Consumes the message.

Optional callback for processing the message before consuming it.

Starts a GenServer process linked to the current process.

Link to this section Callbacks

Link to this callback

consume(chan, payload, meta)

View Source
consume(chan :: AMQP.Channel.t(), payload :: any(), meta :: Map.t()) :: any()

Consumes the message.

Link to this callback

process(payload, meta)

View Source (optional)
process(payload :: any(), meta :: Map.t()) :: payload :: any()

Optional callback for processing the message before consuming it.

Accepts payload and metadata. Must return (modified) payload.

Link to this callback

start_link(options)

View Source
start_link(options :: [GenServer.option()]) :: GenServer.on_start()

Starts a GenServer process linked to the current process.