added advanced Spring Retry example and removed Spring Retry xml config

This commit is contained in:
michaelisvy 2020-09-14 08:16:56 +08:00
parent ed7ce4ce34
commit 4e081b4fb6
6 changed files with 40 additions and 54 deletions

View File

@ -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

View File

@ -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);

View File

@ -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");

View File

@ -0,0 +1,2 @@
retry.maxAttempts=2
retry.maxDelay=100

View File

@ -1,50 +0,0 @@
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd">
<aop:config>
<aop:pointcut id="transactional"
expression="execution(* com.baeldung.springretry..*MyService.defaultXmlRetryService(..))" />
<aop:advisor pointcut-ref="transactional" advice-ref="taskRetryAdvice" order="-1" />
</aop:config>
<bean id="taskRetryAdvice" class="org.springframework.retry.interceptor.RetryOperationsInterceptor">
<property name="RetryOperations" ref="taskBatchRetryTemplate" />
</bean>
<bean id="taskBatchRetryTemplate" class="org.springframework.retry.support.RetryTemplate">
<property name="retryPolicy" ref="taskBatchRetryPolicy" />
<property name="backOffPolicy" ref="ExponentialBackOffPolicy" />
</bean>
<bean id="taskBatchRetryPolicy" class="org.springframework.retry.policy.SimpleRetryPolicy">
<constructor-arg index="0" value="2" />
<constructor-arg index="1">
<map>
<entry key="java.lang.RuntimeException" value="true" />
</map>
</constructor-arg>
</bean>
<bean id="ExponentialBackOffPolicy" class="org.springframework.retry.backoff.ExponentialBackOffPolicy">
<property name="initialInterval" value="300">
<description>
Initial sleep interval value, default 300 ms
</description>
</property>
<property name="maxInterval" value="30000">
<description>
The maximum value of the backoff period in milliseconds.
</description>
</property>
<property name="multiplier" value="2.0">
<description>
The value to increment the exp seed with for each retry attempt.
</description>
</property>
</bean>
</beans>

View File

@ -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 -> {