@Around pointcut invokes method two times
aspectj, java, spring-aop
Solution
AFAIK pointcut interceptions are called both for the `call` and `execution` events of the pointcut advice. You could filter to match only method `execution` in your pointcut:
@Around("execution(* *(..)) && @annotation(com.test.RequestRetriable)")
Problem
Sometimes my API throws exception, that server can not process my request. I decided to create AOP aspect which will be reinvoke API call. For example, 5 times and after that throws exception if it still doesn't work. Please, see my AOP class. This is not a full body, but I hope you will able to understand what is going on: ``` @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; } .... } ``` This is my service class: ``` public class UserApiImpl implements UserApi { ... @Override public List<DomainUser> retrieveSuspendedUsers() throws Exception{ LOG.debug("Retrieving suspended users."); ... List<DomainUser> users = new ArrayList<DomainUser>(64); do { //Invoke API. AOP invoke it two times! currentPage = getUsers(retrieveUrl); ... URL nextLink = currentPage.getNextLink(); if (nextLink == null){ break; } ... } while (nextLink != null); return users; } @Override @RequestRetriable public UserFeed getUsers(URL feedUrl) throws Exception { return userService.getFeed(feedUrl, UserFeed.class); } ... } ``` As you can see, I have annotated only `getUsers` method. Method `retrieveSuspendedUsers` is not annotated. Spring configuration looks like this: ``` <aop:aspectj-autoproxy/> ``` Now, when I invoke directly `getUsers` method everything works OK - AOP invokes it only once. But when I invoke `retrieveSuspendedUsers` method - AOP invoke it two times for each page(I retrieve users page by page with page size equal 100). I can see in log below lines: ``` 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 call is very time consuming and I want to avoid additional, unnecessary call. How to fix this behavior?