Skip to content

Commit 682f2fe

Browse files
authored
GH-1335: RejectAndDontRequeueRecoverer Improvement
Resolves #1335 Allow customization of exception message. * Add simple message CTOR. * Fix javadoc.
1 parent 09f8b89 commit 682f2fe

File tree

2 files changed

+38
-5
lines changed

2 files changed

+38
-5
lines changed

spring-rabbit/src/main/java/org/springframework/amqp/rabbit/retry/RejectAndDontRequeueRecoverer.java

Lines changed: 34 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2019 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -16,12 +16,15 @@
1616

1717
package org.springframework.amqp.rabbit.retry;
1818

19+
import java.util.function.Supplier;
20+
1921
import org.apache.commons.logging.Log;
2022
import org.apache.commons.logging.LogFactory;
2123

2224
import org.springframework.amqp.AmqpRejectAndDontRequeueException;
2325
import org.springframework.amqp.core.Message;
2426
import org.springframework.amqp.rabbit.support.ListenerExecutionFailedException;
27+
import org.springframework.util.Assert;
2528

2629
/**
2730
* MessageRecover that causes the listener container to reject
@@ -35,14 +38,42 @@
3538
*/
3639
public class RejectAndDontRequeueRecoverer implements MessageRecoverer {
3740

38-
protected Log logger = LogFactory.getLog(RejectAndDontRequeueRecoverer.class); // NOSONAR protected
41+
private final Supplier<String> messageSupplier;
42+
43+
protected final Log logger = LogFactory.getLog(getClass()); // NOSONAR protected
44+
45+
/**
46+
* Construct an instance with the default exception message.
47+
*/
48+
public RejectAndDontRequeueRecoverer() {
49+
this(() -> "Retry Policy Exhausted");
50+
}
51+
52+
/**
53+
* Construct an instance with the provided exception message.
54+
* @param message the message.
55+
* @since 2.3.7
56+
*/
57+
public RejectAndDontRequeueRecoverer(String message) {
58+
this(() -> message);
59+
}
60+
61+
/**
62+
* Construct an instance with the provided exception message supplier.
63+
* @param messageSupplier the message supplier.
64+
* @since 2.3.7
65+
*/
66+
public RejectAndDontRequeueRecoverer(Supplier<String> messageSupplier) {
67+
Assert.notNull(messageSupplier, "'messageSupplier' cannot be null");
68+
this.messageSupplier = messageSupplier;
69+
}
3970

4071
@Override
4172
public void recover(Message message, Throwable cause) {
4273
if (this.logger.isWarnEnabled()) {
4374
this.logger.warn("Retries exhausted for message " + message, cause);
4475
}
45-
throw new ListenerExecutionFailedException("Retry Policy Exhausted",
76+
throw new ListenerExecutionFailedException(this.messageSupplier.get(),
4677
new AmqpRejectAndDontRequeueException(cause), message);
4778
}
4879

spring-rabbit/src/test/java/org/springframework/amqp/rabbit/retry/MissingIdRetryTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -58,6 +58,7 @@
5858
import org.springframework.retry.policy.MapRetryContextCache;
5959
import org.springframework.retry.policy.RetryContextCache;
6060
import org.springframework.retry.policy.SimpleRetryPolicy;
61+
import org.springframework.retry.support.RetrySynchronizationManager;
6162
import org.springframework.retry.support.RetryTemplate;
6263

6364
/**
@@ -143,7 +144,8 @@ public void testWithId() throws Exception {
143144
RetryContextCache cache = spy(new MapRetryContextCache());
144145
retryTemplate.setRetryContextCache(cache);
145146
fb.setRetryOperations(retryTemplate);
146-
fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer());
147+
fb.setMessageRecoverer(new RejectAndDontRequeueRecoverer(() ->
148+
"Don't requeue after " + RetrySynchronizationManager.getContext().getRetryCount() + " attempts"));
147149

148150
Advice retryInterceptor = fb.getObject();
149151
container.setAdviceChain(retryInterceptor);

0 commit comments

Comments
 (0)