0

I have two types of exchanges in my project: DirectExchange and TopicExchange. I configured the amqp template the following way:

  @Bean
  public AmqpTemplate amqpTemplate(ConnectionFactory amqpConnectionFactory,
      Jackson2JsonMessageConverter jsonMessageConverter) {
    final RabbitTemplate rabbitTemplate = new RabbitTemplate();
    rabbitTemplate.setConnectionFactory(amqpConnectionFactory);
    rabbitTemplate.setMessageConverter(jsonMessageConverter);
    // will retry for 3 times (default value)
    rabbitTemplate.setRetryTemplate(new RetryTemplate());
    return rabbitTemplate;
  }

I also have a recoverer, to handle the failed message and re-queue it into a dead-letter-queue:

  private RepublishMessageRecoverer getRepublishMessageRecoverer(TopicExchange errorExchange) {
    return new RepublishMessageRecoverer(amqpTemplate, errorExchange.getName()).errorRoutingKeyPrefix(
        generalMessagingConfigParams.getGeneralErrorPrefix());
  }

I basically did the same thing for DirectExchange and everything works as expected: message gets retried 3 times, then re-queued into the queue with "error." prefix. However, for TopicExchange the retries go on infinitely.

I also have the following properties set:

spring.rabbitmq.listener.simple.retry.enabled=true
spring.rabbitmq.listener.simple.retry.initial-interval=1000
spring.rabbitmq.listener.simple.retry.max-attempts=3
spring.rabbitmq.listener.simple.retry.max-interval=1000
spring.rabbitmq.listener.simple.retry.multiplier=1.0
spring.rabbitmq.listener.simple.retry.stateless=true

The stack trace when I add a test exception in the listener of the topic exchange is:

2023-09-01T12:10:59,136 INFO TopicMessagingObjectRegister Received message for {flowname} flow with payload: {sensitive info payload}
2023-09-01T12:10:59,137 WARN ConditionalRejectingErrorHandler Execution of Rabbit message listener failed.
    org.springframework.amqp.rabbit.support.ListenerExecutionFailedException: Listener threw exception
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.wrapToListenerExecutionFailedExceptionIfNeeded(AbstractMessageListenerContainer.java:1787) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1733) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.actualInvokeListener(AbstractMessageListenerContainer.java:1603) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.invokeListener(AbstractMessageListenerContainer.java:1580) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doExecuteListener(AbstractMessageListenerContainer.java:1571) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.executeListener(AbstractMessageListenerContainer.java:1515) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.doReceiveAndExecute(SimpleMessageListenerContainer.java:994) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.receiveAndExecute(SimpleMessageListenerContainer.java:941) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer.access$1600(SimpleMessageListenerContainer.java:85) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.mainLoop(SimpleMessageListenerContainer.java:1319) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at org.springframework.amqp.rabbit.listener.SimpleMessageListenerContainer$AsyncMessageProcessingConsumer.run(SimpleMessageListenerContainer.java:1225) ~[spring-rabbit-2.4.8.jar:2.4.8]
        at java.lang.Thread.run(Thread.java:833) ~[?:?]
    Caused by: MessageReceiverException: Error processing message (Body:'[B@11be560c(byte[196])' MessageProperties [headers={__TypeId__=type, contentType=application/json, contentEncoding=UTF-8, contentLength=0, receivedDeliveryMode=PERSISTENT, priority=0, redelivered=true, receivedExchange={topic_exchange}, receivedRoutingKey={routingKey}, deliveryTag=383, consumerTag={tag}, consumerQueue={queue}])
at de.juris.messaging.config.topic.TopicMessagingObjectRegister.handleMessage(TopicMessagingObjectRegister.java:138) ~[classes/:?]
at de.juris.messaging.config.topic.TopicMessagingObjectRegister.lambda$registerEndpoint$0(TopicMessagingObjectRegister.java:112) ~[classes/:?]
at org.springframework.amqp.rabbit.listener.AbstractMessageListenerContainer.doInvokeListener(AbstractMessageListenerContainer.java:1729) ~[spring-rabbit-2.4.8.jar:2.4.8]
... 10 more
    Caused by: java.lang.RuntimeException: test

For the DirectExchange, the same stacktrace appears but only 3 times then message goes into "error.queue"

PS: Some sensitive info is not included in the logs

Gabriela83
  • 53
  • 5
  • Consumers don't "listen" on exchanges; they listen to queues; the template publishes to exchanges. You have provided no consumer configuration (aside from the properties). You need to provide much more context and configuration. – Gary Russell Sep 01 '23 at 14:08

0 Answers0