From 4e081b4fb65a03ccfa100f8f329e2e202e7b1b78 Mon Sep 17 00:00:00 2001 From: michaelisvy Date: Mon, 14 Sep 2020 08:16:56 +0800 Subject: [PATCH] added advanced Spring Retry example and removed Spring Retry xml config --- .../com/baeldung/springretry/AppConfig.java | 5 +- .../com/baeldung/springretry/MyService.java | 11 +++- .../baeldung/springretry/MyServiceImpl.java | 16 ++++++ .../src/main/resources/retryConfig.properties | 2 + .../src/main/resources/retryadvice.xml | 50 ------------------- .../SpringRetryIntegrationTest.java | 10 ++++ 6 files changed, 40 insertions(+), 54 deletions(-) create mode 100644 spring-scheduling/src/main/resources/retryConfig.properties delete mode 100644 spring-scheduling/src/main/resources/retryadvice.xml 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 e79beb370b..2ca9104e89 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/AppConfig.java @@ -3,6 +3,7 @@ package com.baeldung.springretry; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.ComponentScan; import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.PropertySource; import org.springframework.retry.annotation.EnableRetry; import org.springframework.retry.backoff.FixedBackOffPolicy; import org.springframework.retry.policy.SimpleRetryPolicy; @@ -11,9 +12,7 @@ import org.springframework.retry.support.RetryTemplate; @Configuration @ComponentScan(basePackages = "com.baeldung.springretry") @EnableRetry -// Uncomment this two lines if we need XML configuration -// @EnableAspectJAutoProxy -// @ImportResource("classpath:/retryadvice.xml") +@PropertySource("classpath:retryConfig.properties") public class AppConfig { @Bean diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java index 409bf25845..40e2c419fc 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyService.java @@ -2,18 +2,27 @@ package com.baeldung.springretry; import java.sql.SQLException; +import org.springframework.context.annotation.PropertySource; import org.springframework.retry.annotation.Backoff; import org.springframework.retry.annotation.Recover; import org.springframework.retry.annotation.Retryable; + public interface MyService { @Retryable void retryService(); - @Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 5000)) + @Retryable(value = SQLException.class) void retryServiceWithRecovery(String sql) throws SQLException; + @Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 100)) + void retryServiceWithCustomization(String sql) throws SQLException; + + @Retryable( value = SQLException.class, maxAttemptsExpression = "${retry.maxAttempts}", + backoff = @Backoff(delayExpression = "${retry.maxDelay}")) + void retryServiceWithExternalConfiguration(String sql) throws SQLException; + @Recover void recover(SQLException e, String sql); diff --git a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java index 3e4b5ed00d..7eb4328a47 100644 --- a/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java +++ b/spring-scheduling/src/main/java/com/baeldung/springretry/MyServiceImpl.java @@ -26,6 +26,22 @@ public class MyServiceImpl implements MyService { } } + @Override + public void retryServiceWithCustomization(String sql) throws SQLException { + if (StringUtils.isEmpty(sql)) { + logger.info("throw SQLException in method retryServiceWithCustomization()"); + throw new SQLException(); + } + } + + @Override + public void retryServiceWithExternalConfiguration(String sql) throws SQLException { + if (StringUtils.isEmpty(sql)) { + logger.info("throw SQLException in method retryServiceWithExternalConfiguration()"); + throw new SQLException(); + } + } + @Override public void recover(SQLException e, String sql) { logger.info("In recover method"); diff --git a/spring-scheduling/src/main/resources/retryConfig.properties b/spring-scheduling/src/main/resources/retryConfig.properties new file mode 100644 index 0000000000..7cc360adc6 --- /dev/null +++ b/spring-scheduling/src/main/resources/retryConfig.properties @@ -0,0 +1,2 @@ +retry.maxAttempts=2 +retry.maxDelay=100 \ No newline at end of file diff --git a/spring-scheduling/src/main/resources/retryadvice.xml b/spring-scheduling/src/main/resources/retryadvice.xml deleted file mode 100644 index 8de7801a58..0000000000 --- a/spring-scheduling/src/main/resources/retryadvice.xml +++ /dev/null @@ -1,50 +0,0 @@ - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initial sleep interval value, default 300 ms - - - - - The maximum value of the backoff period in milliseconds. - - - - - The value to increment the exp seed with for each retry attempt. - - - - - \ No newline at end of file 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 2e5fb75482..33ce2fff74 100644 --- a/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java +++ b/spring-scheduling/src/test/java/com/baeldung/springretry/SpringRetryIntegrationTest.java @@ -30,6 +30,16 @@ public class SpringRetryIntegrationTest { myService.retryServiceWithRecovery(null); } + @Test + public void givenRetryServiceWithCustomization_whenCallWithException_thenRetryRecover() throws SQLException { + myService.retryServiceWithCustomization(null); + } + + @Test + public void givenRetryServiceWithExternalConfiguration_whenCallWithException_thenRetryRecover() throws SQLException { + myService.retryServiceWithExternalConfiguration(null); + } + @Test(expected = RuntimeException.class) public void givenTemplateRetryService_whenCallWithException_thenRetry() { retryTemplate.execute(arg0 -> {