gpt4 book ai didi

java - @Around (AspectJ) 中未捕获的异常

转载 作者:行者123 更新时间:2023-11-30 09:15:20 26 4
gpt4 key购买 nike

我的想法是使用AspectJ 捕获带注释的方法中的异常,如果抛出任何异常,带注释的方法应该尝试再次运行。我大部分时间都遵循了本教程 ( http://zoftware.blogspot.cz/2008/02/using-aspectj-and-java-annotations-to_23.html ),但我无法让它工作。一切都应该没问题,但事实并非如此。异常在 finally 之前被捕获,抛出的异常有很多,不只是一个。 Catch inside my aspect 似乎根本不起作用。我使用 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 能够在 inside twitter.showUser() 中捕获异常,但这是错误的。如果有人对解决方案感兴趣,这里是:

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

最佳答案

AspectJ 有一个抛出异常后的建议。

你可以有这样的东西:

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

您还应该在 showUser 方法中重新抛出 TwitterException。有关 after() throw 建议的更多信息,请参阅 this link

关于java - @Around (AspectJ) 中未捕获的异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19977531/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com