Skip to content

Commit

Permalink
spring-projects#264 add noRecoveryForNotRetryable flag
Browse files Browse the repository at this point in the history
  • Loading branch information
ttulka committed Apr 12, 2022
1 parent af950da commit c4614dc
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ private MethodInterceptor getStatelessInterceptor(Object target, Method method,
RetryTemplate template = createTemplate(retryable.listeners());
template.setRetryPolicy(getRetryPolicy(retryable));
template.setBackOffPolicy(getBackoffPolicy(retryable.backoff()));
template.setThrowLastExceptionOnExhausted(retryable.rethrow());
template.setNoRecoveryForNotRetryable(retryable.rethrow());
return RetryInterceptorBuilder.stateless().retryOperations(template).label(retryable.label())
.recoverer(getRecoverer(target, method, retryable.rethrow())).build();
}
Expand All @@ -229,7 +229,7 @@ private MethodInterceptor getStatefulInterceptor(Object target, Method method, R
boolean rethrow = retryable.rethrow();
RetryTemplate template = createTemplate(retryable.listeners());
template.setRetryContextCache(this.retryContextCache);
template.setThrowLastExceptionOnExhausted(rethrow);
template.setNoRecoveryForNotRetryable(rethrow);

CircuitBreaker circuit = AnnotatedElementUtils.findMergedAnnotation(method, CircuitBreaker.class);
if (circuit == null) {
Expand Down
15 changes: 12 additions & 3 deletions src/main/java/org/springframework/retry/support/RetryTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,8 @@ public class RetryTemplate implements RetryOperations {

private boolean throwLastExceptionOnExhausted;

private boolean noRecoveryForNotRetryable;

/**
* Main entry point to configure RetryTemplate using fluent API. See
* {@link RetryTemplateBuilder} for usage examples and details.
Expand All @@ -119,12 +121,19 @@ public static RetryTemplate defaultInstance() {

/**
* @param throwLastExceptionOnExhausted the throwLastExceptionOnExhausted to set
* @since 1.3.3
*/
public void setThrowLastExceptionOnExhausted(boolean throwLastExceptionOnExhausted) {
this.throwLastExceptionOnExhausted = throwLastExceptionOnExhausted;
}

/**
* @param noRecoveryForNotRetryable the noRecoveryForNotRetryable to set
* @since 1.3.3
*/
public void setNoRecoveryForNotRetryable(boolean noRecoveryForNotRetryable) {
this.noRecoveryForNotRetryable = noRecoveryForNotRetryable;
}

/**
* Public setter for the {@link RetryContextCache}.
* @param retryContextCache the {@link RetryContextCache} to set.
Expand Down Expand Up @@ -533,7 +542,7 @@ protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, Retry
if (state != null && !context.hasAttribute(GLOBAL_STATE)) {
this.retryContextCache.remove(state.getKey());
}
if (this.throwLastExceptionOnExhausted && retryPolicy instanceof SimpleRetryPolicy
if (this.noRecoveryForNotRetryable && retryPolicy instanceof SimpleRetryPolicy
&& !((SimpleRetryPolicy) retryPolicy).retryForException(context.getLastThrowable())) {
throw context.getLastThrowable();
}
Expand All @@ -550,7 +559,7 @@ protected <T> T handleRetryExhausted(RecoveryCallback<T> recoveryCallback, Retry
}

protected <E extends Throwable> void rethrow(RetryContext context, String message) throws E {
if (this.throwLastExceptionOnExhausted) {
if (this.throwLastExceptionOnExhausted || this.noRecoveryForNotRetryable) {
@SuppressWarnings("unchecked")
E rethrow = (E) context.getLastThrowable();
throw rethrow;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -384,12 +384,12 @@ public boolean rollbackFor(Throwable exception) {
}

@Test
public void testRethrowNotRetryable() throws Throwable {
public void testRethrowForNotRetryable() throws Throwable {
SimpleRetryPolicy policy = new SimpleRetryPolicy(1,
Collections.<Class<? extends Throwable>, Boolean>singletonMap(IllegalArgumentException.class, true));
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(policy);
retryTemplate.setThrowLastExceptionOnExhausted(true);
retryTemplate.setNoRecoveryForNotRetryable(true);
try {
retryTemplate.execute(new RetryCallback<Object, Exception>() {
@Override
Expand All @@ -410,12 +410,12 @@ public Object recover(RetryContext context) throws Exception {
}

@Test
public void testRethrowRetryable() throws Throwable {
public void testRethrowForRetryable() throws Throwable {
SimpleRetryPolicy policy = new SimpleRetryPolicy(1,
Collections.<Class<? extends Throwable>, Boolean>singletonMap(RuntimeException.class, true));
RetryTemplate retryTemplate = new RetryTemplate();
retryTemplate.setRetryPolicy(policy);
retryTemplate.setThrowLastExceptionOnExhausted(true);
retryTemplate.setNoRecoveryForNotRetryable(true);
final Object value = new Object();
Object result = retryTemplate.execute(new RetryCallback<Object, Exception>() {
@Override
Expand Down

0 comments on commit c4614dc

Please sign in to comment.