mirror of
https://github.com/spring-projects/spring-framework.git
synced 2026-10-03 13:39:34 +00:00
Use ExceptionTypeFilter to filter includes & excludes for retry policies
This commit reduces code duplication by reusing the logic already available in ExceptionTypeFilter. Closes gh-35109 Signed-off-by: Mengqi Xu <2663479778@qq.com>
This commit is contained in:
+5
-22
@@ -20,6 +20,8 @@ import java.time.Duration;
|
||||
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
|
||||
@@ -62,28 +64,9 @@ public record MethodRetrySpec(
|
||||
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -22,6 +22,7 @@ import java.util.function.Predicate;
|
||||
|
||||
import org.jspecify.annotations.Nullable;
|
||||
|
||||
import org.springframework.util.ExceptionTypeFilter;
|
||||
import org.springframework.util.backoff.BackOff;
|
||||
|
||||
/**
|
||||
@@ -55,26 +56,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
|
||||
|
||||
Reference in New Issue
Block a user