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.
This commit is contained in:
Gary Russell
2019-07-23 09:28:58 -04:00
committed by Artem Bilan
parent 586fa83698
commit c9fd46f93d
2 changed files with 61 additions and 4 deletions

View File

@@ -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();
}

View File

@@ -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();
}
}