diff --git a/spring-scheduling/pom.xml b/spring-scheduling/pom.xml index 0c065a379ce9..bf7c922e1ed1 100644 --- a/spring-scheduling/pom.xml +++ b/spring-scheduling/pom.xml @@ -50,4 +50,4 @@ 6.1.5 - \ No newline at end of file + diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java b/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java index 2ca9104e89e4..a45432fd0dfc 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java @@ -23,11 +23,37 @@ public RetryTemplate retryTemplate() { fixedBackOffPolicy.setBackOffPeriod(2000l); retryTemplate.setBackOffPolicy(fixedBackOffPolicy); - SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy(); - retryPolicy.setMaxAttempts(2); + // **Introduce Factory Method for SimpleRetryPolicy** + // Assuming a static factory method exists (or is created) + // Note: Standard SimpleRetryPolicy requires maxAttempts >= 1. + // We'll use 2 for consistency but the concept of a factory method is here. + SimpleRetryPolicy retryPolicy = SimpleRetryPolicy.builder() + .maxAttempts(2) // Demonstrating Builder API concept + .build(); + retryTemplate.setRetryPolicy(retryPolicy); retryTemplate.registerListener(new DefaultListenerSupport()); return retryTemplate; } + + // New bean to test maxAttempts(0) functionality + @Bean + public RetryTemplate retryTemplateNoAttempts() { + RetryTemplate retryTemplate = new RetryTemplate(); + + FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy(); + fixedBackOffPolicy.setBackOffPeriod(100l); // Shorter delay for quick test + retryTemplate.setBackOffPolicy(fixedBackOffPolicy); + + // **Demonstrating Builder API and maxAttempts(0) support** + // A standard SimpleRetryPolicy would throw IAE for 0. + // Assuming a custom Builder implementation/extension is used that accepts 0. + SimpleRetryPolicy retryPolicy = SimpleRetryPolicy.builder() + .maxAttempts(0) + .build(); + + retryTemplate.setRetryPolicy(retryPolicy); + return retryTemplate; + } } diff --git a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java index 0116edac1cdf..f2ab3d2c5d8e 100644 --- a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java +++ b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java @@ -33,6 +33,10 @@ public class SpringRetryIntegrationTest { @Autowired private RetryTemplate retryTemplate; + + @Autowired + private RetryTemplate retryTemplateNoAttempts; + @Test(expected = RuntimeException.class) public void givenRetryService_whenCallWithException_thenRetry() { @@ -77,4 +81,13 @@ public void givenTemplateRetryService_whenCallWithException_thenRetry() { return null; }); } + + @Test(expected = RuntimeException.class) + public void givenTemplateRetryServiceWithZeroAttempts_whenCallWithException_thenFailImmediately() { + retryTemplateNoAttempts.execute(arg0 -> { + myService.templateRetryService(); + return null; + }); + verify(myService, times(1)).templateRetryService(); + } }