From c9fd46f93d850ec41f552a5ee5d31b81ef7e49c8 Mon Sep 17 00:00:00 2001 From: Gary Russell Date: Tue, 23 Jul 2019 09:28:58 -0400 Subject: [PATCH] Fix ImmediateRequeueAmqpException Fixes https://github.com/spring-projects/spring-amqp/issues/1053 `ContainerUtils.shouldRequeue` did not traverse the cause tree so we never matched on this exception. --- .../amqp/rabbit/listener/ContainerUtils.java | 13 +++-- .../rabbit/listener/ContainerUtilsTests.java | 52 +++++++++++++++++++ 2 files changed, 61 insertions(+), 4 deletions(-) create mode 100644 spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ContainerUtilsTests.java diff --git a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/ContainerUtils.java b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/ContainerUtils.java index 79f018e0..14b034e4 100644 --- a/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/ContainerUtils.java +++ b/spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/ContainerUtils.java @@ -39,7 +39,8 @@ public final class ContainerUtils { /** * Determine whether a message should be requeued; returns true if the throwable is a * {@link MessageRejectedWhileStoppingException} or defaultRequeueRejected is true and - * there is not an {@link AmqpRejectAndDontRequeueException} in the cause chain. + * there is not an {@link AmqpRejectAndDontRequeueException} in the cause chain or if + * there is an {@link ImmediateRequeueAmqpException} in the cause chain. * @param defaultRequeueRejected the default requeue rejected. * @param throwable the throwable. * @param logger the logger to use for debug. @@ -47,12 +48,16 @@ public final class ContainerUtils { */ public static boolean shouldRequeue(boolean defaultRequeueRejected, Throwable throwable, Log logger) { boolean shouldRequeue = defaultRequeueRejected || - throwable instanceof MessageRejectedWhileStoppingException || - throwable instanceof ImmediateRequeueAmqpException; + throwable instanceof MessageRejectedWhileStoppingException; Throwable t = throwable; - while (shouldRequeue && t != null) { + while (t != null) { if (t instanceof AmqpRejectAndDontRequeueException) { shouldRequeue = false; + break; + } + else if (t instanceof ImmediateRequeueAmqpException) { + shouldRequeue = true; + break; } t = t.getCause(); } diff --git a/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ContainerUtilsTests.java b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ContainerUtilsTests.java new file mode 100644 index 00000000..91181183 --- /dev/null +++ b/spring-rabbit/src/test/java/org/springframework/amqp/rabbit/listener/ContainerUtilsTests.java @@ -0,0 +1,52 @@ +/* + * Copyright 2019 the original author or authors. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.springframework.amqp.rabbit.listener; + +import static org.assertj.core.api.Assertions.assertThat; +import static org.mockito.Mockito.mock; + +import org.apache.commons.logging.Log; +import org.junit.jupiter.api.Test; + +import org.springframework.amqp.AmqpRejectAndDontRequeueException; +import org.springframework.amqp.ImmediateRequeueAmqpException; +import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException; + +/** + * @author Gary Russell + * @since 2.1.8 + * + */ +public class ContainerUtilsTests { + + @Test + void testMustRequeue() { + assertThat(ContainerUtils.shouldRequeue(false, + new ListenerExecutionFailedException("", new ImmediateRequeueAmqpException("requeue")), + mock(Log.class))) + .isTrue(); + } + + @Test + void testMustNotRequeue() { + assertThat(ContainerUtils.shouldRequeue(true, + new ListenerExecutionFailedException("", new AmqpRejectAndDontRequeueException("no requeue")), + mock(Log.class))) + .isFalse(); + } + +}