我有什么
我为某些特定方法设置了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
并且整个应用程序流将被破坏
问题
如何正确处理异常以便记录它们与进行所有测量业务一起发生并且不允许它们进行越狱?
答案 0 :(得分:1)
就像Nándor一样,在尝试复制你的情况时它对我有用,即使是LTW也是如此。这是一个独立的例子:
Java驱动程序应用程序:
if(sqLite!=null && sqLite.IsOpen() )
sqLite.Close();
<强>方面:强>
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;
}
}
前面有人在StackOverflow上有人想要一种方法来生成某种无法捕获的“查克诺里斯例外”,我使用AspectJ为他创建了一个here。所以,只是猜测一下,你可能在你的代码中的任何地方有另一个方面或建议(重新)从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!
建议中抛出有问题的异常吗?例如,如果您将此添加到您的方面:
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;
}
这与你描述的效果非常相似。