From ea272f162c8927e032de67620edeb316c0ea72d8 Mon Sep 17 00:00:00 2001 From: Andreas Ahlenstorf Date: Tue, 23 May 2023 16:07:30 +0200 Subject: [PATCH] Add constructor make timeout customisable --- .../retry/policy/TimeoutRetryPolicy.java | 18 +++++++++++++++++- .../retry/policy/TimeoutRetryPolicyTests.java | 11 +++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/src/main/java/org/springframework/retry/policy/TimeoutRetryPolicy.java b/src/main/java/org/springframework/retry/policy/TimeoutRetryPolicy.java index a6a7b82d..19cacedb 100644 --- a/src/main/java/org/springframework/retry/policy/TimeoutRetryPolicy.java +++ b/src/main/java/org/springframework/retry/policy/TimeoutRetryPolicy.java @@ -35,7 +35,23 @@ public class TimeoutRetryPolicy implements RetryPolicy { */ public static final long DEFAULT_TIMEOUT = 1000; - private long timeout = DEFAULT_TIMEOUT; + private long timeout; + + /** + * Create a new instance with the timeout set to {@link #DEFAULT_TIMEOUT}. + */ + public TimeoutRetryPolicy() { + this(DEFAULT_TIMEOUT); + } + + /** + * Create a new instance with a configurable timeout. + * @param timeout timeout in milliseconds + * @since 2.0.2 + */ + public TimeoutRetryPolicy(long timeout) { + this.timeout = timeout; + } /** * Setter for timeout in milliseconds. Default is {@link #DEFAULT_TIMEOUT}. diff --git a/src/test/java/org/springframework/retry/policy/TimeoutRetryPolicyTests.java b/src/test/java/org/springframework/retry/policy/TimeoutRetryPolicyTests.java index 40e08f13..6f933e93 100644 --- a/src/test/java/org/springframework/retry/policy/TimeoutRetryPolicyTests.java +++ b/src/test/java/org/springframework/retry/policy/TimeoutRetryPolicyTests.java @@ -57,4 +57,15 @@ public void testParent() { assertThat(child.getParent()).isSameAs(context); } + @Test + public void testConstructorWithCustomTimeout() throws Exception { + TimeoutRetryPolicy policy = new TimeoutRetryPolicy(100); + RetryContext context = policy.open(null); + policy.registerThrowable(context, new Exception()); + assertThat(policy.canRetry(context)).isTrue(); + Thread.sleep(200); + assertThat(policy.canRetry(context)).isFalse(); + policy.close(context); + } + }