RMQ v0.3.1 RMQ.Consumer behaviour View Source

RabbitMQ Consumer.

Configuration

  • :connection - the connection module which implements RMQ.Connection behaviour. Defaults to RMQ.Connection;
  • :queue - the name of the queue to consume. Will be created if does not exist. Also can be a tuple {queue, options}. See the options for AMQP.Queue.declare/3;
  • :exchange - the name of the exchange to which queue should be bound. Also can be a tuple {exchange, type, options}. See the options for AMQP.Exchange.declare/4. 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. Also can be a tuple {queue, options}. See the options for AMQP.Queue.declare/3. Defaults to "#{queue}_error".;
  • :dead_letter_exchange - the name of the exchange to which dead_letter_queue should be bound. Also can be a tuple {type, exchange} or {type, exchange, options}. See the options for AMQP.Exchange.declare/4. Defaults to "#{exchange}.dead-letter";
  • :dead_letter_routing_key - routing key for dead letter messages. Defaults to queue;
  • :concurrency - defines if consume/3 callback should 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;
  • :reconnect_interval - a reconnect interval in milliseconds. It can be also a function that accepts the current connection attempt as a number and returns a new interval. Defaults to 5000;

Examples

defmodule MyApp.Consumer do
  use RMQ.Consumer,
    queue: {"my-app-consumer-queue", durable: true},
    exchange: {"my-exchange", :direct, durable: true}

  @impl RMQ.Consumer
  def consume(chan, payload, meta) do
    # do something with the payload
    ack(chan, meta.delivery_tag)
  end
end

defmodule MyApp.Consumer2 do
  use RMQ.Consumer

  @impl RMQ.Consumer
  def config do
    [
      queue: System.fetch_env!("QUEUE_NAME"),
      reconnect_interval: fn attempt -> attempt * 1000 end,
    ]
  end

  @impl RMQ.Consumer
  def consume(chan, payload, meta) do
    # do something with the payload
    ack(chan, meta.delivery_tag)
  end
end

Link to this section Summary

Functions

The default implementation for setup_queue/2 callback.

Callbacks

A callback for dynamic configuration.

A callback for consuming a message.

Does all the job on preparing the queue.

Link to this section Functions

Link to this function

setup_queue(chan, config)

View Source

Specs

setup_queue(chan :: AMQP.Channel.t(), config :: keyword()) :: :ok

The default implementation for setup_queue/2 callback.

Link to this section Callbacks

Specs

config() :: keyword()

A callback for dynamic configuration.

Link to this callback

consume(chan, payload, meta)

View Source

Specs

consume(chan :: AMQP.Channel.t(), payload :: any(), meta :: map()) :: any()

A callback for consuming a message.

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 for use directly.

RMQ.Utils.reply/4 is imported as well which is convenient for the case when the consumer implements RPC.

When :concurrency is true this function will be executed in the spawned process using Kernel.spawn/1.

Link to this callback

setup_queue(chan, config)

View Source

Specs

setup_queue(chan :: AMQP.Channel.t(), config :: keyword()) :: :ok

Does all the job on preparing the queue.

Whenever you need full control over configuring the queue you can implement this callback and use AMQP library directly.

See setup_queue/2 for the default implementation.