Search code examples
javaspring-bootrabbitmqspring-rabbit

Spring boot rabbitmq dead queue not stopping flow on exception


I am learning spring boot (version 2.2.5.RELEASE) with RabbitMQ based on online resources:

The issue I am experiencing is that when I am throwing purposely an exception, it should retry only 6 times and after that the message should be moved to the dead queue but it is not the case as it keeps retrying without stopping.

Please find below my application.yml:

server:
  servlet:
    contextPath: /test

spring:
  rabbitmq:
    username: guest
    password: guest
    listener:
      simple:
        retry:
          enabled: true
          initial-interval: 3s
          max-attempts: 6
          max-interval: 10s
          multiplier: 2

Please find below rabbitMQ configuration class:

@Configuration
public class RabbitMQConfig {

 
    @Bean
    TopicExchange deadLetterExchange() {
        return new TopicExchange("deadLetterExchange");
    }

    @Bean
    TopicExchange exchange() {
        return new TopicExchange("javainuseExchange");
    }

    @Bean
    Queue dlq() {
        return QueueBuilder.durable("deadLetter.queue").build();
    }

    @Bean
    Queue queue() {
        return QueueBuilder.durable("javainuse.queue").withArgument("x-dead-letter-exchange", "deadLetterExchange")
                .withArgument("x-dead-letter-routing-key", "deadLetter").build();
    }

    @Bean
    Binding DLQbinding() {
        return BindingBuilder.bind(dlq()).to(deadLetterExchange()).with("deadLetter");
    }

    @Bean
    Binding binding() {
        return BindingBuilder.bind(queue()).to(exchange()).with("javainuse");
    }

    @Bean
    SimpleMessageListenerContainer container(ConnectionFactory connectionFactory, MessageListenerAdapter listenerAdapter) {
        SimpleMessageListenerContainer container = new SimpleMessageListenerContainer();
        container.setConnectionFactory(connectionFactory);
        container.setQueueNames("javainuse.queue");
        container.setMessageListener(listenerAdapter);
        return container;
    }

    @Bean
    MessageListenerAdapter listenerAdapter(ReceiveMessageHandler receiver) {
        return new MessageListenerAdapter(receiver, "handleMessage");
    }

}

Please find below my class that consume the message:

@Component
@Slf4j
@RequiredArgsConstructor
public class ReceiveMessageHandler {

   
    private final SendService sendService;

    public void handleMessage(@Valid dto dto) {

        String test = null;

      
        //purposely throw null pointer to test dead queue
        test.toString();

        sendService.sendSomething(dto);
   
    }

When the null pointer exception is thrown it keeps retrying instead of stopping after 6 times max-attempts: 6 as per application.yml config.

Can anyone point me what I am missing here, please?


Solution

  • You are creating your own container bean so the .yml properties are not being applied. Use a RetryInterceptorBuilder.stateless() ... to build the retry advice and inject it into the listener container (advice chain).

    You also need to configure the recoverer.

    builder.recoverer(new RejectAndDontRequeueRecoverer());
    

    Which will direct the queue to the DLQ when retries are exhausted.

    See the Spring AMQP documentation.