Sometimes my API raises an exception, this server cannot process my request. I decided to create an AOP aspect that would invoke an API call. For example, 5 times and after that throws an exception if it still does not work.
Please see my AOP class. This is not a complete body, but I hope you can 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 class of service:
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 only annotated the getUsers method. The retrieveSuspendedUsers method is not annotated.
Spring configuration is as follows:
<aop:aspectj-autoproxy/>
Now, when I call the getUsers method getUsers , everything works fine - AOP calls it only once. But when I call the retrieveSuspendedUsers method - AOP call it twice for each page (I get user pages behind page with page size of 100). I see in the log below the line:
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
An API call is very time consuming and I want to avoid an extra, unnecessary call. How to fix this behavior?