Hi,
I’m using BroadwayRabbitMQ.Producer to read from a RabbitMQ queue and handle the messages. However, I’m having trouble redirecting to a dead-letter queue for failed messages. In the docs it is mentioned several times, but never implemented in an example. And I simply can’t find any example on the internet as well.
I can flag the message as failed in handle_message/3
with Broadway.Message.failed/2
. The message then ends up in handle_failed/2
, where I want to redirect it to the dead-letter exchange/queue. There are two steps as far as I understand:
- ACK to the original queue
- Send to the dead-letter queue/exchange
I would expect to be able to reuse the existing connection to RabbitMQ to redirect to the dead-letter queue/exchange somehow.
The queues are setup as follows:
with {:ok, queue_base} = Application.fetch_env(:amqp, :some_named_queue),
{:ok, connection_options} = Application.fetch_env(:amqp, :connection_options),
{:ok, conn} <- Connection.open(connection_options),
{:ok, chan} <- Channel.open(conn),
{:ok, _} <- Queue.declare(chan, "#{queue_base}_error", durable: true),
# Messages that cannot be delivered to any consumer in the main queue will be routed to the error queue
{:ok, _} <- Queue.declare(chan, queue_base,
durable: true,
arguments: [
{"x-dead-letter-exchange", :longstr, ""},
{"x-dead-letter-routing-key", :longstr, "#{queue_base}_error"}
]
),
:ok <- Exchange.fanout(chan, "#{queue_base}_exchange", durable: true),
:ok <- Queue.bind(chan, queue_base, "#{queue_base}_exchange") do
:ok
end
Thanks, Bas