KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > mx4j > tools > stats > TimedStatisticsRecorder


1 /*
2  * Copyright (C) The MX4J Contributors.
3  * All rights reserved.
4  *
5  * This software is distributed under the terms of the MX4J License version 1.0.
6  * See the terms of the MX4J License in the documentation provided with this software.
7  */

8
9 package mx4j.tools.stats;
10
11 import java.util.Date JavaDoc;
12 import java.util.Timer JavaDoc;
13 import java.util.TimerTask JavaDoc;
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 implements TimedStatisticsRecorderMBean
22 {
23    protected boolean registered = false;
24
25    protected static Timer JavaDoc timer = new Timer JavaDoc();
26
27    protected CollectTask task = new CollectTask();
28
29    protected long granularity = 1000L;
30
31    public TimedStatisticsRecorder()
32    {
33    }
34
35    public void setGranularity(long granularity)
36    {
37       this.granularity = granularity;
38    }
39
40    public long getGranularity()
41    {
42       return granularity;
43    }
44
45    public String JavaDoc toString()
46    {
47       return "TimedStatisticsRecorder";
48    }
49
50    protected synchronized void startObserving() throws Exception JavaDoc
51    {
52       task = new CollectTask();
53       timer.schedule(task, 0, granularity);
54    }
55
56    protected synchronized void stopObserving() throws Exception JavaDoc
57    {
58       task.cancel();
59    }
60
61    private class CollectTask extends TimerTask JavaDoc
62    {
63       public void run()
64       {
65          try
66          {
67             Number JavaDoc value = (Number JavaDoc)server.getAttribute(observedName, observedAttribute);
68             addEntry(new Date JavaDoc(), value);
69          }
70          catch (Exception JavaDoc e)
71          {
72             getLogger().error(new StringBuffer JavaDoc(" Exception reading attribute ").append(observedAttribute).append(" of MBean ").append(observedName).toString(), e);
73          }
74       }
75    }
76
77 }
78
Popular Tags