Spring重新尝试未来任务不起作用
我有一个spring引导应用程序,它需要调用外部API。 如果有“n”个外部调用,则将创建“n”个未来任务并进行rest调用。这里的问题是,如果任何rest调用异常失败,我需要重试调用3次。 我尝试使用Spring-retry,但失败时它不会重试 以下是迄今为止我使用retry尝试的代码片段。 当任何被调用的服务关闭且程序不重试或进入恢复块时,我立即收到IO异常。 这是因为没有为新线程创建代理Spring重新尝试未来任务不起作用,spring,spring-boot,java-8,spring-retry,futuretask,Spring,Spring Boot,Java 8,Spring Retry,Futuretask,我有一个spring引导应用程序,它需要调用外部API。 如果有“n”个外部调用,则将创建“n”个未来任务并进行rest调用。这里的问题是,如果任何rest调用异常失败,我需要重试调用3次。 我尝试使用Spring-retry,但失败时它不会重试 以下是迄今为止我使用retry尝试的代码片段。 当任何被调用的服务关闭且程序不重试或进入恢复块时,我立即收到IO异常。 这是因为没有为新线程创建代理 @SpringBootApplication @EnableRetry public class Sp
@SpringBootApplication
@EnableRetry
public class SpringBootWebApplication implements CommandLineRunner{
@Autowired
RestClient client;
public static void main(String[] args) {
SpringApplication.run(SpringBootWebApplication.class, args);
}
@Override
public void run(String... args) throws Exception {
String a[] = new String[] {args[0],args[1],args[2] };
// getting the list view of Array
List<String> list = Arrays.asList(a);
client.executeRest(list)
}
}
######
I have another class where future tasks will be created.
public class RestClient(){
public void executeRest(List<String> uris)){
ExecutorService executor = Executors.newFixedThreadPool(2);
for(String uri : uris){
Future<String> t = executor.execute(new MyCallable(uri));
}
}
}
public class MyCallable implements Callable<String> {
private String endPoint;
public MyCallable(String endPoint){
this.endPoint=endPoint;
}
@Override
public void call() throws Exception {
system.out.println(makeRestGetCall(String uri));
}
@Retryable(
value = { Excelption.class },
maxAttempts = 2,
backoff = @Backoff(delay = 5000))
public String makeRestGetCall(String uri){
return restTemplate.exchange(uri, HttpMethod.GET,String.class);
}
@Recover
void recover(Exception e){
system.out.println("Max retries done")
}
}
@springboot应用程序
@启用重试
公共类SpringBootWebApplication实现CommandLineRunner{
@自动连线
rest客户端;
公共静态void main(字符串[]args){
run(SpringBootWebApplication.class,args);
}
@凌驾
公共无效运行(字符串…参数)引发异常{
字符串a[]=新字符串[]{args[0],args[1],args[2]};
//获取数组的列表视图
List=Arrays.asList(a);
client.executest(列表)
}
}
######
我有另一个类,将来的任务将被创建。
公共类RestClient(){
public void executeest(列出URI)){
ExecutorService executor=Executors.newFixedThreadPool(2);
for(字符串uri:uri){
Future t=executor.execute(新的MyCallable(uri));
}
}
}
公共类MyCallable实现了Callable{
私有字符串端点;
公共MyCallable(字符串端点){
this.endPoint=endPoint;
}
@凌驾
public void call()引发异常{
system.out.println(makeRestGetCall(字符串uri));
}
@可回收(
值={exception.class},
最大尝试次数=2,
退避=@backoff(延迟=5000))
公共字符串makeRestGetCall(字符串uri){
返回restemplate.exchange(uri,HttpMethod.GET,String.class);
}
@恢复
无效恢复(例外e){
system.out.println(“最大重试次数已完成”)
}
}
只有当该方法抛出值中配置的异常(在您的情况下是异常
)restTemplate时,才会尝试重试。exchange方法抛出多个异常
,因此请尝试使用自定义异常
public class CustomException extends RuntimeException {
}
重试方法
@Retryable(
value = { CustomException.class },
maxAttempts = 2,
backoff = @Backoff(delay = 5000))
public String makeRestGetCall(String uri){
try {
return restTemplate.exchange(uri, HttpMethod.GET,String.class);
}catch(Exception ex) {
// do something or log something
throw new CustomException();
}
}
这是拼写错误吗?