In Spring AMQP, the ErrorHandler interface (org.springframework.util.ErrorHandler) is a callback that a RabbitMQ listener container invokes when a message listener throws an exception. You set it on the container factory to decide whether the failure is logged, wrapped, or turned into a rejection that sends the message to a dead letter queue instead of being requeued forever.
By default, Spring AMQP installs a ConditionalRejectingErrorHandler. It treats some exceptions as fatal and rejects the message without requeueing, and it leaves other exceptions to the container's requeue behaviour.
When a @RabbitListener method throws, the exception reaches the listener container. The container passes it to the ErrorHandler. Whatever the handler throws then decides how the broker is told:
AmqpRejectAndDontRequeueException, the message is rejected and not requeued. With a dead letter exchange configured on the queue, RabbitMQ routes it there.defaultRequeueRejected is true (the default), the message is requeued and delivered again. A permanently failing message then loops indefinitely.This second behaviour is the most common production issue with message consumers, which is why an explicit error strategy matters.
ConditionalRejectingErrorHandler uses a FatalExceptionStrategy. The default strategy treats exceptions that retrying cannot fix as fatal, for example message conversion errors (MessageConversionException), method argument conversion and validation failures, and a missing listener method. For those it throws AmqpRejectAndDontRequeueException, so a malformed payload does not loop. Other exceptions follow normal requeue behaviour.
Define the container factory yourself and set the handler. This example uses Spring Boot 3 with Spring AMQP 3.
The handler receives a ListenerExecutionFailedException that wraps the original exception, which is why it checks getCause(). Validation errors are converted into a non-requeued rejection. Other failures are rethrown so the container's normal behaviour (requeue or retry) applies.
Rejecting without requeueing only helps if the queue has a dead letter exchange; otherwise the message is dropped. Declare it with queue arguments:
Then bind a queue to orders.dlx with the routing key orders.dead, and inspect or replay messages from it.
| Option | Purpose |
|---|---|
RabbitListenerErrorHandler | Set per listener with @RabbitListener(errorHandler = "beanName"). It can translate the failure into a reply for request-reply listeners. |
spring.rabbitmq.listener.simple.default-requeue-rejected=false | Never requeue failed messages, so they go to the dead letter queue. |
spring.rabbitmq.listener.simple.retry.enabled=true | Stateless in-process retries with back-off before the failure reaches the handler. |
| Quorum queues with delivery limit | Broker-side cap on redeliveries, which dead letters poison messages. |
A simple and robust setup is to retry a few times, then dead letter, and alert on the dead letter queue depth.
The listener throws an ordinary exception and defaultRequeueRejected is true, so RabbitMQ requeues it. Throw AmqpRejectAndDontRequeueException, or set the default to false and configure a dead letter exchange.
ErrorHandler is container-wide and handles any listener failure. RabbitListenerErrorHandler is attached to one listener and can return a value that is used as the reply.
No. It covers exceptions from listener execution. Connection and consumer problems are handled by the container's recovery logic and logged separately.
The Spring AMQP ErrorHandler decides what happens when a listener fails. The default ConditionalRejectingErrorHandler rejects unrecoverable errors, and you can supply your own to log, classify and reject. Pair it with a dead letter exchange and, where useful, retry settings, so a poison message never loops forever.