Merge pull request #10026 from michaelisvy/spring-retry-BAEL-470
added advanced Spring Retry example and removed Spring Retry xml config
This commit is contained in:
commit
89f0f75667
|
@ -3,6 +3,7 @@ package com.baeldung.springretry;
|
||||||
import org.springframework.context.annotation.Bean;
|
import org.springframework.context.annotation.Bean;
|
||||||
import org.springframework.context.annotation.ComponentScan;
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
import org.springframework.context.annotation.Configuration;
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.retry.annotation.EnableRetry;
|
import org.springframework.retry.annotation.EnableRetry;
|
||||||
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
||||||
import org.springframework.retry.policy.SimpleRetryPolicy;
|
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||||
|
@ -11,9 +12,7 @@ import org.springframework.retry.support.RetryTemplate;
|
||||||
@Configuration
|
@Configuration
|
||||||
@ComponentScan(basePackages = "com.baeldung.springretry")
|
@ComponentScan(basePackages = "com.baeldung.springretry")
|
||||||
@EnableRetry
|
@EnableRetry
|
||||||
// Uncomment this two lines if we need XML configuration
|
@PropertySource("classpath:retryConfig.properties")
|
||||||
// @EnableAspectJAutoProxy
|
|
||||||
// @ImportResource("classpath:/retryadvice.xml")
|
|
||||||
public class AppConfig {
|
public class AppConfig {
|
||||||
|
|
||||||
@Bean
|
@Bean
|
||||||
|
|
|
@ -2,18 +2,27 @@ package com.baeldung.springretry;
|
||||||
|
|
||||||
import java.sql.SQLException;
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.PropertySource;
|
||||||
import org.springframework.retry.annotation.Backoff;
|
import org.springframework.retry.annotation.Backoff;
|
||||||
import org.springframework.retry.annotation.Recover;
|
import org.springframework.retry.annotation.Recover;
|
||||||
import org.springframework.retry.annotation.Retryable;
|
import org.springframework.retry.annotation.Retryable;
|
||||||
|
|
||||||
|
|
||||||
public interface MyService {
|
public interface MyService {
|
||||||
|
|
||||||
@Retryable
|
@Retryable
|
||||||
void retryService();
|
void retryService();
|
||||||
|
|
||||||
@Retryable(value = { SQLException.class }, maxAttempts = 2, backoff = @Backoff(delay = 5000))
|
@Retryable(value = SQLException.class)
|
||||||
void retryServiceWithRecovery(String sql) throws SQLException;
|
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
|
@Recover
|
||||||
void recover(SQLException e, String sql);
|
void recover(SQLException e, String sql);
|
||||||
|
|
||||||
|
|
|
@ -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
|
@Override
|
||||||
public void recover(SQLException e, String sql) {
|
public void recover(SQLException e, String sql) {
|
||||||
logger.info("In recover method");
|
logger.info("In recover method");
|
||||||
|
|
|
@ -0,0 +1,2 @@
|
||||||
|
retry.maxAttempts=2
|
||||||
|
retry.maxDelay=100
|
|
@ -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>
|
|
|
@ -30,6 +30,16 @@ public class SpringRetryIntegrationTest {
|
||||||
myService.retryServiceWithRecovery(null);
|
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)
|
@Test(expected = RuntimeException.class)
|
||||||
public void givenTemplateRetryService_whenCallWithException_thenRetry() {
|
public void givenTemplateRetryService_whenCallWithException_thenRetry() {
|
||||||
retryTemplate.execute(arg0 -> {
|
retryTemplate.execute(arg0 -> {
|
||||||
|
|
Loading…
Reference in New Issue