BAEL-470-Spring Retry (#967)
* add Spring Retry * remove redundant code * convert tab to space in XML file * align spaces in xml file
This commit is contained in:
parent
e796bdb1c5
commit
8734c956bb
@ -38,6 +38,11 @@
|
|||||||
<groupId>org.springframework</groupId>
|
<groupId>org.springframework</groupId>
|
||||||
<artifactId>spring-context</artifactId>
|
<artifactId>spring-context</artifactId>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.retry</groupId>
|
||||||
|
<artifactId>spring-retry</artifactId>
|
||||||
|
<version>${springretry.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
<!-- aspectj -->
|
<!-- aspectj -->
|
||||||
|
|
||||||
@ -166,7 +171,6 @@
|
|||||||
<artifactId>ehcache</artifactId>
|
<artifactId>ehcache</artifactId>
|
||||||
<version>${ehcache.version}</version>
|
<version>${ehcache.version}</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
</dependencies>
|
</dependencies>
|
||||||
|
|
||||||
<dependencyManagement>
|
<dependencyManagement>
|
||||||
@ -275,6 +279,7 @@
|
|||||||
<!-- Spring -->
|
<!-- Spring -->
|
||||||
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
<org.springframework.version>4.3.4.RELEASE</org.springframework.version>
|
||||||
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
<org.springframework.security.version>4.2.0.RELEASE</org.springframework.security.version>
|
||||||
|
<springretry.version>1.1.5.RELEASE</springretry.version>
|
||||||
|
|
||||||
<!-- persistence -->
|
<!-- persistence -->
|
||||||
<hibernate.version>5.2.5.Final</hibernate.version>
|
<hibernate.version>5.2.5.Final</hibernate.version>
|
||||||
|
@ -0,0 +1,34 @@
|
|||||||
|
package org.baeldung.springretry;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.ComponentScan;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.retry.annotation.EnableRetry;
|
||||||
|
import org.springframework.retry.backoff.FixedBackOffPolicy;
|
||||||
|
import org.springframework.retry.policy.SimpleRetryPolicy;
|
||||||
|
import org.springframework.retry.support.RetryTemplate;
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@ComponentScan(basePackages = "org.baeldung.springretry")
|
||||||
|
@EnableRetry
|
||||||
|
//Uncomment this two lines if we need XML configuration
|
||||||
|
//@EnableAspectJAutoProxy
|
||||||
|
//@ImportResource("classpath:/retryadvice.xml")
|
||||||
|
public class AppConfig {
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public RetryTemplate retryTemplate() {
|
||||||
|
RetryTemplate retryTemplate = new RetryTemplate();
|
||||||
|
|
||||||
|
FixedBackOffPolicy fixedBackOffPolicy = new FixedBackOffPolicy();
|
||||||
|
fixedBackOffPolicy.setBackOffPeriod(2000l);
|
||||||
|
retryTemplate.setBackOffPolicy(fixedBackOffPolicy);
|
||||||
|
|
||||||
|
SimpleRetryPolicy retryPolicy = new SimpleRetryPolicy();
|
||||||
|
retryPolicy.setMaxAttempts(2);
|
||||||
|
retryTemplate.setRetryPolicy(retryPolicy);
|
||||||
|
|
||||||
|
retryTemplate.registerListener(new DefaultListenerSupport());
|
||||||
|
return retryTemplate;
|
||||||
|
}
|
||||||
|
}
|
@ -0,0 +1,31 @@
|
|||||||
|
package org.baeldung.springretry;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.retry.RetryCallback;
|
||||||
|
import org.springframework.retry.RetryContext;
|
||||||
|
import org.springframework.retry.listener.RetryListenerSupport;
|
||||||
|
|
||||||
|
public class DefaultListenerSupport extends RetryListenerSupport {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(DefaultListenerSupport.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T, E extends Throwable> void close(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
|
||||||
|
logger.info("onClose");
|
||||||
|
super.close(context, callback, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T, E extends Throwable> void onError(RetryContext context, RetryCallback<T, E> callback, Throwable throwable) {
|
||||||
|
logger.info("onError");
|
||||||
|
super.onError(context, callback, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T, E extends Throwable> boolean open(RetryContext context, RetryCallback<T, E> callback) {
|
||||||
|
logger.info("onOpen");
|
||||||
|
return super.open(context, callback);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
@ -0,0 +1,21 @@
|
|||||||
|
package org.baeldung.springretry;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
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))
|
||||||
|
void retryServiceWithRecovery(String sql) throws SQLException;
|
||||||
|
|
||||||
|
@Recover
|
||||||
|
void recover(SQLException e, String sql);
|
||||||
|
|
||||||
|
void templateRetryService();
|
||||||
|
}
|
@ -0,0 +1,39 @@
|
|||||||
|
package org.baeldung.springretry;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
import org.springframework.stereotype.Service;
|
||||||
|
import org.springframework.util.StringUtils;
|
||||||
|
|
||||||
|
@Service
|
||||||
|
public class MyServiceImpl implements MyService {
|
||||||
|
|
||||||
|
private static final Logger logger = LoggerFactory.getLogger(MyServiceImpl.class);
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void retryService() {
|
||||||
|
logger.info("throw RuntimeException in method retryService()");
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void retryServiceWithRecovery(String sql) throws SQLException {
|
||||||
|
if (StringUtils.isEmpty(sql)) {
|
||||||
|
logger.info("throw SQLException in method retryServiceWithRecovery()");
|
||||||
|
throw new SQLException();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void recover(SQLException e, String sql) {
|
||||||
|
logger.info("In recover method");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void templateRetryService() {
|
||||||
|
logger.info("throw RuntimeException in method templateRetryService()");
|
||||||
|
throw new RuntimeException();
|
||||||
|
}
|
||||||
|
}
|
50
spring-all/src/main/resources/retryadvice.xml
Normal file
50
spring-all/src/main/resources/retryadvice.xml
Normal file
@ -0,0 +1,50 @@
|
|||||||
|
<?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>
|
@ -0,0 +1,45 @@
|
|||||||
|
package org.baeldung.springretry;
|
||||||
|
|
||||||
|
import java.sql.SQLException;
|
||||||
|
|
||||||
|
import org.junit.Test;
|
||||||
|
import org.junit.runner.RunWith;
|
||||||
|
import org.springframework.beans.factory.annotation.Autowired;
|
||||||
|
import org.springframework.retry.RetryCallback;
|
||||||
|
import org.springframework.retry.RetryContext;
|
||||||
|
import org.springframework.retry.support.RetryTemplate;
|
||||||
|
import org.springframework.test.context.ContextConfiguration;
|
||||||
|
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
|
||||||
|
import org.springframework.test.context.support.AnnotationConfigContextLoader;
|
||||||
|
|
||||||
|
@RunWith(SpringJUnit4ClassRunner.class)
|
||||||
|
@ContextConfiguration(classes = AppConfig.class, loader = AnnotationConfigContextLoader.class)
|
||||||
|
public class SpringRetryTest {
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private MyService myService;
|
||||||
|
|
||||||
|
@Autowired
|
||||||
|
private RetryTemplate retryTemplate;
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void givenRetryService_whenCallWithException_thenRetry() {
|
||||||
|
myService.retryService();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenRetryServiceWithRecovery_whenCallWithException_thenRetryRecover() throws SQLException {
|
||||||
|
myService.retryServiceWithRecovery(null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test(expected = RuntimeException.class)
|
||||||
|
public void givenTemplateRetryService_whenCallWithException_thenRetry() {
|
||||||
|
retryTemplate.execute(new RetryCallback<Void, RuntimeException>() {
|
||||||
|
@Override
|
||||||
|
public Void doWithRetry(RetryContext arg0) {
|
||||||
|
myService.templateRetryService();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user