我使用 SimpleRetryPolicy/RetryTemplate 尝试了带有简单 java 类的 spring 重试框架。它工作得很好。所以我想对注释做同样的事情。注释从未对我有用。网上也没有太多帮助。下面的代码就像普通的 Java 程序一样工作,在第一次尝试时抛出异常,这不是预期的行为。在抛出异常或从中恢复之前,它应该至少尝试了 5 次。不知道我哪里出错了。我是否需要为此进行任何 XML/spring AOP 配置才能工作?那会怎样
import org.springframework.context.annotation.Configuration;
import org.springframework.retry.annotation.EnableRetry;
@EnableRetry
@Configuration
public class App {
public static void main(String[] args) throws Exception {
Parentservice p = new SpringRetryWithHystrixService();
String status = p.getStatus();
System.out.println(status);
}
}
import org.springframework.retry.annotation.Retryable;
public interface Parentservice {
@Retryable(maxAttempts=5)
String getStatus() throws Exception;
}
import org.springframework.retry.annotation.Recover;
public class SpringRetryWithHystrixService implements Parentservice{
public static int i=0;
public String getStatus() throws Exception{
System.out.println("Attempt:"+i++);
throw new NullPointerException();
}
@Recover
public static String getRecoveryStatus(){
return "Recovered from NullPointer Exception";
}
}
我在接口和实现的顶部尝试了@Retryable(maxAttempts=5),但没有任何区别。