<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.dalong.spring.io</groupId>
<artifactId>springretrydemo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.4.2.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.5.RELEASE</version><!--$NO-MVN-MAN-VER$ -->
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.geronimo.bundles</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.6.8_2</version>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version><!--$NO-MVN-MAN-VER$ -->
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
@Service
public class FirstService {
@Retryable(value=RemoteAccessException.class,maxAttempts=3,backoff=@Backoff(delay = 5000l,multiplier = 1))
public String service(String value) {
// ... do something
System.out.println("do something...");
if(null==value||"".equals(value)){
throw new RemoteAccessException("RPC调用异常");
}
else{
return "dalong demo info";
}
}
@Recover
public String recover(RemoteAccessException e) {
// ... panic
System.out.println(e.getMessage());
System.out.println("rong method");
return e.getMessage();
}
}
@Service
public class MyService2 {
@Autowired
public UserOperator userOperator;
public UserInfo getUserinfo() throws TimeoutException {
RetryTemplate template = new RetryTemplate();
TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
policy.setTimeout(1000L);
template.setRetryPolicy(policy);
UserInfo result = template.execute(new RetryCallback<UserInfo, TimeoutException>() {
@Override
public UserInfo doWithRetry(RetryContext context) throws TimeoutException {
// TODO Auto-generated method stub
UserInfo info = null;
info = userOperator.getUserinfo();
return info;
}
},new RecoveryCallback<UserInfo>(){
@Override
public UserInfo recover(RetryContext context) throws Exception {
// TODO Auto-generated method stub
UserInfo inf = new UserInfo();
inf.setDate(new Date());
inf.setAge(333);
inf.setInfo("default");
return inf;
}
});
return result;
}
}
@Service
public class UserOperator {
public UserInfo getUserinfo() throws TimeoutException {
UserInfo info = new UserInfo();
info.setDate(new Date());
info.setAge(333);
info.setInfo("dddddd");
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
throw new TimeoutException("timeout");
}
return info;
}
}
@Autowired
public MyService2 myService2;
@RequestMapping(value = "/user", method = RequestMethod.GET)
public Object getuser() throws TimeoutException {
UserInfo info = null;
info = myService2.getUserinfo();
return info;
// return "this is demo";
}
org.springframework.retry.policy.ExceptionClassifierRetryPolicy
根据产生的异常选择重试策略。
org.springframework.retry.policy.CompositeRetryPolicy
用户指定一组策略,随后根据optimistic选项来确认如何重试。
原文:http://www.cnblogs.com/rongfengliang/p/6253121.html