Using the Framework
Say you want to monitor the Apache Web server process and re-launch it if it goes down. To do so, create a ProcessInfo class that implements the Info interface. Code the
isOk() method to iterate through the list of processes and check if the
apache.exe process is running, and then code the
process() method to launch the
apache.exe:
public class ProcessInfo implements Info
{
ProcessInfo(String processName, String execName){ ..}
//check if process is running or not
boolean isOk() throws Exception {..}
//re-launch process
boolean process() {..};
}
Currently, no API-level support exists in Java for obtaining a list of processes and disk usage, CPU load, network connectivity, etc. For my project, I developed these functions in C for each OS and provided JNI wrappers so that the Java code could access them. Although very efficient, this solution is a bit risky as the JNI calls are executed in the context of the JVM process.
Alternatively, you could have a separate process for accessing native functionality. The sample client provided with the code download includes a Win32 application CheckProcess.exe that takes the process name as a command-line parameter and writes out TRUE or FALSE on to the stdout. The isOk() method of ProcessInfo executes CheckProcess.exe and reads the status from the process's input stream:
public boolean isOk() throws Exception {
String[] cmdArray = new String[]
{“CheckProcess.exe”, “apache.exe”};
Process proc = Runtime.getRuntime().exec(cmdArray);
BufferedReader br = new BufferedReader(new
InputStreamReader(proc.getInputStream()));
String status = br.readLine();
if(status.equals("TRUE")) return true;
return false;
}
Next, implement a TestClient that configures the ProcessInfo object and starts the framework:
public class TestClient {
public static void main(String[] args)
throws Exception {
ProcessInfo p = new ProcessInfo(“apache.exe”,
“<Path>/apache.exe”);
int counter = 10; //10 seconds
byte criticality = LWMFramework.HIGH;
LWMFramework lwmf = new LWMFramework();
//set minimum monitorable frequency 1 second
lwmf.setMonitorInterval(1);
//add info object
lwmf.add(p, counter, level);
//start the framework
lwmf.start();
Thread.currentThread().join();
}
}