Skip to content

Commit 489ebd2

Browse files
remeiosbrannen
authored andcommitted
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 <[email protected]>
1 parent 132836f commit 489ebd2

File tree

2 files changed

+10
-42
lines changed

2 files changed

+10
-42
lines changed

spring-context/src/main/java/org/springframework/resilience/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: 5 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222

2323
import org.jspecify.annotations.Nullable;
2424

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

2728
/**
@@ -55,26 +56,10 @@ class DefaultRetryPolicy implements RetryPolicy {
5556

5657
@Override
5758
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);
59+
ExceptionTypeFilter exceptionTypeFilter = new ExceptionTypeFilter(this.includes,
60+
this.excludes, true);
61+
return exceptionTypeFilter.match(throwable.getClass()) &&
62+
(this.predicate == null || this.predicate.test(throwable));
7863
}
7964

8065
@Override

0 commit comments

Comments
 (0)