Check this one http://aspects.jcabi.com/annotation-retryonfailure.html
It provides an annotation to specify the retry strategy.
سؤال
I am invoking web service in java. I have the requirement, tha if if there was error during web service invocation, the same Web service call needs to be retriggered after a certain predefined time interval for max attempts, say 5.
Is there any api in java I can use for this purpose?
المحلول
Check this one http://aspects.jcabi.com/annotation-retryonfailure.html
It provides an annotation to specify the retry strategy.
نصائح أخرى
Spring has a retry annotation which servers the purpose
Step 1: Add following dependency to your POM
<dependency>
<groupId>org.springframework.retry</groupId>
<artifactId>spring-retry</artifactId>
<version>1.1.5.RELEASE</version>
</dependency>
Step 2: Enabling Spring Retry
To enable Spring Retry in an application, we need to add the @EnableRetry annotation to our @Configuration class:
Ex:
@Configuration
@EnableRetry
public class AppConfig { ... }
Step 3: To add retry functionality to methods, @Retryable can be used:
Ex:
@Service
public interface MyService {
@Retryable(
value = { SQLException.class },
maxAttempts = 2,
backoff = @Backoff(delay = 5000))
void retryService(String sql) throws SQLException;
...
}
Step 4.The @Recover annotation is used to define a separate recovery method when a @Retryable method fails with a specified exception:
Ex:
@Service
public interface MyService {
...
@Recover
void recover(SQLException e, String sql);
}
See Url for more details : http://www.baeldung.com/spring-retry