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 1 commit 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 @@ -20,6 +20,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 @@ -62,28 +64,9 @@ public MethodRetrySpec(MethodRetryPredicate predicate, long maxAttempts, Duratio


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 @@ -22,13 +22,15 @@

import org.jspecify.annotations.Nullable;

import org.springframework.util.ExceptionTypeFilter;
import org.springframework.util.backoff.BackOff;

/**
* 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 All @@ -55,26 +57,10 @@ class DefaultRetryPolicy implements RetryPolicy {

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

@Override
Expand Down