快速业务通道

AOP@Work: 用AspectJ进行性能监视,第1部分 - 编程入门网

作者 佚名技术 来源 NET编程 浏览 发布时间 2012-06-15
a 发行版的完整最终代码。

基本的系统

我将从 一 个基本的面向方面的性能监视系统开始。这个系统可以捕捉处理 Web 请求的不 同 servlet 的时间和计数。清单 1 显示了一个捕捉这个性能信息的简单方面:

AOP@Work: 用AspectJ进行性能监视,第1部分(3)

时间:2011-09-07 IBM Ron Bodkin

清单 1. 捕捉 servlet 时间和计数的方面

/** * Monitors performance timing and execution counts for * <code>HttpServlet</code> operations */ public aspect HttpServletMonitor {  /** Execution of any Servlet request methods. */  public pointcut monitoredOperation(Object operation) :   execution(void HttpServlet.do*(..)) && this (operation);  /** Advice that records statistics for each monitored operation. */  void around(Object operation) : monitoredOperation(operation) {    long start = getTime();    proceed(operation);    PerfStats stats = lookupStats (operation);    stats.recordExecution(getTime(), start);  }  /**   * Find the appropriate statistics collector object for this   * operation.   *   * @param operation   *      the instance of the operation being monitored   */  protected PerfStats lookupStats(Object operation) {    Class keyClass = operation.getClass();     synchronized(operations) {      stats = (PerfStats) operations.get(keyClass);      if (stats == null) {                  stats = perfStatsFactory.           createTopLevelOperationStats(HttpServlet.class,               keyClass);        operations.put(keyClass, stats);      }    }    return stats;  }  /**   * Helper method to collect time in milliseconds. Could plug in   * nanotimer.   */  public long getTime() {    return System.currentTimeMillis();  }  public void setPerfStatsFactory(PerfStatsFactory    perfStatsFactory) {    this.perfStatsFactory = perfStatsFactory;  }  public PerfStatsFactory getPerfStatsFactory() {    return perfStatsFactory;  }  /** Track top-level operations. */  private Map/*<Class,PerfStats>*/ operations =   new WeakIdentityHashMap();  private PerfStatsFactory perfStatsFactory; } /** * Holds summary performance statistics for a * given topic of interest * (e.g., a subclass of Servlet). */ public interface PerfStats {   /**   * Record that a single execution occurred.   *   * @param start time in milliseconds   * @param end time in milliseconds   */  void recordExecution(long start, long end);  /**   * Reset these statistics back to zero. Useful to track statistics   * during an interval.   */   void reset();  /**   * @return total accumulated time in milliseconds from all   *     executions (since last reset).   */  int getAccumulatedTime();  /**   * @return the largest time for any single execution, in   *       milliseconds (since last reset).   */  int getMaxTime ();  /**   * @return the number of executions recorded (since last reset).   */  int getCount(); } /** * Implementation of the * * @link

凌众科技专业提供服务器租用、服务器托管、企业邮局、虚拟主机等服务,公司网站:http://www.lingzhong.cn 为了给广大客户了解更多的技术信息,本技术文章收集来源于网络,凌众科技尊重文章作者的版权,如果有涉及你的版权有必要删除你的文章,请和我们联系。以上信息与文章正文是不可分割的一部分,如果您要转载本文章,请保留以上信息,谢谢!

分享到: 更多

Copyright ©1999-2011 厦门凌众科技有限公司 厦门优通互联科技开发有限公司 All rights reserved

地址(ADD):厦门软件园二期望海路63号701E(东南融通旁) 邮编(ZIP):361008

电话:0592-5908028 传真:0592-5908039 咨询信箱:web@lingzhong.cn 咨询OICQ:173723134

《中华人民共和国增值电信业务经营许可证》闽B2-20100024  ICP备案:闽ICP备05037997号