1

我想在特定条件下跟踪我所有的公共方法调用。但我希望这种跟踪尽可能少地损害整个系统的性能。

我需要一种方法来动态地“打开”一个切入点,以便仅为指定的线程执行,并且这种打开(和关闭)必须可以从我的代码中动态调用。

即,如果我的Java 代码发现某件事发生了,它会为它自己的线程打开一个切入点。这个切入点将记录所有公共方法调用,并且在一段时间(或一定数量的拦截)之后,切入点会自行关闭。

当然,我可以在建议中调用代码,例如“...getCurrentThread().equals(myMonitoringThread) && monitoringEnabled”,但这意味着建议将针对所有线程的所有公共方法运行,并始终执行此代码肯定会损害整个应用程序的性能。

我想要的是仅在启用的时间内为所选线程隔离性能,并使其余线程不受影响。

有人知道如何做到这一点的技术吗?

谢谢托马斯

4

1 回答 1

0

When you use aspects, your classes are affected : the bytecode is modified to be aspect compliant (or in the case of Spring, your calls are proxied with a dedicated aspect compliant class).

In the case of AspectJ it can be done during compilation, just after the compilation or at load-time (have a look here). Instrumented classes are affected the way your aspects are defined (intercept all public methods for example) and that means they can't really be de-affected at runtime (even if I'm quite sure that a crazy coder could invent a load/unload class mecanism to handle that but it would be insane ;).

So to answer your question, I think you can't fully plug/unplug your aspects at runtime in a simple way (if it's only possible !).

于 2013-11-21T10:44:39.047 回答