1 22 package org.jboss.monitor; 23 24 import org.jboss.util.NestedRuntimeException; 25 import org.jboss.logging.Logger; 26 import org.jboss.system.ServiceMBeanSupport; 27 28 import java.util.List ; 29 30 import javax.management.ObjectName ; 31 import javax.management.MBeanServer ; 32 import javax.management.MBeanRegistration ; 33 import java.util.ArrayList ; 34 import java.util.Date ; 35 import java.util.LinkedList ; 36 37 44 public class SnapshotRecordingMonitor implements Runnable , SnapshotRecordingMonitorMBean, MBeanRegistration 45 { 46 protected Logger log; 47 protected String monitorName; 48 protected ObjectName observedObject; 49 protected String attribute; 50 protected boolean recording; 51 protected long period; 52 protected ArrayList history; 53 protected long startTime; 54 protected long endTime; 55 protected MBeanServer mbeanServer; 56 57 public SnapshotRecordingMonitor() 58 { 59 log = Logger.getLogger(monitorName); 60 history = new ArrayList (100); 61 } 62 63 protected void startMonitorThread() 64 { 65 Thread t = new Thread (this, "JBoss JMX Attribute Snapshot " + monitorName); 66 t.start(); 67 } 68 69 public String getMonitorName() 70 { 71 return monitorName; 72 } 73 74 public void setMonitorName(String name) 75 { 76 monitorName = name; 77 } 78 79 public ObjectName getObservedObject() 80 { 81 return observedObject; 82 } 83 84 public void setObservedObject(ObjectName oname) 85 { 86 this.observedObject = oname; 87 } 88 89 public String getObservedAttribute() 90 { 91 return attribute; 92 } 93 94 public void setObservedAttribute(String attr) 95 { 96 attribute = attr; 97 } 98 99 public boolean isRecording() { return recording; } 100 public void setRecording(boolean start) 101 { 102 if (start == recording) return; 103 recording = start; 104 105 if (start) 106 { 107 startMonitorThread(); 108 } 109 } 110 111 public long getPeriod() 112 { 113 return period; 114 } 115 116 public void setPeriod(long period) 117 { 118 this.period = period; 119 } 120 121 public ArrayList getData() 122 { 123 return history; 124 } 125 126 public void clearData() 127 { 128 history.clear(); 129 } 130 131 public void startSnapshot() 132 { 133 history.clear(); 134 setRecording(true); 135 } 136 137 public void endSnapshot() 138 { 139 recording = false; 140 } 141 142 public long getStartTime() 143 { 144 return startTime; 145 } 146 147 public long getEndTime() 148 { 149 return endTime; 150 } 151 152 public void run() 153 { 154 startTime = System.currentTimeMillis(); 155 while (recording) 156 { 157 try 158 { 159 Object value = mbeanServer.getAttribute(observedObject, attribute); 160 history.add(value); 161 endTime = System.currentTimeMillis(); 162 } 163 catch (Exception ex) 164 { 165 log.error(monitorName + " had error while monitoring", ex); 166 } 167 if (recording) 168 { 169 try 170 { 171 Thread.sleep(period); 172 } 173 catch (InterruptedException ignored) 174 { 175 } 176 } 177 } 178 } 179 180 182 public ObjectName preRegister(MBeanServer server, ObjectName objectName) 183 throws Exception 184 { 185 mbeanServer = server; 186 return objectName; 187 } 188 189 public void postRegister(Boolean registrationDone) 190 { 191 } 192 193 public void preDeregister() 194 throws Exception 195 { 196 } 197 198 public void postDeregister() 199 { 200 } 201 202 203 } 204 | Popular Tags |