1 8 9 package mx4j.tools.stats; 10 11 import java.util.Date ; 12 import java.util.Timer ; 13 import java.util.TimerTask ; 14 15 21 public class TimedStatisticsRecorder extends ObserverStatisticsRecorder implements TimedStatisticsRecorderMBean 22 { 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 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 toString() 46 { 47 return "TimedStatisticsRecorder"; 48 } 49 50 protected synchronized void startObserving() throws Exception 51 { 52 task = new CollectTask(); 53 timer.schedule(task, 0, granularity); 54 } 55 56 protected synchronized void stopObserving() throws Exception 57 { 58 task.cancel(); 59 } 60 61 private class CollectTask extends TimerTask 62 { 63 public void run() 64 { 65 try 66 { 67 Number value = (Number )server.getAttribute(observedName, observedAttribute); 68 addEntry(new Date (), value); 69 } 70 catch (Exception e) 71 { 72 getLogger().error(new StringBuffer (" Exception reading attribute ").append(observedAttribute).append(" of MBean ").append(observedName).toString(), e); 73 } 74 } 75 } 76 77 } 78 | Popular Tags |