【发布时间】:2013-09-19 13:37:28
【问题描述】:
我想使用 JCabi 手动调用方法重试。面向方面的编程应该让这很容易,但我想不通。
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);
}
}
jcabi 提供的唯一示例是下面的示例,它没有显示如何引发异常以强制重试调用:
使用 @RetryOnFailure 注释来注释您的方法,以防万一 方法中的异常将重复执行几次:
public class Resource { @RetryOnFailure(attempts = 2, delay = 10, verbose = false) public String load(URL url) { return url.openConnection().getContent(); } }如果发生异常,该方法将重试两次,每次 10 毫秒 尝试之间的延迟。
【问题讨论】: