Skip to content

Filter throwable by ExceptionTypeFilter #35109

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
import java.util.Collection;
import java.util.Collections;

import org.springframework.util.ExceptionTypeFilter;

/**
* A specification for retry attempts on a given method, combining common
* retry characteristics. This roughly matches the annotation attributes
Expand Down Expand Up @@ -61,28 +63,9 @@ public MethodRetrySpec(MethodRetryPredicate predicate, int maxAttempts, long del


MethodRetryPredicate combinedPredicate() {
return (method, throwable) -> {
if (!this.excludes.isEmpty()) {
for (Class<? extends Throwable> exclude : this.excludes) {
if (exclude.isInstance(throwable)) {
return false;
}
}
}
if (!this.includes.isEmpty()) {
boolean included = false;
for (Class<? extends Throwable> include : this.includes) {
if (include.isInstance(throwable)) {
included = true;
break;
}
}
if (!included) {
return false;
}
}
return this.predicate.shouldRetry(method, throwable);
};
return (method, throwable) -> new ExceptionTypeFilter(this.includes, this.excludes, true)
.match(throwable.getClass()) &&
this.predicate.shouldRetry(method, throwable);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,14 @@
import org.jspecify.annotations.Nullable;

import org.springframework.util.Assert;
import org.springframework.util.ExceptionTypeFilter;

/**
* Default {@link RetryPolicy} created by {@link RetryPolicy.Builder}.
*
* @author Sam Brannen
* @author Mahmoud Ben Hassine
* @author Mengqi Xu
* @since 7.0
*/
class DefaultRetryPolicy implements RetryPolicy {
Expand Down Expand Up @@ -105,6 +107,8 @@ private class DefaultRetryPolicyExecution implements RetryExecution {

private int retryCount;

private final ExceptionTypeFilter exceptionTypeFilter = new ExceptionTypeFilter(DefaultRetryPolicy.this.includes,
DefaultRetryPolicy.this.excludes, true);

@Override
public boolean shouldRetry(Throwable throwable) {
Expand All @@ -118,26 +122,8 @@ public boolean shouldRetry(Throwable throwable) {
return false;
}
}
if (!DefaultRetryPolicy.this.excludes.isEmpty()) {
for (Class<? extends Throwable> excludedType : DefaultRetryPolicy.this.excludes) {
if (excludedType.isInstance(throwable)) {
return false;
}
}
}
if (!DefaultRetryPolicy.this.includes.isEmpty()) {
boolean included = false;
for (Class<? extends Throwable> includedType : DefaultRetryPolicy.this.includes) {
if (includedType.isInstance(throwable)) {
included = true;
break;
}
}
if (!included) {
return false;
}
}
return DefaultRetryPolicy.this.predicate == null || DefaultRetryPolicy.this.predicate.test(throwable);
return this.exceptionTypeFilter.match(throwable.getClass()) &&
(DefaultRetryPolicy.this.predicate == null || DefaultRetryPolicy.this.predicate.test(throwable));
}

@Override
Expand Down