gpt4 book ai didi

java - 为什么 Spring @Retryable 不提供重试?

转载 作者:行者123 更新时间:2023-11-29 05:03:53 26 4
gpt4 key购买 nike

我刚刚使用@Retryable 注释设置了最简单的Spring 应用程序。

@Service
public class MyRestService {
@Autowired
private RestTemplate restTemplate;

@Retryable(Exception.class)
public void runRest() {
ResponseEntity<String> response = restTemplate.getForEntity(
"https://dat.sparkfun.com/streams/dZ4EVmE8yGCRGx5XRX1W.json",
String.class);
}

@Recover
public void recover(Exception e) {
System.out.println("Recover=" + e);
}
}

根据 Spring 文档 (https://github.com/spring-projects/spring-retry),方法 runRest 应该运行三次,因为它会抛出异常(特别是 org.springframework.web.client.ResourceAccessException)。但是,我没有观察到任何重试。将 ResourceAccessException 用作 @Retryable 的参数没有帮助。

最佳答案

抱歉,答案很简单。我需要在我的类中使用 main 方法指定 @EnableRetry。

@Configuration
@EnableRetry
public class Application {

关于java - 为什么 Spring @Retryable 不提供重试?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31082281/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com