Skip to content

Commit 8b9e620

Browse files
committed
Allow FixedBackOff to be constructed with only a custom interval
This commit introduces two new constructors: - FixedBackOff(long) - FixedBackOff(Duration) Closes gh-35028
1 parent fcdd439 commit 8b9e620

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package org.springframework.core.retry;
1818

19+
import java.time.Duration;
1920
import java.util.ArrayList;
2021
import java.util.List;
2122

@@ -60,7 +61,7 @@ public class RetryTemplate implements RetryOperations {
6061

6162
protected RetryPolicy retryPolicy = new MaxRetryAttemptsPolicy();
6263

63-
protected BackOff backOffPolicy = new FixedBackOff(1000, Long.MAX_VALUE);
64+
protected BackOff backOffPolicy = new FixedBackOff(Duration.ofSeconds(1));
6465

6566
protected RetryListener retryListener = new RetryListener() {
6667
};

spring-core/src/main/java/org/springframework/util/backoff/FixedBackOff.java

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.util.backoff;
1818

19+
import java.time.Duration;
20+
1921
/**
2022
* A simple {@link BackOff} implementation that provides a fixed interval
2123
* between two attempts and a maximum number of retries.
@@ -50,6 +52,28 @@ public class FixedBackOff implements BackOff {
5052
public FixedBackOff() {
5153
}
5254

55+
/**
56+
* Create an instance with the supplied interval and an unlimited number of
57+
* attempts.
58+
* @param interval the interval between two attempts in milliseconds
59+
* @since 7.0
60+
* @see #setMaxAttempts(long)
61+
*/
62+
public FixedBackOff(long interval) {
63+
this.interval = interval;
64+
}
65+
66+
/**
67+
* Create an instance with the supplied interval and an unlimited number of
68+
* attempts.
69+
* @param interval the interval between two attempts
70+
* @since 7.0
71+
* @see #setMaxAttempts(long)
72+
*/
73+
public FixedBackOff(Duration interval) {
74+
this.interval = interval.toMillis();
75+
}
76+
5377
/**
5478
* Create an instance with the supplied interval and maximum number of attempts.
5579
* @param interval the interval between two attempts in milliseconds

spring-core/src/test/java/org/springframework/core/retry/RetryTemplateTests.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@
1616

1717
package org.springframework.core.retry;
1818

19+
import java.time.Duration;
20+
1921
import org.junit.jupiter.api.Test;
2022

2123
import org.springframework.core.retry.support.MaxRetryAttemptsPolicy;
@@ -55,7 +57,7 @@ public String getName() {
5557
}
5658
};
5759

58-
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
60+
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
5961

6062
assertThat(retryTemplate.execute(retryable)).isEqualTo("hello world");
6163
}
@@ -76,7 +78,7 @@ public String getName() {
7678
}
7779
};
7880

79-
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
81+
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
8082

8183
assertThatExceptionOfType(RetryException.class)
8284
.isThrownBy(() -> retryTemplate.execute(retryable))
@@ -123,7 +125,7 @@ public boolean shouldRetry(Throwable throwable) {
123125
}
124126
};
125127
retryTemplate.setRetryPolicy(retryPolicy);
126-
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
128+
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
127129

128130
assertThatExceptionOfType(RetryException.class)
129131
.isThrownBy(() -> retryTemplate.execute(retryable))

spring-jms/src/main/java/org/springframework/jms/listener/DefaultMessageListenerContainer.java

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2024 the original author or authors.
2+
* Copyright 2002-2025 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -194,7 +194,7 @@ public class DefaultMessageListenerContainer extends AbstractPollingMessageListe
194194

195195
private boolean virtualThreads = false;
196196

197-
private BackOff backOff = new FixedBackOff(DEFAULT_RECOVERY_INTERVAL, Long.MAX_VALUE);
197+
private BackOff backOff = new FixedBackOff(DEFAULT_RECOVERY_INTERVAL);
198198

199199
private int cacheLevel = CACHE_AUTO;
200200

@@ -278,8 +278,8 @@ public void setVirtualThreads(boolean virtualThreads) {
278278
* between recovery attempts. If the {@link BackOffExecution} implementation
279279
* returns {@link BackOffExecution#STOP}, this listener container will not further
280280
* attempt to recover.
281-
* <p>The {@link #setRecoveryInterval(long) recovery interval} is ignored
282-
* when this property is set.
281+
* <p>Note that setting the {@linkplain #setRecoveryInterval(long) recovery
282+
* interval} overrides this property.
283283
* @since 4.1
284284
*/
285285
public void setBackOff(BackOff backOff) {
@@ -288,15 +288,17 @@ public void setBackOff(BackOff backOff) {
288288

289289
/**
290290
* Specify the interval between recovery attempts, in <b>milliseconds</b>.
291-
* The default is 5000 ms, that is, 5 seconds. This is a convenience method
292-
* to create a {@link FixedBackOff} with the specified interval.
293-
* <p>For more recovery options, consider specifying a {@link BackOff}
294-
* instance instead.
291+
* <p>The default is 5000 ms, that is, 5 seconds.
292+
* <p>This is a convenience method to create a {@link FixedBackOff} with the
293+
* specified interval. For more recovery options, consider specifying a
294+
* {@link #setBackOff(BackOff) BackOff} instance instead. Note, however, that
295+
* explicitly setting the {@link #setBackOff(BackOff) BackOff} overrides this
296+
* property.
295297
* @see #setBackOff(BackOff)
296298
* @see #handleListenerSetupFailure
297299
*/
298300
public void setRecoveryInterval(long recoveryInterval) {
299-
this.backOff = new FixedBackOff(recoveryInterval, Long.MAX_VALUE);
301+
this.backOff = new FixedBackOff(recoveryInterval);
300302
}
301303

302304
/**

0 commit comments

Comments
 (0)