diff --git a/spring-aop/src/main/java/org/springframework/aop/retry/MethodRetrySpec.java b/spring-aop/src/main/java/org/springframework/aop/retry/MethodRetrySpec.java index 919648fc5cc3..eabe91505429 100644 --- a/spring-aop/src/main/java/org/springframework/aop/retry/MethodRetrySpec.java +++ b/spring-aop/src/main/java/org/springframework/aop/retry/MethodRetrySpec.java @@ -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 @@ -61,28 +63,9 @@ public MethodRetrySpec(MethodRetryPredicate predicate, int maxAttempts, long del MethodRetryPredicate combinedPredicate() { - return (method, throwable) -> { - if (!this.excludes.isEmpty()) { - for (Class exclude : this.excludes) { - if (exclude.isInstance(throwable)) { - return false; - } - } - } - if (!this.includes.isEmpty()) { - boolean included = false; - for (Class 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); } } diff --git a/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java b/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java index eb599cba0021..890a4291c245 100644 --- a/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java +++ b/spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java @@ -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 { @@ -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) { @@ -118,26 +122,8 @@ public boolean shouldRetry(Throwable throwable) { return false; } } - if (!DefaultRetryPolicy.this.excludes.isEmpty()) { - for (Class excludedType : DefaultRetryPolicy.this.excludes) { - if (excludedType.isInstance(throwable)) { - return false; - } - } - } - if (!DefaultRetryPolicy.this.includes.isEmpty()) { - boolean included = false; - for (Class 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