javaspringspring-amqpspring-rabbitspring-context

Spring AMQP global config for RabbitListenerErrorHandler


I want to remove the errorHandler section from the @RabbitListener(queues = "${service.queue.name}", errorHandler = "amqpExceptionHandler") anotation, as it's common for all my listeners.

Is there any way to set global config for RabbitListenerErrorHandler?

@Component
public class AmqpExceptionHandler implements RabbitListenerErrorHandler {
    @Override
    public Object handleError(
        Message amqpMessage,
        org.springframework.messaging.Message<?> message,
        ListenerExecutionFailedException exception) {
        
        if (exception.getCause() instanceof MyException) {
            // some logic
        }
        throw exception;
    }
}

Solution

  • That one is possible via meta-annotation: https://docs.spring.io/spring-amqp/reference/amqp/receiving-messages/async-annotation-driven/meta.html.

    You might need to repeat most of the attributes to let to customize specific listener method while keeping all the common only in your annotation.