我想使用 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 中唯一可用的示例是下面的示例,它没有显示如何抛出异常以强制重试调用:
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.
最佳答案
对于那些仍在寻找答案的人来说,叶戈尔的答案现在已经过时了。他发布的 jcabi-maven-plugin 版本 0.8
对我不起作用。
经过几个小时的挖掘,我找到了 this其中声明我们应该使用的最新版本 As of this time, July 2014 是 0.9.2
。
这就是我为什么在运行 mvn jcabi:ajc 时从链接中收到错误并且编织不工作的原因。
关于java - JCabi方面@RetryOnFailure如何抛出异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18896204/