Spring 4-Spring重试1.2.1.RELEASE@Recover不工作
我已经创建了一个adapterImpl类,该类将使用objA重试一个方法,但是如果它抛出一个异常(硬编码为抛出),它将调用recover方法,该方法将再次使用objB调用该方法 我的问题是,@Recover方法没有被调用。我不确定我在这里做错了什么 弹簧版本-4.3.5.1版本 Spring重试-1.2.1.0版本 我的配置类-Spring 4-Spring重试1.2.1.RELEASE@Recover不工作,spring,spring-4,spring-retry,Spring,Spring 4,Spring Retry,我已经创建了一个adapterImpl类,该类将使用objA重试一个方法,但是如果它抛出一个异常(硬编码为抛出),它将调用recover方法,该方法将再次使用objB调用该方法 我的问题是,@Recover方法没有被调用。我不确定我在这里做错了什么 弹簧版本-4.3.5.1版本 Spring重试-1.2.1.0版本 我的配置类- @Configuration @EnableRetry public class ConfigClass { @Bean public ClassTest beanA
@Configuration
@EnableRetry
public class ConfigClass {
@Bean
public ClassTest beanA(){
ClassTest obj = new ClassTest();
obj.setProp(5);
return obj;
}
@Bean
public ClassTest beanB(){
ClassTest obj = new ClassTest();
obj.setProp(10);
return obj;
}
@Bean("adapterImpl")
public AdapterInterfaceImpl adapter(){
AdapterInterfaceImpl obj = new AdapterInterfaceImpl();
return obj;
}
}
我的AdapterInterfaceImpl类-
public class AdapterInterfaceImpl implements AdapterInterface{
@Autowired
@Qualifier("beanA")
private ClassTest objA;
@Autowired
@Qualifier("beanB")
private ClassTest objB;
public ClassTest getObjA() {
return objA;
}
public void setObjA(ClassTest objA) {
this.objA = objA;
}
public ClassTest getObjB() {
return objB;
}
public void setObjB(ClassTest objB) {
this.objB = objB;
}
@Retryable(maxAttempts = 3, include = Exception.class, backoff = @Backoff(delay = 2000))
public int getValue(int val) throws Exception{
System.out.println("obj A get Value");
return getValue(objA,val);
}
public int getValue(ClassTest obj, int val) throws Exception{
System.out.println("get Value");
if(obj==objA){
throw new Exception("This is msg");
}
return obj.methodA(val);
}
@Recover
public int getValue(Exception e, int val){
System.out.println("Recover get Value");
try{
return getValue(objB,val);
}catch(Exception e1){
return 0;
}
}
我的班级考试-
public class ClassTest {
private int prop;
public int getProp() {
return prop;
}
public void setProp(int prop) {
this.prop = prop;
}
public int methodA(int x){
return x+prop;
}
}
我的课堂主要方法是-
public class App
{
public static void main( String[] args )
{
AbstractApplicationContext context = new
AnnotationConfigApplicationContext(ConfigClass.class);
AdapterInterface adapter = (AdapterInterface)
context.getBean("adapterImpl");
try {
System.out.println(adapter.getValue(3));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
我的输出未显示任何重试或恢复-
A get Value
get Value
obj A get Value
get Value
obj A get Value
get Value
org.springframework.retry.ExhaustedRetryException: Cannot locate recovery method; nested exception is java.lang.Exception: This is msg
Spring重试使用AOP,内部调用(从
getValue(int)
到getValue(ClassTest,int)
)不会通过代理
您必须将
@Retryable
放在外部调用的方法上,以便代理可以拦截调用并应用重试逻辑。中报告了类似的问题
因此,
@enablerery(proxyTargetClass=true)
现在可以在实现类中找到恢复方法了。您好,谢谢,我的重试成功了。但我得到的无法定位恢复方法代码>作为例外。我尝试将恢复方法的名称更改为getValue(exception,int)。通过删除ClassTest参数,使其能够匹配getValue(int)retryable方法。仍然不起作用。