KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jboss > web > loadbalancer > monitor > AbstractMonitor


1 /*
2  * JBoss, the OpenSource WebOS
3  *
4  * Distributable under LGPL license.
5  * See terms of license at gnu.org.
6  */

7 package org.jboss.web.loadbalancer.monitor;
8
9 import java.io.IOException JavaDoc;
10 import java.util.ArrayList JavaDoc;
11 import javax.management.ObjectName JavaDoc;
12 import javax.servlet.http.HttpServletResponse JavaDoc;
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 /**
25  * A base class for loadbalancer-monitors.
26  *
27  * @jmx:mbean name="jboss.web.loadbalancer: service=Monitor"
28  * extends="org.jboss.system.ServiceMBean"
29  * @author Thomas Peuss <jboss@peuss.de>
30  * @version $Revision: 1.3 $
31  */

32 public abstract class AbstractMonitor
33     extends ServiceMBeanSupport
34     implements AbstractMonitorMBean, Runnable JavaDoc
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 JavaDoc schedulerObjectName;
41   protected int timeout;
42   protected String JavaDoc path;
43   protected Thread JavaDoc monitorThread;
44
45   protected void startService() throws java.lang.Exception JavaDoc
46   {
47     // create a proxy object to the scheduler service
48
scheduler = (AbstractSchedulerMBean)
49         MBeanProxyExt.create(AbstractSchedulerMBean.class,
50                              schedulerObjectName);
51
52     // start monitoring thread
53
this.setKeepRunning(true);
54     monitorThread = new Thread JavaDoc(this, "LoadbalancerMonitor");
55     monitorThread.setDaemon(true);
56     monitorThread.start();
57   }
58
59   protected void stopService() throws java.lang.Exception JavaDoc
60   {
61     this.setKeepRunning(false);
62     monitorThread.interrupt();
63     monitorThread.join();
64   }
65
66   protected void destroyService() throws java.lang.Exception JavaDoc
67   {
68     monitorThread = null;
69   }
70
71   /**
72    * Override this method to create new monitors.
73    * @param method
74    * @return
75    */

76   protected abstract boolean checkHostStatus(HttpMethod method);
77
78   protected void monitorHosts()
79   {
80     // go through down-list
81
ArrayList JavaDoc list = (ArrayList JavaDoc) 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     // go through up-list
101
list = (ArrayList JavaDoc) 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   /**
122    * Check the given host.
123    * @param url
124    * @return
125    */

126   protected boolean checkHost(Host host)
127   {
128     log.debug("Checking host " + host.getUrl() + path);
129
130     // set up HttpClient
131
HttpClient httpClient = new HttpClient();
132     httpClient.setConnectionTimeout(this.getTimeout());
133     httpClient.setTimeout(this.getTimeout());
134
135     // set up request method
136
GetMethod method = new GetMethod(host.getUrl().toExternalForm() + path);
137     method.setFollowRedirects(false);
138     method.setDoAuthentication(false);
139
140     try
141     {
142       // initiate request
143
httpClient.executeMethod(method);
144
145       // a status code >= 400 is BAD
146
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 JavaDoc 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   // Runnable interface implementation
165
public void run()
166   {
167     while (keepRunning)
168     {
169       try
170       {
171         Thread.sleep(interval);
172         monitorHosts();
173       }
174       catch (InterruptedException JavaDoc 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   /**
191    * @jmx:managed-attribute
192    */

193   public void setPath(String JavaDoc 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   /**
206    * @jmx:managed-attribute
207    */

208   public String JavaDoc getPath()
209   {
210     return "/" + path;
211   }
212
213   /**
214    * @jmx:managed-attribute
215    */

216   public void setInterval(long interval)
217   {
218     this.interval = interval;
219   }
220
221   /**
222    * @jmx:managed-attribute
223    */

224   public long getInterval()
225   {
226     return interval;
227   }
228
229   /**
230    * @jmx:managed-attribute
231    */

232   public int getTimeout()
233   {
234     return timeout;
235   }
236
237   /**
238    * @jmx:managed-attribute
239    */

240   public void setTimeout(int timeout)
241   {
242     this.timeout = timeout;
243   }
244
245   /**
246    * @jmx:managed-attribute
247    */

248   public ObjectName JavaDoc getScheduler()
249   {
250     return schedulerObjectName;
251   }
252
253   /**
254    * @jmx:managed-attribute
255    */

256   public void setScheduler(ObjectName JavaDoc schedulerObjectName)
257   {
258     this.schedulerObjectName = schedulerObjectName;
259   }
260 }
Popular Tags