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 implementsRMQ.Connection
behaviour;:queue
- the name of the queue to consume. Will be created if does not exist;:exchange
- the name of the exchange to whichqueue
should be bound. Also accepts two-element tuple{type, name}
. Defaults to""
;:routing_key
- queue binding key. Defaults toqueue
; Will be created if does not exist. Defaults to""
;:dead_letter
- defines if the consumer should setup deadletter exchange and queue. Defaults totrue
;:dead_letter_queue
- the name of dead letter queue. Defaults to"#{queue}_error"
;:dead_letter_exchange
- the name of the exchange to whichdead_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 toqueue
;:concurrency
- defines ifconsume/3
callback will be called in a separate process. Defaults totrue
;:prefetch_count
- sets the message prefetch count. Defaults to10
;:consumer_tag
- consumer tag. Defaults to a current module name;:restart_delay
- restart delay. Defaults to5000
.
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
Link to this section Callbacks
Link to this callback
consume(chan, payload, meta)
View Sourceconsume(chan :: AMQP.Channel.t(), payload :: any(), meta :: Map.t()) :: any()
Consumes the message.
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 Sourcestart_link(options :: [GenServer.option()]) :: GenServer.on_start()
Starts a GenServer
process linked to the current process.