15
15
*/
16
16
package org .springframework .retry .support ;
17
17
18
+ import java .time .Duration ;
18
19
import java .util .ArrayList ;
19
20
import java .util .List ;
20
21
83
84
* @author Aleksandr Shamukov
84
85
* @author Artem Bilan
85
86
* @author Kim In Hoi
87
+ * @author Andreas Ahlenstorf
86
88
* @since 1.3
87
89
*/
88
90
public class RetryTemplateBuilder {
@@ -122,7 +124,9 @@ public RetryTemplateBuilder maxAttempts(int maxAttempts) {
122
124
* @param timeout whole execution timeout in milliseconds
123
125
* @return this
124
126
* @see TimeoutRetryPolicy
127
+ * @deprecated Use {@link #withTimeout(long)} instead.
125
128
*/
129
+ @ Deprecated (since = "2.0.2" , forRemoval = true )
126
130
public RetryTemplateBuilder withinMillis (long timeout ) {
127
131
Assert .isTrue (timeout > 0 , "Timeout should be positive" );
128
132
Assert .isNull (this .baseRetryPolicy , "You have already selected another retry policy" );
@@ -132,6 +136,36 @@ public RetryTemplateBuilder withinMillis(long timeout) {
132
136
return this ;
133
137
}
134
138
139
+ /**
140
+ * Retry until {@code timeout} has passed since the initial attempt.
141
+ * @param timeoutMillis timeout in milliseconds
142
+ * @return this
143
+ * @see TimeoutRetryPolicy
144
+ * @throws IllegalArgumentException if timeout is {@literal <=} 0 or if another retry
145
+ * policy has already been configured.
146
+ * @since 2.0.2
147
+ */
148
+ public RetryTemplateBuilder withTimeout (long timeoutMillis ) {
149
+ Assert .isTrue (timeoutMillis > 0 , "timeoutMillis should be greater than 0" );
150
+ Assert .isNull (this .baseRetryPolicy , "You have already selected another retry policy" );
151
+ this .baseRetryPolicy = new TimeoutRetryPolicy (timeoutMillis );
152
+ return this ;
153
+ }
154
+
155
+ /**
156
+ * Retry until {@code timeout} has passed since the initial attempt.
157
+ * @param timeout duration for how long retries should be attempted
158
+ * @return this
159
+ * @see TimeoutRetryPolicy
160
+ * @throws IllegalArgumentException if timeout is {@code null} or 0, or if another
161
+ * retry policy has already been configured.
162
+ * @since 2.0.2
163
+ */
164
+ public RetryTemplateBuilder withTimeout (Duration timeout ) {
165
+ Assert .notNull (timeout , "timeout is null" );
166
+ return this .withinMillis (timeout .toMillis ());
167
+ }
168
+
135
169
/**
136
170
* Allows infinite retry, do not limit attempts by number or time.
137
171
* <p>
@@ -180,6 +214,27 @@ public RetryTemplateBuilder exponentialBackoff(long initialInterval, double mult
180
214
return exponentialBackoff (initialInterval , multiplier , maxInterval , false );
181
215
}
182
216
217
+ /**
218
+ * Use exponential backoff policy. The formula of backoff period:
219
+ * <p>
220
+ * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
221
+ * <p>
222
+ * (for first attempt retryNum = 0)
223
+ * @param initialInterval initial sleep duration
224
+ * @param multiplier backoff interval multiplier
225
+ * @param maxInterval maximum backoff duration
226
+ * @return this
227
+ * @see ExponentialBackOffPolicy
228
+ * @throws IllegalArgumentException if initialInterval is {@code null}, multiplier is
229
+ * {@literal <=} 1, or if maxInterval is {@code null}
230
+ * @since 2.0.2
231
+ */
232
+ public RetryTemplateBuilder exponentialBackoff (Duration initialInterval , double multiplier , Duration maxInterval ) {
233
+ Assert .notNull (initialInterval , "initialInterval is null" );
234
+ Assert .notNull (maxInterval , "maxInterval is null" );
235
+ return exponentialBackoff (initialInterval .toMillis (), multiplier , maxInterval .toMillis (), false );
236
+ }
237
+
183
238
/**
184
239
* Use exponential backoff policy. The formula of backoff period (without randomness):
185
240
* <p>
@@ -210,6 +265,31 @@ public RetryTemplateBuilder exponentialBackoff(long initialInterval, double mult
210
265
return this ;
211
266
}
212
267
268
+ /**
269
+ * Use exponential backoff policy. The formula of backoff period (without randomness):
270
+ * <p>
271
+ * {@code currentInterval = Math.min(initialInterval * Math.pow(multiplier, retryNum), maxInterval)}
272
+ * <p>
273
+ * (for first attempt retryNum = 0)
274
+ * @param initialInterval initial sleep duration
275
+ * @param multiplier backoff interval multiplier
276
+ * @param maxInterval maximum backoff duration
277
+ * @param withRandom adds some randomness to backoff intervals. For details, see
278
+ * {@link ExponentialRandomBackOffPolicy}
279
+ * @return this
280
+ * @see ExponentialBackOffPolicy
281
+ * @see ExponentialRandomBackOffPolicy
282
+ * @throws IllegalArgumentException if initialInterval is {@code null}, multiplier is
283
+ * {@literal <=} 1, or maxInterval is {@code null}
284
+ * @since 2.0.2
285
+ */
286
+ public RetryTemplateBuilder exponentialBackoff (Duration initialInterval , double multiplier , Duration maxInterval ,
287
+ boolean withRandom ) {
288
+ Assert .notNull (initialInterval , "initialInterval is null" );
289
+ Assert .notNull (maxInterval , "maxInterval is null" );
290
+ return this .exponentialBackoff (initialInterval .toMillis (), multiplier , maxInterval .toMillis (), withRandom );
291
+ }
292
+
213
293
/**
214
294
* Perform each retry after fixed amount of time.
215
295
* @param interval fixed interval in milliseconds
@@ -225,6 +305,24 @@ public RetryTemplateBuilder fixedBackoff(long interval) {
225
305
return this ;
226
306
}
227
307
308
+ /**
309
+ * Perform each retry after fixed amount of time.
310
+ * @param interval fixed backoff duration
311
+ * @return this
312
+ * @see FixedBackOffPolicy
313
+ * @throws IllegalArgumentException if another backoff policy has already been
314
+ * configured, interval is {@code null} or less than 1 millisecond
315
+ * @since 2.0.2
316
+ */
317
+ public RetryTemplateBuilder fixedBackoff (Duration interval ) {
318
+ Assert .notNull (interval , "interval is null" );
319
+
320
+ long millis = interval .toMillis ();
321
+ Assert .isTrue (millis >= 1 , "interval is less than 1 millisecond" );
322
+
323
+ return this .fixedBackoff (millis );
324
+ }
325
+
228
326
/**
229
327
* Use {@link UniformRandomBackOffPolicy}, see it's doc for details.
230
328
* @param minInterval in milliseconds
@@ -244,6 +342,23 @@ public RetryTemplateBuilder uniformRandomBackoff(long minInterval, long maxInter
244
342
return this ;
245
343
}
246
344
345
+ /**
346
+ * Use {@link UniformRandomBackOffPolicy}.
347
+ * @param minInterval minimum backoff duration
348
+ * @param maxInterval maximum backoff duration
349
+ * @return this
350
+ * @see UniformRandomBackOffPolicy
351
+ * @throws IllegalArgumentException if minInterval is {@code null} or {@literal <} 1,
352
+ * maxInterval is {@code null} or {@literal <} 1, maxInterval {@literal >=}
353
+ * minInterval or if another backoff policy has already been configured.
354
+ * @since 2.0.2
355
+ */
356
+ public RetryTemplateBuilder uniformRandomBackoff (Duration minInterval , Duration maxInterval ) {
357
+ Assert .notNull (minInterval , "minInterval is null" );
358
+ Assert .notNull (maxInterval , "maxInterval is null" );
359
+ return this .uniformRandomBackoff (minInterval .toMillis (), maxInterval .toMillis ());
360
+ }
361
+
247
362
/**
248
363
* Do not pause between attempts, retry immediately.
249
364
* @return this
0 commit comments