开发者

AspectJ - Doubt

开发者 https://www.devze.com 2023-03-13 03:56 出处:网络
An aspect can be used to measure the performance of method invocations, as illustrated in the example below:

An aspect can be used to measure the performance of method invocations, as illustrated in the example below:

public aspect MonitorRequests {
  void around() : monitoredRequestO {
    PerfStats stats = getPerfStats(thisDoinPointStaticPart);
    long start = System-currentTimeMillisO;
    proceedO;
    stats.ecunter++;
    stats.time += System.currentTimeMillisC)-start;
  }
  pointcut monitoredRequestO :
    execution(void HttpServ1et.do*(..)) && if(enabled);
    // can expose stats via JMX, dump method, getstats etc.
  public static class PerfStats { _. }
  private Map<StaticPart,PerfStats> perfStatMap • //...
  private boolean enabled;
}

By default, an aspect instance is associated with the Java Virtual Machine, rather with specific execution flows, similar to a static class. Another aspect below uses percflow() to associate an aspect instance differently from the default:

public aspect MonitorDatabaseRequests
  percflow(monitoredRequest() && !cflowbelow(mon-5toredRequest()) {
  void around() : monitor开发者_如何学编程edRequestO {
    PerfStats stats = getPerfStats(thisJoinPointStaticPart);
    long time.= System.currentTimeMi 11 i s O ;
    proceed();
    stats.counter++;
    stats.databaseTime += accumulatedoatabaseTime;
    stats.time 4= System.currentTimeMi 11 isO-time;
  }
}

What is the difference that adding the percflow() declaration makes in this example

I'm confused how percflow works and how this is different from not using it....


percflow is the aspect instantiation model. See here: http://eclipse.org/aspectj/doc/released/progguide/quick-aspectAssociations.html

This means that one instance of this aspect is created for every cflow entered.

The first aspect is a singleton and so it must store a map for all of the performance stats it keeps track of. The second aspect is instantiated as needed, so performance stats are implicitly stored and associated with the proper dynamic call graph.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号