I want to manually call for a retry on a method by using JCabi. Aspect oriented programming should make this easy but I can't figure it out.
import com.jcabi.aspects.RetryOnFailure;
public class Example
{
public int j;
@RetryOnFailure(attempts = 4, delay = 100, verbose = true)
public void retryFun() throws Exception
{
j++;
if(j<3)
throw new Exception();
else
return;
}
public static void main(String[] args) throws Exception
{
Example example = new Example();
System.out.println(example.j);
example.retryFun();
System.out.println(example.j);
}
}
The only example available from jcabi is this one below which doesn't show how to throw an exception to force retry call:
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(); } }
In an exception occurs the method will retry two times, with a 10 msec delay between attempts.