Skip to content

Commit

Permalink
spring-projectsGH-383: maxAttemptsExpression Ignored Sometimes
Browse files Browse the repository at this point in the history
Resolves spring-projects#383

If explicit exceptions are referenced, and an `exceptionExpression` is
provided, the `maxAttemptsExpression` was ignored.
  • Loading branch information
garyrussell committed Oct 2, 2023
1 parent fa00aa1 commit 91c0203
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -352,7 +352,7 @@ private RetryPolicy getRetryPolicy(Annotation retryable, boolean stateless) {
@SuppressWarnings("unchecked")
Class<? extends Throwable>[] includes = (Class<? extends Throwable>[]) attrs.get("value");
String exceptionExpression = (String) attrs.get("exceptionExpression");
boolean hasExpression = StringUtils.hasText(exceptionExpression);
boolean hasExceptionExpression = StringUtils.hasText(exceptionExpression);
if (includes.length == 0) {
@SuppressWarnings("unchecked")
Class<? extends Throwable>[] value = (Class<? extends Throwable>[]) attrs.get("retryFor");
Expand All @@ -370,14 +370,14 @@ private RetryPolicy getRetryPolicy(Annotation retryable, boolean stateless) {
parsedExpression = null;
}
}
final Expression expression = parsedExpression;
final Expression maxAttExpression = parsedExpression;
SimpleRetryPolicy simple = null;
if (includes.length == 0 && excludes.length == 0) {
simple = hasExpression
simple = hasExceptionExpression
? new ExpressionRetryPolicy(resolve(exceptionExpression)).withBeanFactory(this.beanFactory)
: new SimpleRetryPolicy();
if (expression != null) {
simple.maxAttemptsSupplier(() -> evaluate(expression, Integer.class, stateless));
if (maxAttExpression != null) {
simple.maxAttemptsSupplier(() -> evaluate(maxAttExpression, Integer.class, stateless));
}
else {
simple.setMaxAttempts(maxAttempts);
Expand All @@ -392,16 +392,16 @@ private RetryPolicy getRetryPolicy(Annotation retryable, boolean stateless) {
}
boolean retryNotExcluded = includes.length == 0;
if (simple == null) {
if (hasExpression) {
if (hasExceptionExpression) {
simple = new ExpressionRetryPolicy(maxAttempts, policyMap, true, resolve(exceptionExpression),
retryNotExcluded)
.withBeanFactory(this.beanFactory);
}
else {
simple = new SimpleRetryPolicy(maxAttempts, policyMap, true, retryNotExcluded);
if (expression != null) {
simple.maxAttemptsSupplier(() -> evaluate(expression, Integer.class, stateless));
}
}
if (maxAttExpression != null) {
simple.maxAttemptsSupplier(() -> evaluate(maxAttExpression, Integer.class, stateless));
}
}
@SuppressWarnings("unchecked")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -560,22 +560,22 @@ public static class RuntimeConfigs {
int count = 0;

public int getMaxAttempts() {
count++;
this.count++;
return 3;
}

public long getInitial() {
count++;
this.count++;
return 1000;
}

public long getMax() {
count++;
this.count++;
return 2000;
}

public double getMult() {
count++;
this.count++;
return 1.2;
}

Expand Down Expand Up @@ -639,10 +639,10 @@ protected static class RecoverableService {
@Retryable(retryFor = RuntimeException.class, noRetryFor = IllegalStateException.class,
notRecoverable = { IllegalArgumentException.class, IllegalStateException.class })
public void service() {
if (this.count++ >= 3 && count < 7) {
if (this.count++ >= 3 && this.count < 7) {
throw new IllegalArgumentException("Planned");
}
else if (count > 6) {
else if (this.count > 6) {
throw new IllegalStateException("Planned");
}
else {
Expand Down Expand Up @@ -801,7 +801,7 @@ public void service2() {
throw new RuntimeException("this cannot be retried");
}

@Retryable(exceptionExpression = "@exceptionChecker.${retryMethod}(#root)",
@Retryable(exceptionExpression = "@exceptionChecker.${retryMethod}(#root)", retryFor = RuntimeException.class,
maxAttemptsExpression = "@integerFiveBean", backoff = @Backoff(delayExpression = "${one}",
maxDelayExpression = "@integerFiveBean", multiplierExpression = "${onePointOne}"))
public void service3() {
Expand Down

0 comments on commit 91c0203

Please sign in to comment.