【问题标题】:How to perform new operation on @RetryOnFailure by jcabi如何通过 jcabi 对 @RetryOnFailure 执行新操作
【发布时间】:2016-09-24 07:11:52
【问题描述】:

我正在使用 jcabi-aspects 重试连接到我的 URL http://xxxxxx:8080/hello 直到连接恢复。如您所知,jcabi 的 @RetryOnFailure 有两个字段尝试和延迟。

我想在 jcabi @RetryOnFailure 上执行 attempts(12)=expiryTime(1 min=60000 millis)/delay(5 sec=5000 millis) 之类的操作。我该怎么做。代码sn-p如下。

@RetryOnFailure(attempts = 12, delay = 5)
public String load(URL url) {
  return url.openConnection().getContent();
}

【问题讨论】:

  • 看叶戈尔的博客,有一篇关于这个功能的帖子:yegor256.com/2014/08/15/retry-java-method-on-exception.html我觉得可以帮到你
  • 不,实际上我想运行每 5 秒失败一次的代码,最多 1 分钟,即 到期时间。因为我的代码到期时间很重要!!如何在@RetryOnFailure 中加入到期时间是我的问题

标签: spring spring-aop jcabi


【解决方案1】:

你可以组合两个注解:

@Timeable(unit = TimeUnit.MINUTE, limit = 1)
@RetryOnFailure(attempts = Integer.MAX_VALUE, delay = 5)
public String load(URL url) {
  return url.openConnection().getContent();
}

@RetryOnFailure 将永远重试,但@Timeable 将在一分钟后停止。

【讨论】:

    【解决方案2】:

    您选择的库 (jcabi) 没有此功能。但幸运的是,Spring-Batch 中非常方便的 RetryPolicies 已被提取(因此您可以单独使用它们,无需批处理):

    Spring-Retry

    您可以从那里使用的众多类之一是 TimeoutRetryPolicy:

    RetryTemplate 模板 = new RetryTemplate();

    TimeoutRetryPolicy policy = new TimeoutRetryPolicy();
    policy.setTimeout(30000L);
    
    template.setRetryPolicy(policy);
    
    Foo result = template.execute(new RetryCallback<Foo>() {
    
        public Foo doWithRetry(RetryContext context) {
            // Do stuff that might fail, e.g. webservice operation
            return result;
        }
    
    });
    

    整个spring-retry项目非常好用,功能齐全,比如backOffPolicies、listeners等。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2014-11-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-30
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多