Welcome 微信登录

首页 / 软件开发 / JAVA / AOP@Work: 用AspectJ进行性能监视,第2部分

AOP@Work: 用AspectJ进行性能监视,第2部分2011-09-07 IBM Ron Bodkin通过装载时织入使用Glassbox Inspector

简介:有了基本的面向方面的监视基础架构后,可以对它进行扩展以满足真 实 世界的监视要求。在这篇由两部分组成的文章的第二部分,Ron Bodkin 展示了 如 何在 Glassbox Inspector 中添加企业监视功能,包括监视多个应用程序、Web 服务和 Web 应用程序框架。他还展示了如何跟踪应用程序错误并在监视代码中 包 含它们,并展示了如何以编程方式部署和控制这个监视基础架构。

我在本文的 第 1 部分 中给出的基本 Glassbox Inspector 只能监视简单的 、基于 Servlet 的应用程序(如在 Duke 的 Bookstore 这个例子中),更系统 化的框架应包括对流行的 Web 应用程序框架(如 Struts 和 Spring)的支持。 如今的 Web 应用程序很少使用 Servlet 直接处理请求。这些框架通常将所有动 态页请求委派给网关 Servlet,如 Spring 框架的 DispatcherServlet,然后它 再将请求处理委派给不同的控制器。与此类似,Struts ActionServlet 委派给 作 为控制器的 Action 子类。

在这篇探索用 AspectJ 进行性能监视的文章的第二部分中,我对 Glassbox Inspector 进行扩展,通过增加监视器以提供关于应用程序性能的更有意义的信 息。这些监视器跟踪 Struts 和 Spring Web 应用程序框架的控制器之间的交互 以及 Web 服务的响应和请求操作。我还扩展了系统以支持多个应用程序,并添 加 了一个错误处理层和在运行时方便地启用和禁止监视的能力。在文章的最后展示 如何用装载时织入部署 Glassbox Inspector,以及如何测量带来的开销。

为了理解本文中的例子,需要对 Spring MVC、Struts 和 Apache Axis for Web Services 有一些了解。Glassbox Inspector 监视基础架构的完整代码请参 阅 下载。要下载运行本文中的例子所需要的 Aspectj、JMX 和 Tomcat 5 请参 阅 参考资料。

可重用的操作监视器

为了得到范围更广泛的监视功能,我将 第 1 部分的清单 5 中大多数可重用 的 Servlet 监视代码放到 AbstractOperationMonitor 方面中。很多情况下跟 踪 嵌套的请求很重要,包括跟踪在框架不同级别上的控制器以及跟踪委派的请求( 如从转发到嵌套请求)的性能。清单 1 显示了我如何扩展 Glassbox Inspector 以跟踪对操作的嵌套请求:

清单 1. 可重用的操作监视方面

public abstract aspect AbstractOperationMonitor extends AbstractRequestMonitor {

protected abstract class OperationRequestContext extends RequestContext {
/**
* Find the appropriate statistics collector object for this
* operation.
* @param operation an instance of the operation being
* monitored
*/
public PerfStats lookupStats() {
if (getParent() != null) {
// nested operation
OperationStats parentStats =
(OperationStats) getParent().getStats();
return parentStats.getOperationStats(getKey());
}
return getTopLevelStats(getKey());
}

/**
* Determine the top-level statistics for a given operation
* key. This also looks up the context name for the
* application from the operation monitor:
* @see AbstractOperationMonitor#getContextName(Object)
* For a Web application, top-level statistics are normally
* all Servlets, and the key is the Servlet name.
* @param key An object to uniquely identify the
* operation being performed.
*/
protected OperationStats getTopLevelStats(Object key) {
OperationStats stats;
synchronized(topLevelOperations) {
stats = (OperationStats)topLevelOperations.get(key);
if (stats == null) {
stats =
perfStatsFactory.createTopLevelOperationStats(key,
getContextName(controller));
topLevelOperations.put(key, stats);
}
}
return stats;
}

/**
* @return An object that uniquely identifies the operation
* being performed.
*/
protected abstract Object getKey();

/** The current controller object executing, if any. */
protected Object controller;
};
/**
* This advice stores the controller object whenever we construct a
* request context.
*/
after(Object controller) returning (OperationRequestContext ctxt) :
cflow (adviceexecution() && args(controller, ..) &&
this(AbstractOperationMonitor)) &&
call (OperationRequestContext+.new(..)) {
ctxt.controller = controller;
}
...