Java @围绕切入点调用方法两次
有时我的API抛出异常,该服务器无法处理我的请求。我决定创建AOP方面,它将重新调用API调用。例如,5次之后,如果仍然不起作用,就会抛出异常 请看我的AOP课程。这不是一个完整的身体,但我希望你们能够理解发生了什么:Java @围绕切入点调用方法两次,java,aspectj,spring-aop,Java,Aspectj,Spring Aop,有时我的API抛出异常,该服务器无法处理我的请求。我决定创建AOP方面,它将重新调用API调用。例如,5次之后,如果仍然不起作用,就会抛出异常 请看我的AOP课程。这不是一个完整的身体,但我希望你们能够理解发生了什么: @Aspect public class RetryRequestExecutor { .... @Around("@annotation(com.test.RequestRetriable)") public Object retryApiReque
@Aspect
public class RetryRequestExecutor {
....
@Around("@annotation(com.test.RequestRetriable)")
public Object retryApiRequest(ProceedingJoinPoint point) throws Throwable {
int numAttempts = 0;
ServiceException lastException;
do {
numAttempts++;
try {
preInvokeLog(point);
Object retValue = point.proceed();
postInvokeLog(point);
return retValue;
} catch (ServiceException e) {
lastException = handleServiceException(point, numAttempts, e);
}
} while (numAttempts <= maxRetries);
throw lastException;
}
....
}
现在,当我直接调用getUsers
方法时,一切正常-AOP只调用它一次。但是,当我调用retrieveSuspendedUsers
method时,AOP会为每个页面调用它两次(我逐页检索用户,页面大小等于100)。我可以在日志中看到以下几行:
2013-03-11 13:06:40,179 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/]
2013-03-11 13:06:40,180 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/]
2013-03-11 13:06:41,745 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/] finished successfully
2013-03-11 13:06:41,745 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/] finished successfully
API调用非常耗时,我希望避免额外的、不必要的调用。如何修复此行为?针对切入点建议的
调用和执行事件调用AFAIK切入点拦截。您可以筛选以仅匹配切入点中的方法执行
:
@Around("execution(* *(..)) && @annotation(com.test.RequestRetriable)")
你能记录调用方线程名吗nextLink
的分配是如何处理的?你的建议似乎很好,我怀疑有人在调用getUsers
两次,如果你能记录调用方线程,我们可以验证这一点我已经将线程名添加到日志中了@matsev-我已经添加了源代码,显示了如何检索nextlink。你是对的!它起作用了!你让我开心!现在它只调用一次,即使在我的“retrieveSuspendedUsers”方法中也是如此。我认为它只适用于默认情况下的“执行”。
2013-03-11 13:06:40,179 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/]
2013-03-11 13:06:40,180 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/]
2013-03-11 13:06:41,745 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/] finished successfully
2013-03-11 13:06:41,745 DEBUG [pool-2-thread-1] Invoke API request getUsers with arguments [https://domain.com/user/2.0/] finished successfully
@Around("execution(* *(..)) && @annotation(com.test.RequestRetriable)")