Skip to content

Commit c9a0993

Browse files
committed
Filter throwable by ExceptionTypeFilter.
Signed-off-by: Mengqi Xu <[email protected]>
1 parent ed86daa commit c9a0993

File tree

2 files changed

+11
-42
lines changed

2 files changed

+11
-42
lines changed

spring-aop/src/main/java/org/springframework/aop/retry/MethodRetrySpec.java

Lines changed: 5 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
import java.util.Collection;
2121
import java.util.Collections;
2222

23+
import org.springframework.util.ExceptionTypeFilter;
24+
2325
/**
2426
* A specification for retry attempts on a given method, combining common
2527
* retry characteristics. This roughly matches the annotation attributes
@@ -62,28 +64,9 @@ public MethodRetrySpec(MethodRetryPredicate predicate, long maxAttempts, Duratio
6264

6365

6466
MethodRetryPredicate combinedPredicate() {
65-
return (method, throwable) -> {
66-
if (!this.excludes.isEmpty()) {
67-
for (Class<? extends Throwable> exclude : this.excludes) {
68-
if (exclude.isInstance(throwable)) {
69-
return false;
70-
}
71-
}
72-
}
73-
if (!this.includes.isEmpty()) {
74-
boolean included = false;
75-
for (Class<? extends Throwable> include : this.includes) {
76-
if (include.isInstance(throwable)) {
77-
included = true;
78-
break;
79-
}
80-
}
81-
if (!included) {
82-
return false;
83-
}
84-
}
85-
return this.predicate.shouldRetry(method, throwable);
86-
};
67+
return (method, throwable) -> new ExceptionTypeFilter(this.includes, this.excludes, true)
68+
.match(throwable.getClass()) &&
69+
this.predicate.shouldRetry(method, throwable);
8770
}
8871

8972
}

spring-core/src/main/java/org/springframework/core/retry/DefaultRetryPolicy.java

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@
2222

2323
import org.jspecify.annotations.Nullable;
2424

25+
import org.springframework.util.ExceptionTypeFilter;
2526
import org.springframework.util.backoff.BackOff;
2627

2728
/**
2829
* Default {@link RetryPolicy} created by {@link RetryPolicy.Builder}.
2930
*
3031
* @author Sam Brannen
3132
* @author Mahmoud Ben Hassine
33+
* @author Mengqi Xu
3234
* @since 7.0
3335
*/
3436
class DefaultRetryPolicy implements RetryPolicy {
@@ -55,26 +57,10 @@ class DefaultRetryPolicy implements RetryPolicy {
5557

5658
@Override
5759
public boolean shouldRetry(Throwable throwable) {
58-
if (!this.excludes.isEmpty()) {
59-
for (Class<? extends Throwable> excludedType : this.excludes) {
60-
if (excludedType.isInstance(throwable)) {
61-
return false;
62-
}
63-
}
64-
}
65-
if (!this.includes.isEmpty()) {
66-
boolean included = false;
67-
for (Class<? extends Throwable> includedType : this.includes) {
68-
if (includedType.isInstance(throwable)) {
69-
included = true;
70-
break;
71-
}
72-
}
73-
if (!included) {
74-
return false;
75-
}
76-
}
77-
return this.predicate == null || this.predicate.test(throwable);
60+
ExceptionTypeFilter exceptionTypeFilter = new ExceptionTypeFilter(this.includes,
61+
this.excludes, true);
62+
return exceptionTypeFilter.match(throwable.getClass()) &&
63+
(this.predicate == null || this.predicate.test(throwable));
7864
}
7965

8066
@Override

0 commit comments

Comments
 (0)