我有什么
我设置了一个 AspectJ一些特定方法的联合点,以便能够测量它们的执行时间。我从不拦截代码流中的任何内容(因此我们可以将其称为“只读”类型的编织代码)。相应的代码如下所示:
@Around("execution (* my.package.myclass..*(..)) && @annotation(my.another.package.Monitored)")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
long startTime = System.currentTimeMillis();
try {
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
System.out.println("Intercepted exception " + throwable.getClass().getName() + ": " + throwable.getMessage());
throw throwable; //<---- this does the jail-breaking
} finally {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
Monitored annotation = method.getAnnotation(Monitored.class);
//do some more logic as logging etc.
}
return returnObject;
}
同样在应用程序代码本身中,我有类似的东西:
try {
//this is a monitored call:
my.package.myclass.doStuff();
} catch (Throwable anyException) {
//log stuff
//& return default non-null entity
}
这意味着我会在该层优雅地处理任何可能的异常,并不允许将其抛出到上层。
出了什么问题
如果应用程序代码没有抛出异常,则没有问题,所有逻辑都正常工作 - 时间被测量、记录和跟踪。但是如果应用程序抛出异常,它会逃脱我在上面发布的应用程序处理程序并被抛出到上层。
在调试器中,我看到它是由从我的方面处理程序中抛出 throwable
的行完成的。这是我不明白的。显然,如果我从那里删除抛出异常,情况会变得更糟,因为现在实体将为 null
并且整个应用程序流程将被破坏
问题
如何正确处理异常,以便在进行所有测量业务时将它们记录下来,不让它们越狱?
最佳答案
就像 Nándor 一样,它在尝试复制您的情况时对我有用,即使是 LTW。这是一个独立的例子:
Java驱动程序应用:
package de.scrum_master.app;
public class Application {
public static void main(String[] args) {
try {
new Application().doSomething();
}
catch (Throwable t) {
System.out.println("Caught & handled exception: " + t);
}
}
public void doSomething() throws InterruptedException {
Thread.sleep(100);
throw new RuntimeException("Oops!");
}
}
看点:
package de.scrum_master.aspect;
import java.lang.reflect.Method;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.reflect.MethodSignature;
@Aspect
public class RuntimeLogger {
@Around("execution(!static * *(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
Object returnObject = null;
long startTime = System.currentTimeMillis();
try {
returnObject = joinPoint.proceed();
} catch (Throwable throwable) {
System.out.println("Intercepted exception " + throwable.getClass().getName() + ": " + throwable.getMessage());
throw throwable; //<---- this does the jail-breaking
} finally {
long endTime = System.currentTimeMillis();
long elapsedTime = endTime - startTime;
MethodSignature signature = (MethodSignature) joinPoint.getSignature();
Method method = signature.getMethod();
System.out.println(elapsedTime + " " + method);
}
return returnObject;
}
}
控制台日志:
Intercepted exception java.lang.RuntimeException: Oops!
100 public void de.scrum_master.app.Application.doSomething() throws java.lang.InterruptedException
Caught & handled exception: java.lang.RuntimeException: Oops!
不久前在 StackOverflow 上,有人在寻求一种方法来生成某种无法捕获的“Chuck Norris 异常”,我为他创建了一个 here使用 AspectJ。所以,只是猜测,您是否可能在代码中的任何地方有另一个方面或建议,从 before() : handler()
建议中(重新)抛出有问题的异常?例如,如果您将此添加到您的方面:
@Before("handler(*) && args(t)")
public void enforceThrow(Throwable t) throws Throwable {
System.out.println("Let's see if we can break the jail...");
throw t;
}
然后控制台日志变成:
Let's see if we can break the jail...
100 public void de.scrum_master.app.Application.doSomething() throws java.lang.InterruptedException
Let's see if we can break the jail...
Exception in thread "main" java.lang.RuntimeException: Oops!
at de.scrum_master.app.Application.doSomething_aroundBody0(Application.java:15)
at de.scrum_master.app.Application$AjcClosure1.run(Application.java:1)
at org.aspectj.runtime.reflect.JoinPointImpl.proceed(JoinPointImpl.java:149)
at de.scrum_master.aspect.RuntimeLogger.logExecutionTime(RuntimeLogger.aj:18)
at de.scrum_master.app.Application.doSomething(Application.java:14)
at de.scrum_master.app.Application.main(Application.java:6)
这与您描述的效果非常相似。
关于Java AspectJ 异常越狱,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37881033/