1 7 package org.jboss.web.loadbalancer.monitor; 8 9 import java.io.IOException ; 10 import java.util.ArrayList ; 11 import javax.management.ObjectName ; 12 import javax.servlet.http.HttpServletResponse ; 13 14 import org.apache.commons.httpclient.HttpClient; 15 import org.apache.commons.httpclient.HttpMethod; 16 import org.apache.commons.httpclient.methods.GetMethod; 17 import org.jboss.logging.Logger; 18 import org.jboss.mx.util.MBeanProxyExt; 19 import org.jboss.system.ServiceMBeanSupport; 20 import org.jboss.web.loadbalancer.scheduler.AbstractSchedulerMBean; 21 import org.jboss.web.loadbalancer.scheduler.Host; 22 import org.jboss.web.loadbalancer.util.Constants; 23 24 32 public abstract class AbstractMonitor 33 extends ServiceMBeanSupport 34 implements AbstractMonitorMBean, Runnable 35 { 36 protected boolean keepRunning = true; 37 protected long interval = 15000; 38 protected AbstractSchedulerMBean scheduler; 39 protected Logger log = Logger.getLogger(this.getClass()); 40 protected ObjectName schedulerObjectName; 41 protected int timeout; 42 protected String path; 43 protected Thread monitorThread; 44 45 protected void startService() throws java.lang.Exception 46 { 47 scheduler = (AbstractSchedulerMBean) 49 MBeanProxyExt.create(AbstractSchedulerMBean.class, 50 schedulerObjectName); 51 52 this.setKeepRunning(true); 54 monitorThread = new Thread (this, "LoadbalancerMonitor"); 55 monitorThread.setDaemon(true); 56 monitorThread.start(); 57 } 58 59 protected void stopService() throws java.lang.Exception 60 { 61 this.setKeepRunning(false); 62 monitorThread.interrupt(); 63 monitorThread.join(); 64 } 65 66 protected void destroyService() throws java.lang.Exception 67 { 68 monitorThread = null; 69 } 70 71 76 protected abstract boolean checkHostStatus(HttpMethod method); 77 78 protected void monitorHosts() 79 { 80 ArrayList list = (ArrayList ) scheduler.getHostsDown().clone(); 82 83 for (int i = 0; i < list.size(); ++i) 84 { 85 Host checkHost = (Host) list.get(i); 86 87 if (checkHost.getState()==Constants.STATE_NODE_FORCED_DOWN) 88 { 89 log.debug("Ignoring Host "+checkHost+" because it is forced down"); 90 continue; 91 } 92 93 if (checkHost(checkHost)) 94 { 95 log.info("Host " + checkHost + " is up again - adding to up list"); 96 checkHost.markNodeUp(); 97 } 98 } 99 100 list = (ArrayList ) scheduler.getHostsUp().clone(); 102 103 for (int i = 0; i < list.size(); ++i) 104 { 105 Host checkHost = (Host) list.get(i); 106 107 if (checkHost.getState()==Constants.STATE_NODE_FORCED_DOWN) 108 { 109 log.debug("Ignoring Host "+checkHost+" because it is forced down"); 110 continue; 111 } 112 113 if (!checkHost(checkHost)) 114 { 115 log.error("Host " + checkHost + " is DOWN - adding to down list"); 116 checkHost.markNodeDown(); 117 } 118 } 119 } 120 121 126 protected boolean checkHost(Host host) 127 { 128 log.debug("Checking host " + host.getUrl() + path); 129 130 HttpClient httpClient = new HttpClient(); 132 httpClient.setConnectionTimeout(this.getTimeout()); 133 httpClient.setTimeout(this.getTimeout()); 134 135 GetMethod method = new GetMethod(host.getUrl().toExternalForm() + path); 137 method.setFollowRedirects(false); 138 method.setDoAuthentication(false); 139 140 try 141 { 142 httpClient.executeMethod(method); 144 145 if (method.getStatusCode() >= HttpServletResponse.SC_BAD_REQUEST) 147 { 148 log.error("Server is up but sends error: " + method.getStatusLine()); 149 return false; 150 } 151 return checkHostStatus(method); 152 } 153 catch (IOException ex) 154 { 155 log.error("Check for host " + host.getUrl() + " failed", ex); 156 return false; 157 } 158 finally 159 { 160 method.recycle(); 161 } 162 } 163 164 public void run() 166 { 167 while (keepRunning) 168 { 169 try 170 { 171 Thread.sleep(interval); 172 monitorHosts(); 173 } 174 catch (InterruptedException ex) 175 { 176 } 177 } 178 } 179 180 public boolean isKeepRunning() 181 { 182 return keepRunning; 183 } 184 185 public void setKeepRunning(boolean keepRunning) 186 { 187 this.keepRunning = keepRunning; 188 } 189 190 193 public void setPath(String path) 194 { 195 if (path.startsWith("/")) 196 { 197 this.path = path.substring(1); 198 } 199 else 200 { 201 this.path = path; 202 } 203 } 204 205 208 public String getPath() 209 { 210 return "/" + path; 211 } 212 213 216 public void setInterval(long interval) 217 { 218 this.interval = interval; 219 } 220 221 224 public long getInterval() 225 { 226 return interval; 227 } 228 229 232 public int getTimeout() 233 { 234 return timeout; 235 } 236 237 240 public void setTimeout(int timeout) 241 { 242 this.timeout = timeout; 243 } 244 245 248 public ObjectName getScheduler() 249 { 250 return schedulerObjectName; 251 } 252 253 256 public void setScheduler(ObjectName schedulerObjectName) 257 { 258 this.schedulerObjectName = schedulerObjectName; 259 } 260 } | Popular Tags |