1515 */
1616package org .springframework .retry .support ;
1717
18+ import java .time .Duration ;
1819import java .util .ArrayList ;
1920import java .util .List ;
2021
8384 * @author Aleksandr Shamukov
8485 * @author Artem Bilan
8586 * @author Kim In Hoi
87+ * @author Andreas Ahlenstorf
8688 * @since 1.3
8789 */
8890public class RetryTemplateBuilder {
@@ -122,16 +124,43 @@ public RetryTemplateBuilder maxAttempts(int maxAttempts) {
122124 * @param timeout whole execution timeout in milliseconds
123125 * @return this
124126 * @see TimeoutRetryPolicy
127+ * @deprecated Use {@link #withTimeout(long)} instead.
125128 */
129+ @ Deprecated (since = "2.0.2" , forRemoval = true )
126130 public RetryTemplateBuilder withinMillis (long timeout ) {
127- Assert .isTrue (timeout > 0 , "Timeout should be positive" );
131+ return withTimeout (timeout );
132+ }
133+
134+ /**
135+ * Retry until {@code timeout} has passed since the initial attempt.
136+ * @param timeoutMillis timeout in milliseconds
137+ * @return this
138+ * @see TimeoutRetryPolicy
139+ * @throws IllegalArgumentException if timeout is {@literal <=} 0 or if another retry
140+ * policy has already been configured.
141+ * @since 2.0.2
142+ */
143+ public RetryTemplateBuilder withTimeout (long timeoutMillis ) {
144+ Assert .isTrue (timeoutMillis > 0 , "timeoutMillis should be greater than 0" );
128145 Assert .isNull (this .baseRetryPolicy , "You have already selected another retry policy" );
129- TimeoutRetryPolicy timeoutRetryPolicy = new TimeoutRetryPolicy ();
130- timeoutRetryPolicy .setTimeout (timeout );
131- this .baseRetryPolicy = timeoutRetryPolicy ;
146+ this .baseRetryPolicy = new TimeoutRetryPolicy (timeoutMillis );
132147 return this ;
133148 }
134149
150+ /**
151+ * Retry until {@code timeout} has passed since the initial attempt.
152+ * @param timeout duration for how long retries should be attempted
153+ * @return this
154+ * @see TimeoutRetryPolicy
155+ * @throws IllegalArgumentException if timeout is {@code null} or 0, or if another
156+ * retry policy has already been configured.
157+ * @since 2.0.2
158+ */
159+ public RetryTemplateBuilder withTimeout (Duration timeout ) {
160+ Assert .notNull (timeout , "timeout must not be null" );
161+ return withTimeout (timeout .toMillis ());
162+ }
163+
135164 /**
136165 * Allows infinite retry, do not limit attempts by number or time.
137166 * <p>
@@ -180,6 +209,27 @@ public RetryTemplateBuilder exponentialBackoff(long initialInterval, double mult
180209 return exponentialBackoff (initialInterval , multiplier , maxInterval , false );
181210 }
182211
212+ /**
213+ * Use exponential backoff policy. The formula of backoff period:
214+ * <p>
215+ * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
216+ * <p>
217+ * (for first attempt retryNum = 0)
218+ * @param initialInterval initial sleep duration
219+ * @param multiplier backoff interval multiplier
220+ * @param maxInterval maximum backoff duration
221+ * @return this
222+ * @see ExponentialBackOffPolicy
223+ * @throws IllegalArgumentException if initialInterval is {@code null}, multiplier is
224+ * {@literal <=} 1, or if maxInterval is {@code null}
225+ * @since 2.0.2
226+ */
227+ public RetryTemplateBuilder exponentialBackoff (Duration initialInterval , double multiplier , Duration maxInterval ) {
228+ Assert .notNull (initialInterval , "initialInterval must not be null" );
229+ Assert .notNull (maxInterval , "maxInterval must not be null" );
230+ return exponentialBackoff (initialInterval .toMillis (), multiplier , maxInterval .toMillis (), false );
231+ }
232+
183233 /**
184234 * Use exponential backoff policy. The formula of backoff period (without randomness):
185235 * <p>
@@ -210,6 +260,31 @@ public RetryTemplateBuilder exponentialBackoff(long initialInterval, double mult
210260 return this ;
211261 }
212262
263+ /**
264+ * Use exponential backoff policy. The formula of backoff period (without randomness):
265+ * <p>
266+ * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
267+ * <p>
268+ * (for first attempt retryNum = 0)
269+ * @param initialInterval initial sleep duration
270+ * @param multiplier backoff interval multiplier
271+ * @param maxInterval maximum backoff duration
272+ * @param withRandom adds some randomness to backoff intervals. For details, see
273+ * {@link ExponentialRandomBackOffPolicy}
274+ * @return this
275+ * @see ExponentialBackOffPolicy
276+ * @see ExponentialRandomBackOffPolicy
277+ * @throws IllegalArgumentException if initialInterval is {@code null}, multiplier is
278+ * {@literal <=} 1, or maxInterval is {@code null}
279+ * @since 2.0.2
280+ */
281+ public RetryTemplateBuilder exponentialBackoff (Duration initialInterval , double multiplier , Duration maxInterval ,
282+ boolean withRandom ) {
283+ Assert .notNull (initialInterval , "initialInterval most not be null" );
284+ Assert .notNull (maxInterval , "maxInterval must not be null" );
285+ return this .exponentialBackoff (initialInterval .toMillis (), multiplier , maxInterval .toMillis (), withRandom );
286+ }
287+
213288 /**
214289 * Perform each retry after fixed amount of time.
215290 * @param interval fixed interval in milliseconds
@@ -225,6 +300,24 @@ public RetryTemplateBuilder fixedBackoff(long interval) {
225300 return this ;
226301 }
227302
303+ /**
304+ * Perform each retry after fixed amount of time.
305+ * @param interval fixed backoff duration
306+ * @return this
307+ * @see FixedBackOffPolicy
308+ * @throws IllegalArgumentException if another backoff policy has already been
309+ * configured, interval is {@code null} or less than 1 millisecond
310+ * @since 2.0.2
311+ */
312+ public RetryTemplateBuilder fixedBackoff (Duration interval ) {
313+ Assert .notNull (interval , "interval must not be null" );
314+
315+ long millis = interval .toMillis ();
316+ Assert .isTrue (millis >= 1 , "interval is less than 1 millisecond" );
317+
318+ return this .fixedBackoff (millis );
319+ }
320+
228321 /**
229322 * Use {@link UniformRandomBackOffPolicy}, see it's doc for details.
230323 * @param minInterval in milliseconds
@@ -244,6 +337,23 @@ public RetryTemplateBuilder uniformRandomBackoff(long minInterval, long maxInter
244337 return this ;
245338 }
246339
340+ /**
341+ * Use {@link UniformRandomBackOffPolicy}.
342+ * @param minInterval minimum backoff duration
343+ * @param maxInterval maximum backoff duration
344+ * @return this
345+ * @see UniformRandomBackOffPolicy
346+ * @throws IllegalArgumentException if minInterval is {@code null} or {@literal <} 1,
347+ * maxInterval is {@code null} or {@literal <} 1, maxInterval {@literal >=}
348+ * minInterval or if another backoff policy has already been configured.
349+ * @since 2.0.2
350+ */
351+ public RetryTemplateBuilder uniformRandomBackoff (Duration minInterval , Duration maxInterval ) {
352+ Assert .notNull (minInterval , "minInterval must not be null" );
353+ Assert .notNull (maxInterval , "maxInterval must not be null" );
354+ return this .uniformRandomBackoff (minInterval .toMillis (), maxInterval .toMillis ());
355+ }
356+
247357 /**
248358 * Do not pause between attempts, retry immediately.
249359 * @return this
0 commit comments