【发布时间】:2013-08-01 03:46:38
【问题描述】:
这是我第一次使用 AOP,所以这可能是一个非常菜鸟的问题。
public class MyAspect implements AspectI {
public void method1() throws AsyncApiException {
System.out.println("In Method1. calling method 2");
method2();
}
@RetryOnInvalidSessionId
public void method2() throws AsyncApiException {
System.out.println("In Method2, throwing exception");
throw new AsyncApiException("method2", AsyncExceptionCode.InvalidSessionId);
}
public void login() {
System.out.println("Logging");
}
InvalidSessionHandler 看起来像这样。
@Aspect
public class InvalidSessionIdHandler implements Ordered {
@Around("@annotation(com.pkg.RetryOnInvalidSessionId)")
public void reLoginAll(ProceedingJoinPoint joinPoint) throws Throwable {
System.out.println("Hijacked call: " + joinPoint.getSignature().getName() + " Proceeding");
try {
joinPoint.proceed();
} catch (Throwable e) {
if (e instanceof AsyncApiException) {
AsyncApiException ae = (AsyncApiException) e;
if (ae.getExceptionCode() == AsyncExceptionCode.InvalidSessionId) {
System.out.println("invalid session id. relogin");
AspectI myAspect = (AspectI) joinPoint.getTarget();
myAspect.login();
System.out.println("Login done. Proceeding again now");
joinPoint.proceed();
}
}
}
}
@Override
public int getOrder() {
return 1;
}
}
弹簧配置
<aop:aspectj-autoproxy />
<bean id="myAspect" class="com.pkg.MyAspect" />
<bean id="invalidSessionIdHandler" class="com.pkg.InvalidSessionIdHandler" />
我的意图是当我调用
myAspect.method1()时,它又调用method2,如果method2抛出InvalidSessionId异常,那么只有method2应该被重试。但是上面的代码似乎没有做任何事情。它只是在从方法 2 引发异常后立即返回。但是,如果我将@RetryOnInvalidSessionId放在method1上,那么整个method1都会重试。为了学习,我保持
method2是公开的,但实际上我希望它是private。我在这里不知道如何重试私有方法。
任何建议都会有所帮助。
谢谢
【问题讨论】: