Chetan Sistla
Chetan Sistla

Reputation: 175

How to perform new operation on @RetryOnFailure by jcabi

Iam using jcabi-aspects to retry connection to my URL http://xxxxxx:8080/hello till the connection comes back.As you know @RetryOnFailure by jcabi has two fields attempts and delay.

I want to perform the operation like attempts(12)=expiryTime(1 min=60000 millis)/delay(5 sec=5000 millis) on jcabi @RetryOnFailure.How do i do this.The code snippet is as below.

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

Upvotes: 1

Views: 858

Answers (2)

yegor256
yegor256

Reputation: 105153

You can combine two annotations:

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

@RetryOnFailure will retry forever, but @Timeable will stop it in a minute.

Upvotes: 1

Gergely Bacso
Gergely Bacso

Reputation: 14661

The library you picked (jcabi) does not have this feature. But luckily the very handy RetryPolicies from Spring-Batch have been extracted (so you can use them alone, without the batching):

Spring-Retry

One of the many classes you could use from there is TimeoutRetryPolicy:

RetryTemplate template = 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;
    }

});

The whole spring-retry project is very easy to use and full of features, like backOffPolicies, listeners, etc.

Upvotes: 1

Related Questions