JCabi方面@RetryOnFailure如何抛出异常

den*_*ini 7 java aop jcabi

我想通过使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

jcabi提供的唯一示例是下面的示例,它不显示如何抛出异常以强制重试调用:

使用@RetryOnFailure注释注释您的方法,如果方法中有异常,它的执行将重复几次:

public class Resource {
  @RetryOnFailure(attempts = 2, delay = 10, verbose = false)
  public String load(URL url) {
    return url.openConnection().getContent();
  }
}
Run Code Online (Sandbox Code Playgroud)

在发生异常时,该方法将重试两次,尝试之间延迟10毫秒.

Tur*_*lin 7

对于那些仍在寻找答案的人来说,叶戈尔的答案现在已经过时了.0.8他发布的jcabi-maven-plugin版本不适合我.

经过几个小时的挖掘后,我发现这表明我们应该使用最后一个版本截至2014年7月是0.9.2.

这是我为什么在运行mvn jcabi时的答案:ajc我从链接中得到错误,而且编织也无法正常工作.


yeg*_*256 2

事实上,仅使用 jcabi 注释是不够的。您应该“编织”您的源代码或二进制文件。我建议编织二进制文件,如下所述: http: //aspects.jcabi.com/example-weaving.html。将此插件添加到您的pom.xml中即可完成:

<plugin>
    <groupId>com.jcabi</groupId>
    <artifactId>jcabi-maven-plugin</artifactId>
    <version>0.8</version>
    <executions>
      <execution>
        <goals>
          <goal>ajc</goal>
        </goals>
      </execution>
    </executions>
</plugin>
Run Code Online (Sandbox Code Playgroud)