【问题标题】:JCabi aspects @RetryOnFailure how to throw exceptionJCabi方面@RetryOnFailure如何抛出异常
【发布时间】: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 毫秒 尝试之间的延迟。

【问题讨论】:

    标签: java aop jcabi


    【解决方案1】:

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

    经过几个小时的挖掘,我找到了this,它指出我们应该使用最新版本截至目前,2014 年 7 月0.9.2

    这就是我为什么在运行 mvn jcabi:ajc 时从链接中收到错误并且编织不工作的原因。

    【讨论】:

      【解决方案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>
      

      【讨论】:

        【解决方案3】:

        好吧,不幸的是我发现这些 jcabi 方面需要一个自定义 pom.xml 任务来编译项目中所需的方面。

        所以将 jcabi jar 放到 NetBeans 中并编译上面的代码是不够的。

        【讨论】:

          【解决方案4】:

          替代方案,你可以将这些配置添加到 pom.xml 以在编译时进行编织。

          <plugin>
            <groupId>org.codehaus.mojo</groupId>
            <artifactId>aspectj-maven-plugin</artifactId>
            <version>1.4</version>
            <configuration>
                <complianceLevel>1.6</complianceLevel>
                <encoding>${project.build.sourceEncoding}</encoding>
                <showWeaveInfo>true</showWeaveInfo>
                <source>1.7</source>
                <target>1.7</target>
                <verbose>true</verbose>
                <aspectLibraries>
                    <aspectLibrary>
                        <groupId>com.jcabi</groupId>
                        <artifactId>jcabi-aspects</artifactId>
                    </aspectLibrary>
                </aspectLibraries>
            </configuration>
            <executions>
                <execution>
                    <id>weave-classes</id>
                    <phase>process-classes</phase>
                    <goals>
                        <goal>compile</goal>
                    </goals>
                </execution>
            </executions>
          </plugin>
          

          参考:http://aspects.jcabi.com/example-aspectj.html

          【讨论】:

            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 2016-12-25
            • 2013-05-24
            • 1970-01-01
            • 2012-08-10
            • 2014-10-20
            • 1970-01-01
            • 1970-01-01
            相关资源
            最近更新 更多