Repeating Java methods on exception
Annotate your methods with @RetryOnFailure annotation and, in case of exception in the method, its execution will be repeated a few times:
public class Resource {
@RetryOnFailure(attempts = 2, delay = 10, verbose = false)
public String load(URL url) {
return url.openConnection().getContent();
}
}If an exception occurs, the method will retry two times, with a 10 msec exponential back-off delay between attempts.
Since version 0.7.8, you can change time units used for delays (milliseconds by default). For example, this method will wait for two minutes between attempts:
public class Resource {
@RetryOnFailure(delay = 2, unit = TimeUnit.MINUTES)
public String load(URL url) {
// loading of resource
}
}The mechanism is implemented with AOP/AspectJ. Read how to integrate it into your pom.xml.