我想通过使用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注释注释您的方法,如果方法中有异常,它的执行将重复几次:
Run Code Online (Sandbox Code Playgroud)public class Resource { @RetryOnFailure(attempts = 2, delay = 10, verbose = false) public String load(URL url) { return url.openConnection().getContent(); } }在发生异常时,该方法将重试两次,尝试之间延迟10毫秒.
事实上,仅使用 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)
| 归档时间: |
|
| 查看次数: |
2395 次 |
| 最近记录: |