Исключения не обнаружены внутри @Around (AspectJ)

Моя идея состоит в том, чтобы использовать AspectJ для перехвата исключений в аннотированных методах, и, если возникнут какие-либо исключения, аннотированный метод должен попытаться запустить снова. Я в основном следовал этому руководству ( http://zoftware.blogspot.cz/2008/02/using-aspectj-and-java-annotations-to_23.html), но не могу заставить его работать. Все должно быть хорошо, но это не так. Исключения обнаруживаются до того, как в конце концов появляется много исключений, а не одно. Поймать в моем аспекте, кажется, не работает вообще. Я использую AspectJ 1.7.3. Код...

Аннотация:

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface RetryIfFailed {

    int maxRetries() default 5;
}

Аннотированный метод:

@RetryIfFailed(maxRetries = 3)
private User showUser(String userName) {
    try {
        return twitter.showUser(userName);
    } catch (TwitterException e) {
        System.out.println("I am catch inside showUser");
    }
    return null;
}

аспект:

    @Around("call(@RetryIfFailed * *..*(..))")
    public Object retryMaxRetriesTimes(ProceedingJoinPoint thisJoinPoint) throws Throwable {
        System.out.println("Entering retryMax...");
        Method method = ((MethodSignature) thisJoinPoint.getSignature()).getMethod();
        RetryIfFailed annotation = method.getAnnotation(RetryIfFailed.class);
        int retries = annotation.maxRetries();

        Object ret = null;
        while (retries > 0) {
            try {
                System.out.println("Before proceeding... Retries=" + retries);
                ret = thisJoinPoint.proceed();
            } catch (Throwable e) {
                System.out.println("I am catched in RetryMax ");
                retries--;
                if (retries == 0) {
                    System.out.println("Exception caught. Rethrowing..." + e);
                    throw new ConnectionErrorException("Twitter service failed to establish connection", e);
                }
            } finally {
                System.out.println("Finally block..." + retries);
                if (ret != null) {
                    System.out.println("Object returned: " + ret);
                    return ret;
                }

                System.out.println("Decresing retries to" + retries);
                retries--;
                if (retries == 0) {
                    throw new ConnectionErrorException("It should not get here.");
                }
            }
        }

        //should never be reached
        return null;
    }
}

Конфигурация Maven:

<!-- Build with AspectJ-->
    <build>
        <plugins>
            <plugin>
                <groupId>org.codehaus.mojo</groupId>
                <artifactId>aspectj-maven-plugin</artifactId>
                <version>1.5</version>
                <configuration>
                    <source>1.7</source>
                    <target>1.7</target>
                    <complianceLevel>1.7</complianceLevel>
                    <verbose>true</verbose>
                </configuration>
                <executions>
                    <execution>
                        <goals>
                            <goal>compile</goal>
                        </goals>
                    </execution>
                </executions>
                <dependencies>
                </dependencies>
            </plugin>
        </plugins>
    </build>

Выход:

Entering retryMax...
Before proceeding... Retries=3
I am catch inside showUser
Finally block...3
Decresing retries to3
Before proceeding... Retries=2
I am catch inside showUser
Finally block...2
Decresing retries to2
Before proceeding... Retries=1
I am catch inside showUser
Finally block...1
Decresing retries to1
Exception in thread "main" ...<path>....ConnectionErrorException: It should not get here.
       at ...<stackTrace follows>...

Спасибо за любой совет:).

РЕДАКТИРОВАТЬ

Как предложил mvieghofer, я никогда не отказываюсь от исключения. Я ожидал, что @Around поймает исключение внутри twitter.showUser(), и это было неправильно. В случае, если кто-то будет заинтересован в решении, вот оно:

    @RetryIfFailed
    public static User showUser(String userName) throws ConnectionErrorException {
        try {
            return twitter.showUser(userName);
        } catch (TwitterException e) {
            throw new ConnectionErrorException(exceptionMessage, e);
        }
    }

1 ответ

Решение

Есть совет по исключению после броска для AspectJ.

Вы могли бы иметь что-то вроде этого:

aspect A {
  pointcut publicCall(): call(@RetryIfFailed * *..*(..));
  after() throwing (TwitterExepction e): publicCall() {
  System.out.println("Threw an exception: " + e);
  }

}

Также вам следует сбросить TwitterException внутри вашего метода showUser. Для получения более подробной информации о метании after() смотрите эту ссылку

Другие вопросы по тегам