1717package org .springframework .boot .retry ;
1818
1919import java .time .Duration ;
20+ import java .util .ArrayList ;
21+ import java .util .List ;
2022import java .util .function .Function ;
23+ import java .util .function .Predicate ;
2124
2225import org .jspecify .annotations .Nullable ;
2326
@@ -53,6 +56,12 @@ public final class RetryPolicySettings {
5356 */
5457 public static final Duration DEFAULT_MAX_DELAY = Duration .ofMillis (RetryPolicy .Builder .DEFAULT_MAX_DELAY );
5558
59+ private List <Class <? extends Throwable >> exceptionIncludes = new ArrayList <>();
60+
61+ private List <Class <? extends Throwable >> exceptionExcludes = new ArrayList <>();
62+
63+ private @ Nullable Predicate <Throwable > exceptionPredicate ;
64+
5665 private Long maxAttempts = DEFAULT_MAX_ATTEMPTS ;
5766
5867 private Duration delay = DEFAULT_DELAY ;
@@ -72,6 +81,9 @@ public final class RetryPolicySettings {
7281 public RetryPolicy createRetryPolicy () {
7382 PropertyMapper map = PropertyMapper .get ();
7483 RetryPolicy .Builder builder = RetryPolicy .builder ();
84+ map .from (this ::getExceptionIncludes ).to (builder ::includes );
85+ map .from (this ::getExceptionExcludes ).to (builder ::excludes );
86+ map .from (this ::getExceptionPredicate ).to (builder ::predicate );
7587 map .from (this ::getMaxAttempts ).to (builder ::maxAttempts );
7688 map .from (this ::getDelay ).to (builder ::delay );
7789 map .from (this ::getJitter ).to (builder ::jitter );
@@ -80,6 +92,64 @@ public RetryPolicy createRetryPolicy() {
8092 return (this .factory != null ) ? this .factory .apply (builder ) : builder .build ();
8193 }
8294
95+ /**
96+ * Return the applicable exception types to attempt a retry for.
97+ * <p>
98+ * The default is empty, leading to a retry attempt for any exception.
99+ * @return the applicable exception types
100+ */
101+ public List <Class <? extends Throwable >> getExceptionIncludes () {
102+ return this .exceptionIncludes ;
103+ }
104+
105+ /**
106+ * Replace the applicable exception types to attempt a retry for by the given
107+ * {@code includes}. Alternatively consider using {@link #getExceptionIncludes()} to
108+ * mutate the existing list.
109+ * @param includes the applicable exception types
110+ */
111+ public void setExceptionIncludes (List <Class <? extends Throwable >> includes ) {
112+ this .exceptionIncludes = new ArrayList <>(includes );
113+ }
114+
115+ /**
116+ * Return the non-applicable exception types to avoid a retry for.
117+ * <p>
118+ * The default is empty, leading to a retry attempt for any exception.
119+ * @return the non-applicable exception types
120+ */
121+ public List <Class <? extends Throwable >> getExceptionExcludes () {
122+ return this .exceptionExcludes ;
123+ }
124+
125+ /**
126+ * Replace the non-applicable exception types to attempt a retry for by the given
127+ * {@code excludes}. Alternatively consider using {@link #getExceptionExcludes()} to
128+ * mutate the existing list.
129+ * @param excludes the non-applicable types
130+ */
131+ public void setExceptionExcludes (List <Class <? extends Throwable >> excludes ) {
132+ this .exceptionExcludes = new ArrayList <>(excludes );
133+ }
134+
135+ /**
136+ * Return the predicate to use to determine whether to retry a failed operation based
137+ * on a given {@link Throwable}.
138+ * @return the predicate to use
139+ */
140+ public @ Nullable Predicate <Throwable > getExceptionPredicate () {
141+ return this .exceptionPredicate ;
142+ }
143+
144+ /**
145+ * Set the predicate to use to determine whether to retry a failed operation based on
146+ * a given {@link Throwable}.
147+ * @param exceptionPredicate the predicate to use
148+ */
149+ public void setExceptionPredicate (@ Nullable Predicate <Throwable > exceptionPredicate ) {
150+ this .exceptionPredicate = exceptionPredicate ;
151+ }
152+
83153 /**
84154 * Return the maximum number of retry attempts.
85155 * @return the maximum number of retry attempts
0 commit comments