这是检查您的问题的代码(注意:该代码与 Failsafe 2.0 相关)
private RetryPolicy<Object> MY_RETRY_POLICY = new RetryPolicy<>()
.handle(RuntimeException.class)
.withMaxRetries(3);
private void myCurrentFunction() {
Failsafe.with(MY_RETRY_POLICY)
.run(() -> runJob());
otherFunction1();
}
private void otherFunction1() {
System.out.println("OtherFunction1");
}
private void runJob() {
System.out.println("run job...");
throw new RuntimeException("Exception");
}
答案是否定的;OtherFunction1
在所有重试完成之前不会执行。
实际上,如果所有重试失败OtherFunction1
将永远不会被调用。
这是测试代码的输出
run job...
run job...
run job...
run job...
java.lang.RuntimeException: Exception
不过,您可以修改重试策略以执行OtherFunction1
1) 每次重试后
private RetryPolicy<Object> MY_RETRY_POLICY = new RetryPolicy<>()
.handle(RuntimeException.class)
.onRetry(fail -> otherFunction1())
.withMaxRetries(3);
2)重试失败后
private RetryPolicy<Object> MY_RETRY_POLICY = new RetryPolicy<>()
.handle(RuntimeException.class)
.onFailure(fail -> otherFunction1())
.withMaxRetries(3);