Allow FixedBackOff to be constructed with only a custom interval

This commit introduces two new constructors:

- FixedBackOff(long)

- FixedBackOff(Duration)

Closes gh-35028
This commit is contained in:
Sam Brannen
2025-06-11 13:26:16 +02:00
parent fcdd439ad0
commit 8b9e620084
4 changed files with 42 additions and 13 deletions

View File

@@ -16,6 +16,7 @@
package org.springframework.core.retry;
import java.time.Duration;
import java.util.ArrayList;
import java.util.List;
@@ -60,7 +61,7 @@ public class RetryTemplate implements RetryOperations {
protected RetryPolicy retryPolicy = new MaxRetryAttemptsPolicy();
protected BackOff backOffPolicy = new FixedBackOff(1000, Long.MAX_VALUE);
protected BackOff backOffPolicy = new FixedBackOff(Duration.ofSeconds(1));
protected RetryListener retryListener = new RetryListener() {
};

View File

@@ -16,6 +16,8 @@
package org.springframework.util.backoff;
import java.time.Duration;
/**
* A simple {@link BackOff} implementation that provides a fixed interval
* between two attempts and a maximum number of retries.
@@ -50,6 +52,28 @@ public class FixedBackOff implements BackOff {
public FixedBackOff() {
}
/**
* Create an instance with the supplied interval and an unlimited number of
* attempts.
* @param interval the interval between two attempts in milliseconds
* @since 7.0
* @see #setMaxAttempts(long)
*/
public FixedBackOff(long interval) {
this.interval = interval;
}
/**
* Create an instance with the supplied interval and an unlimited number of
* attempts.
* @param interval the interval between two attempts
* @since 7.0
* @see #setMaxAttempts(long)
*/
public FixedBackOff(Duration interval) {
this.interval = interval.toMillis();
}
/**
* Create an instance with the supplied interval and maximum number of attempts.
* @param interval the interval between two attempts in milliseconds

View File

@@ -16,6 +16,8 @@
package org.springframework.core.retry;
import java.time.Duration;
import org.junit.jupiter.api.Test;
import org.springframework.core.retry.support.MaxRetryAttemptsPolicy;
@@ -55,7 +57,7 @@ class RetryTemplateTests {
}
};
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
assertThat(retryTemplate.execute(retryable)).isEqualTo("hello world");
}
@@ -76,7 +78,7 @@ class RetryTemplateTests {
}
};
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
assertThatExceptionOfType(RetryException.class)
.isThrownBy(() -> retryTemplate.execute(retryable))
@@ -123,7 +125,7 @@ class RetryTemplateTests {
}
};
retryTemplate.setRetryPolicy(retryPolicy);
retryTemplate.setBackOffPolicy(new FixedBackOff(100, Long.MAX_VALUE));
retryTemplate.setBackOffPolicy(new FixedBackOff(Duration.ofMillis(10)));
assertThatExceptionOfType(RetryException.class)
.isThrownBy(() -> retryTemplate.execute(retryable))