Skip to content

Commit c9fd46f

Browse files
garyrussellartembilan
authored andcommitted
Fix ImmediateRequeueAmqpException
Fixes #1053 `ContainerUtils.shouldRequeue` did not traverse the cause tree so we never matched on this exception.
1 parent 586fa83 commit c9fd46f

File tree

2 files changed

+61
-4
lines changed

2 files changed

+61
-4
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/listener/ContainerUtils.java

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -39,20 +39,25 @@ private ContainerUtils() {
3939
/**
4040
* Determine whether a message should be requeued; returns true if the throwable is a
4141
* {@link MessageRejectedWhileStoppingException} or defaultRequeueRejected is true and
42-
* there is not an {@link AmqpRejectAndDontRequeueException} in the cause chain.
42+
* there is not an {@link AmqpRejectAndDontRequeueException} in the cause chain or if
43+
* there is an {@link ImmediateRequeueAmqpException} in the cause chain.
4344
* @param defaultRequeueRejected the default requeue rejected.
4445
* @param throwable the throwable.
4546
* @param logger the logger to use for debug.
4647
* @return true to requeue.
4748
*/
4849
public static boolean shouldRequeue(boolean defaultRequeueRejected, Throwable throwable, Log logger) {
4950
boolean shouldRequeue = defaultRequeueRejected ||
50-
throwable instanceof MessageRejectedWhileStoppingException ||
51-
throwable instanceof ImmediateRequeueAmqpException;
51+
throwable instanceof MessageRejectedWhileStoppingException;
5252
Throwable t = throwable;
53-
while (shouldRequeue && t != null) {
53+
while (t != null) {
5454
if (t instanceof AmqpRejectAndDontRequeueException) {
5555
shouldRequeue = false;
56+
break;
57+
}
58+
else if (t instanceof ImmediateRequeueAmqpException) {
59+
shouldRequeue = true;
60+
break;
5661
}
5762
t = t.getCause();
5863
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
* Copyright 2019 the original author or authors.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* https://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package org.springframework.amqp.rabbit.listener;
18+
19+
import static org.assertj.core.api.Assertions.assertThat;
20+
import static org.mockito.Mockito.mock;
21+
22+
import org.apache.commons.logging.Log;
23+
import org.junit.jupiter.api.Test;
24+
25+
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
26+
import org.springframework.amqp.ImmediateRequeueAmqpException;
27+
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
28+
29+
/**
30+
* @author Gary Russell
31+
* @since 2.1.8
32+
*
33+
*/
34+
public class ContainerUtilsTests {
35+
36+
@Test
37+
void testMustRequeue() {
38+
assertThat(ContainerUtils.shouldRequeue(false,
39+
new ListenerExecutionFailedException("", new ImmediateRequeueAmqpException("requeue")),
40+
mock(Log.class)))
41+
.isTrue();
42+
}
43+
44+
@Test
45+
void testMustNotRequeue() {
46+
assertThat(ContainerUtils.shouldRequeue(true,
47+
new ListenerExecutionFailedException("", new AmqpRejectAndDontRequeueException("no requeue")),
48+
mock(Log.class)))
49+
.isFalse();
50+
}
51+
52+
}

0 commit comments

Comments
 (0)