代码清单2: SiteMonitor.java

01 package devtrends.jmxping;
02 
03 import java.io.IOException;
04 import java.net.HttpURLConnection;
05 import java.net.MalformedURLException;
06 import java.net.URL;
07 
08 public class SiteMonitor implements SiteMonitorMBean, Runnable  {
09 
10   // Managed properties
11   private String address     = "";
12   private int interval       = 10; // in seconds
13   private String status      = "UNKNOWN::HTTP monitor not running.";
14   private boolean running    = false;
15   
16   // Application variables
17   private boolean stop = true; // Flag to stop the thread
18   Thread monitor = null; // The thread
19   
20   public String getSiteAddress() {
21     return address;
22   }
23 
24   public void setSiteAddress(String address) {
25     this.address = address;
26     if (isRunning()) {
27       monitor.interrupt(); // wake thread from slumber
28     }
29   }
30 
31   public int getPingInterval() {
32     return interval;
33   }
34 
35   public void setPingInterval(int interval) {
36     if (interval < 0) {
37       throw new IllegalArgumentException("interval must be positive.");
38     }
39     this.interval = interval;
40   }
41 
42   public String getSiteStatus() {
43     return status;
44   }
45 
46   public boolean isRunning() {
47     return running;
48   }
49 
50   public void start() {
51     if (isRunning()) {
52       throw new IllegalStateException("Can't start, already running.");
53     }
54     if (address == "")  {
55       throw new IllegalStateException("Can't start when address is unspecified.");
56     }
57     System.out.println("monitor starting...");
58     this.stop = false;
59     monitor = new Thread(this);
60     monitor.start();
61   }
62 
63   public void stop() {
64     if (!isRunning()) {
65       throw new IllegalStateException("Can't stop, not running.");
66     }
67     System.out.println("monitor stopping...");
68     this.stop = true;
69     monitor.interrupt();
70   }
71   
72   public void run() {
73   
74     int responseCode;
75     String responseMessage;
76     URL url;
77     HttpURLConnection conn = null;
78 
79       while( !stop ) {
80         this.running = true;
81         try {
82           Thread.sleep(getPingInterval() * 1000);
83         }
84         catch (InterruptedException e) {
85           System.out.println("Sleep interrupted.");
86           if (stop) continue;
87         }
88 
89         // Attempt to create the URL object
90         try {
91           url = new URL(getSiteAddress());
92         }
93         catch (MalformedURLException e) {
94           status = "UNKNOWN::Malformed URL.";
95           System.err.println(e.toString());
96           continue;     
97         }
98         
99         // If the protocol is not HTTP, no point in continuing
100         if (!url.getProtocol().equalsIgnoreCase("http")) {
101           status = "UNKNOWN::Protocol in URL is not HTTP.";
102           continue;
103         }
104         
105         try {
106           // Obtain HTTP connection and set properties
107           conn = (HttpURLConnection)url.openConnection();
108           conn.setRequestMethod("HEAD");
109           conn.setFollowRedirects(false);
110           
111           // Connect to URL and get response
112           conn.connect();
113           responseCode = conn.getResponseCode();
114           responseMessage = conn.getResponseMessage();
115           
116           // Decode the response
117           if (responseCode == -1 || responseMessage == null) {
118             status = "UNKNOWN::Bad response, not HTTP.";
119           } else {
120             if (responseCode >= 200 && responseCode < 400) {
121             status = "OK";
122             } else if (responseCode >= 400 && responseCode < 500) {
123               status = "WARNING";
124             } else {
125               status = "ERROR";
126             }
127             status = status + ":" + Integer.toString(responseCode) + ":" + responseMessage;
128             System.out.println(getSiteAddress() + "==>" + status);
129           }
130           
131         }
132         catch (IOException e) {
133           status = "UNKNOWN::IOException:" + e.toString();
134           System.err.println(e.toString());
135         }
136         finally {
137           conn.disconnect();
138         }
139         
140       } //while
141 
142     running = false;
143     status = "UNKNOWN::HTTP monitor not running.";
144   }
145 }

E-mail this page