Skip to content

Commit c4614dc

Browse files
committed
spring-projects#264 add noRecoveryForNotRetryable flag
1 parent af950da commit c4614dc

File tree

3 files changed

+18
-9
lines changed

3 files changed

+18
-9
lines changed

src/main/java/org/springframework/retry/annotation/AnnotationAwareRetryOperationsInterceptor.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ private MethodInterceptor getStatelessInterceptor(Object target, Method method,
220220
RetryTemplate template = createTemplate(retryable.listeners());
221221
template.setRetryPolicy(getRetryPolicy(retryable));
222222
template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
223-
template.setThrowLastExceptionOnExhausted(retryable.rethrow());
223+
template.setNoRecoveryForNotRetryable(retryable.rethrow());
224224
return RetryInterceptorBuilder.stateless().retryOperations(template).label(retryable.label())
225225
.recoverer(getRecoverer(target, method, retryable.rethrow())).build();
226226
}
@@ -229,7 +229,7 @@ private MethodInterceptor getStatefulInterceptor(Object target, Method method, R
229229
boolean rethrow = retryable.rethrow();
230230
RetryTemplate template = createTemplate(retryable.listeners());
231231
template.setRetryContextCache(this.retryContextCache);
232-
template.setThrowLastExceptionOnExhausted(rethrow);
232+
template.setNoRecoveryForNotRetryable(rethrow);
233233

234234
CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
235235
if (circuit == null) {

src/main/java/org/springframework/retry/support/RetryTemplate.java

+12-3
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,8 @@ public class RetryTemplate implements RetryOperations {
9696

9797
private boolean throwLastExceptionOnExhausted;
9898

99+
private boolean noRecoveryForNotRetryable;
100+
99101
/**
100102
* Main entry point to configure RetryTemplate using fluent API. See
101103
* {@link RetryTemplateBuilder} for usage examples and details.
@@ -119,12 +121,19 @@ public static RetryTemplate defaultInstance() {
119121

120122
/**
121123
* @param throwLastExceptionOnExhausted the throwLastExceptionOnExhausted to set
122-
* @since 1.3.3
123124
*/
124125
public void setThrowLastExceptionOnExhausted(boolean throwLastExceptionOnExhausted) {
125126
this.throwLastExceptionOnExhausted = throwLastExceptionOnExhausted;
126127
}
127128

129+
/**
130+
* @param noRecoveryForNotRetryable the noRecoveryForNotRetryable to set
131+
* @since 1.3.3
132+
*/
133+
public void setNoRecoveryForNotRetryable(boolean noRecoveryForNotRetryable) {
134+
this.noRecoveryForNotRetryable = noRecoveryForNotRetryable;
135+
}
136+
128137
/**
129138
* Public setter for the {@link RetryContextCache}.
130139
* @param retryContextCache the {@link RetryContextCache} to set.
@@ -533,7 +542,7 @@ protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, Retry
533542
if (state != null && !context.hasAttribute(GLOBAL_STATE)) {
534543
this.retryContextCache.remove(state.getKey());
535544
}
536-
if (this.throwLastExceptionOnExhausted && retryPolicy instanceof SimpleRetryPolicy
545+
if (this.noRecoveryForNotRetryable && retryPolicy instanceof SimpleRetryPolicy
537546
&& !((SimpleRetryPolicy) retryPolicy).retryForException(context.getLastThrowable())) {
538547
throw context.getLastThrowable();
539548
}
@@ -550,7 +559,7 @@ protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, Retry
550559
}
551560

552561
protected <E extends Throwable> void rethrow(RetryContext context, String message) throws E {
553-
if (this.throwLastExceptionOnExhausted) {
562+
if (this.throwLastExceptionOnExhausted || this.noRecoveryForNotRetryable) {
554563
@SuppressWarnings("unchecked")
555564
E rethrow = (E) context.getLastThrowable();
556565
throw rethrow;

src/test/java/org/springframework/retry/support/RetryTemplateTests.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -384,12 +384,12 @@ public boolean rollbackFor(Throwable exception) {
384384
}
385385

386386
@Test
387-
public void testRethrowNotRetryable() throws Throwable {
387+
public void testRethrowForNotRetryable() throws Throwable {
388388
SimpleRetryPolicy policy = new SimpleRetryPolicy(1,
389389
Collections.<Class<? extends Throwable>, Boolean>singletonMap(IllegalArgumentException.class, true));
390390
RetryTemplate retryTemplate = new RetryTemplate();
391391
retryTemplate.setRetryPolicy(policy);
392-
retryTemplate.setThrowLastExceptionOnExhausted(true);
392+
retryTemplate.setNoRecoveryForNotRetryable(true);
393393
try {
394394
retryTemplate.execute(new RetryCallback<Object, Exception>() {
395395
@Override
@@ -410,12 +410,12 @@ public Object recover(RetryContext context) throws Exception {
410410
}
411411

412412
@Test
413-
public void testRethrowRetryable() throws Throwable {
413+
public void testRethrowForRetryable() throws Throwable {
414414
SimpleRetryPolicy policy = new SimpleRetryPolicy(1,
415415
Collections.<Class<? extends Throwable>, Boolean>singletonMap(RuntimeException.class, true));
416416
RetryTemplate retryTemplate = new RetryTemplate();
417417
retryTemplate.setRetryPolicy(policy);
418-
retryTemplate.setThrowLastExceptionOnExhausted(true);
418+
retryTemplate.setNoRecoveryForNotRetryable(true);
419419
final Object value = new Object();
420420
Object result = retryTemplate.execute(new RetryCallback<Object, Exception>() {
421421
@Override

0 commit comments

Comments
 (0)