【问题标题】:Why Spring @Retryable does not provide retry?为什么 Spring @Retryable 不提供重试?
【发布时间】:2015-06-26 21:16:23
【问题描述】:

我只是用@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 方法应该运行 3 次,因为它会抛出异常(特别是 org.springframework.web.client.ResourceAccessException)。但是,我没有观察到任何重试。使用 @Retryable 作为参数 ResourceAccessException 没有帮助。

【问题讨论】:

    标签: java spring spring-retry


    【解决方案1】:

    对不起,很容易回答。我需要在我的类中使用 main 方法指定 @EnableRetry。

    @Configuration
    @EnableRetry
    public class Application {
    

    【讨论】:

    【解决方案2】:

    您也可以使用基于 XML 的配置。

    <aop:config>
    <aop:pointcut id="retryable" expression="execution(* it..*class.method(..))" />
    <aop:advisor pointcut-ref="retryable" advice-ref="retryAdvice" order="-1"/>
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-13
      • 2022-06-22
      • 1970-01-01
      • 2021-12-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多