1 22 package org.jboss.monitor; 23 24 import org.jboss.system.ServiceMBeanSupport; 25 import org.jboss.logging.Logger; 26 27 import javax.management.ObjectName ; 28 import java.util.ArrayList ; 29 30 37 public abstract class JBossMonitor extends ServiceMBeanSupport implements Runnable , JBossMonitorMBean 38 { 39 protected Logger log; 40 protected String monitorName; 41 protected ObjectName observedObject; 42 protected String attribute; 43 protected boolean enabled; 44 protected boolean alertSent = false; 45 protected long period; 46 protected ArrayList alertListeners = null; 47 protected String thresholdString; 48 protected Object triggeredAttributeValue; 49 protected long triggerTime; 50 51 protected void startService 52 () throws Exception 53 { 54 super.startService(); 55 log = Logger.getLogger(monitorName); 56 if (alertListeners != null) 57 { 58 for (int i = 0; i < alertListeners.size(); i++) 59 { 60 ObjectName aname = (ObjectName )alertListeners.get(i); 61 getServer().addNotificationListener(getServiceName(), aname, null, null); 62 } 63 } 64 if (enabled) 65 { 66 startMonitorThread(); 67 } 68 } 69 70 protected void stopService() 71 { 72 enabled = false; } 74 75 protected void startMonitorThread() 76 { 77 Thread t = new Thread (this, "JBoss JMX Attribute Monitor " + monitorName); 78 t.start(); 79 } 80 81 protected abstract void testThreshold(); 82 83 public String getMonitorName() 84 { 85 return monitorName; 86 } 87 88 public void setMonitorName(String name) 89 { 90 monitorName = name; 91 } 92 93 public ObjectName getObservedObject() 94 { 95 return observedObject; 96 } 97 98 public void setObservedObject(ObjectName oname) 99 { 100 this.observedObject = oname; 101 } 102 103 public String getObservedAttribute() 104 { 105 return attribute; 106 } 107 108 public void setObservedAttribute(String attr) 109 { 110 attribute = attr; 111 } 112 113 public boolean alerted() 114 { 115 return alertSent; 116 } 117 118 public void clearAlert() 119 { 120 alertSent = false; 121 triggeredAttributeValue = null; 122 triggerTime = 0; 123 } 124 125 public boolean getEnabled() 126 { 127 return enabled; 128 } 129 130 public void setEnabled(boolean start) 131 { 132 if (start == enabled) return; 133 enabled = start; 134 135 if (start && getState() == STARTED) 138 { 139 startMonitorThread(); 140 } 141 } 142 143 public long getPeriod() 144 { 145 return period; 146 } 147 148 public void setPeriod(long period) 149 { 150 this.period = period; 151 } 152 153 public ArrayList getAlertListeners() 154 { 155 return alertListeners; 156 } 157 158 public void setAlertListeners(ArrayList listeners) 159 { 160 if (alertListeners != null && getState() == STARTED) 161 { 162 ArrayList copy = new ArrayList (listeners); 164 for (int i = 0; i < alertListeners.size(); i++) 165 { 166 ObjectName oname = (ObjectName )alertListeners.get(i); 167 int idx = copy.indexOf(oname); 168 if (idx == -1) 169 { 170 try 171 { 172 getServer().removeNotificationListener(getServiceName(), oname); 173 } 174 catch (Exception ex) 175 { 176 getLog().warn("failed to remove listener", ex); 177 } 178 } 179 else 180 { 181 copy.remove(idx); 182 } 183 } 184 for (int i = 0; i < copy.size(); i++) 186 { 187 ObjectName aname = (ObjectName )copy.get(i); 188 try 189 { 190 getServer().addNotificationListener(getServiceName(), aname, null, null); 191 } 192 catch (Exception ex) 193 { 194 getLog().warn("failed to remove listener", ex); 195 } 196 } 197 } 198 alertListeners = listeners; 199 } 200 201 public Object getTriggeredAttributeValue() 202 { 203 return triggeredAttributeValue; 204 } 205 206 public long getTriggerTime() 207 { 208 return triggerTime; 209 } 210 211 public void run() 212 { 213 while (this.getState() == STARTED || this.getState() == STARTING) 214 { 215 if (enabled) 216 { 217 try 218 { 219 testThreshold(); 220 } 221 catch (Exception ex) 222 { 223 log.error(monitorName + " had error while monitoring", ex); 224 } 225 } 226 try 227 { 228 Thread.sleep(period); 229 } 230 catch (InterruptedException ignored) 231 { 232 } 233 } 234 } 235 236 public String getThreshold() 237 { 238 return thresholdString; 239 } 240 241 public void setThreshold(String val) 242 { 243 thresholdString = val; 244 } 245 } 246 | Popular Tags |