1

我只是使用 @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)。但是,我没有观察到任何重试。使用 @Retryable 作为参数 ResourceAccessException 没有帮助。

4

2 回答 2

3

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

@Configuration
@EnableRetry
public class Application {
于 2015-06-26T21:27:43.760 回答
0

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

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

于 2015-09-08T15:32:51.923 回答