KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > springframework > samples > petclinic > jmx > CallMonitoringInterceptor


1 package org.springframework.samples.petclinic.jmx;
2
3 import org.aopalliance.intercept.MethodInterceptor;
4 import org.aopalliance.intercept.MethodInvocation;
5
6 import org.springframework.util.StopWatch;
7
8 /**
9  * Simple interceptor that monitors call count and call invocation time.
10  * Implements the CallMonitor management interface.
11  *
12  * @author Rob Harrop
13  * @author Juergen Hoeller
14  * @since 1.2
15  */

16 public class CallMonitoringInterceptor implements CallMonitor, MethodInterceptor {
17
18     private boolean isEnabled = true;
19
20     private int callCount = 0;
21
22     private long accumulatedCallTime = 0;
23
24
25     public void setEnabled(boolean enabled) {
26         isEnabled = enabled;
27     }
28
29     public boolean isEnabled() {
30         return isEnabled;
31     }
32
33     public void reset() {
34         this.callCount = 0;
35         this.accumulatedCallTime = 0;
36     }
37
38     public int getCallCount() {
39         return callCount;
40     }
41
42     public long getCallTime() {
43         return (this.callCount > 0 ? this.accumulatedCallTime / this.callCount : 0);
44     }
45
46
47     public Object JavaDoc invoke(MethodInvocation invocation) throws Throwable JavaDoc {
48         if (this.isEnabled) {
49             this.callCount++;
50
51             StopWatch sw = new StopWatch(invocation.getMethod().getName());
52
53             sw.start("invoke");
54             Object JavaDoc retVal = invocation.proceed();
55             sw.stop();
56
57             this.accumulatedCallTime += sw.getTotalTimeMillis();
58             return retVal;
59         }
60
61         else {
62             return invocation.proceed();
63         }
64     }
65
66 }
67
Popular Tags