01: /*
02: * Copyright (C) The MX4J Contributors.
03: * All rights reserved.
04: *
05: * This software is distributed under the terms of the MX4J License version 1.0.
06: * See the terms of the MX4J License in the documentation provided with this software.
07: */
08:
09: package mx4j.tools.stats;
10:
11: import java.util.Date;
12: import java.util.Timer;
13: import java.util.TimerTask;
14:
15: /**
16: * TimedStatisticsRecorder records statistics of an attribute
17: * with a timer polling the value every certain interval
18: *
19: * @version $Revision: 1.4 $
20: */
21: public class TimedStatisticsRecorder extends ObserverStatisticsRecorder
22: implements TimedStatisticsRecorderMBean {
23: protected boolean registered = false;
24:
25: protected static Timer timer = new Timer();
26:
27: protected CollectTask task = new CollectTask();
28:
29: protected long granularity = 1000L;
30:
31: public TimedStatisticsRecorder() {
32: }
33:
34: public void setGranularity(long granularity) {
35: this .granularity = granularity;
36: }
37:
38: public long getGranularity() {
39: return granularity;
40: }
41:
42: public String toString() {
43: return "TimedStatisticsRecorder";
44: }
45:
46: protected synchronized void startObserving() throws Exception {
47: task = new CollectTask();
48: timer.schedule(task, 0, granularity);
49: }
50:
51: protected synchronized void stopObserving() throws Exception {
52: task.cancel();
53: }
54:
55: private class CollectTask extends TimerTask {
56: public void run() {
57: try {
58: Number value = (Number) server.getAttribute(
59: observedName, observedAttribute);
60: addEntry(new Date(), value);
61: } catch (Exception e) {
62: getLogger().error(
63: new StringBuffer(
64: " Exception reading attribute ")
65: .append(observedAttribute).append(
66: " of MBean ").append(
67: observedName).toString(), e);
68: }
69: }
70: }
71:
72: }
|