0
How to check if server is up and responding
Posted by sanjay
on
3:33 PM
in
java
// todo launch server
Thread executeThread = new Thread(this);
executeThread.setPriority(Thread.NORM_PRIORITY);
executeThread.start();
int port = 8080
String host = "localhost";
int sleepCount = 0;
boolean doLoop = true;
while (doLoop) {
try {
logEvent(this,Log.DBG,"..Creating Socket ");
Socket sock = new Socket (host,port);
logEvent(this,Log.DBG,"..Connected to Server");
// test code ...
PrintWriter out = new PrintWriter (sock.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.println("GET /index.jsp HTTP/1.1");
out.println("Host: " + host + ":" + port);
out.println("Connection: Close");
out.println();
while (doLoop){
if (in.ready()){
sock.close();
doLoop = false;
break;
} else {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
logEvent(this,Log.WARNING,"..Unable to connect");
if (sleepCount==120) {
logEvent(this,Log.WARNING,".. proceeding without connection to " + host + " at " + port);
break;
}
logEvent(this,Log.DBG,"..About to sleep for 5 seconds");
try {
Thread.sleep(5000);
}catch (InterruptedException ie) {
logEvent(this,Log.WARNING,"..InterruptedException on main thread:" + ie);
}
logEvent(this,Log.DBG,"..Waking up after 5 seconds");
sleepCount++;
}
}
try {
// sleep 90 seconds for the server to finish startup
Thread.sleep(1000);
} catch (InterruptedException e ) {
logEvent(this,Log.DBG,e.toString());
}
logEvent(this,Log.DBG,"..server is up");
return true;
}
public void run() {
String startupScript = hubDeployer.getStartupScript();
String[] args = null;
String logFolder = logFolder
try {
ps = new ProcessExec(SHELL_COMMAND, args, outputFile, outputFile);
ps.executeProcess();
} catch (Exception e) {
logEvent(this, Log.ERROR, "Failed to execute " + installScript);
logEvent(this, Log.ERROR, e);
}
}
}
Thread executeThread = new Thread(this);
executeThread.setPriority(Thread.NORM_PRIORITY);
executeThread.start();
int port = 8080
String host = "localhost";
int sleepCount = 0;
boolean doLoop = true;
while (doLoop) {
try {
logEvent(this,Log.DBG,"..Creating Socket ");
Socket sock = new Socket (host,port);
logEvent(this,Log.DBG,"..Connected to Server");
// test code ...
PrintWriter out = new PrintWriter (sock.getOutputStream(),true);
BufferedReader in = new BufferedReader(new InputStreamReader(sock.getInputStream()));
out.println("GET /index.jsp HTTP/1.1");
out.println("Host: " + host + ":" + port);
out.println("Connection: Close");
out.println();
while (doLoop){
if (in.ready()){
sock.close();
doLoop = false;
break;
} else {
try {
Thread.currentThread().sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
} catch (IOException e) {
logEvent(this,Log.WARNING,"..Unable to connect");
if (sleepCount==120) {
logEvent(this,Log.WARNING,".. proceeding without connection to " + host + " at " + port);
break;
}
logEvent(this,Log.DBG,"..About to sleep for 5 seconds");
try {
Thread.sleep(5000);
}catch (InterruptedException ie) {
logEvent(this,Log.WARNING,"..InterruptedException on main thread:" + ie);
}
logEvent(this,Log.DBG,"..Waking up after 5 seconds");
sleepCount++;
}
}
try {
// sleep 90 seconds for the server to finish startup
Thread.sleep(1000);
} catch (InterruptedException e ) {
logEvent(this,Log.DBG,e.toString());
}
logEvent(this,Log.DBG,"..server is up");
return true;
}
public void run() {
String startupScript = hubDeployer.getStartupScript();
String[] args = null;
String logFolder = logFolder
try {
ps = new ProcessExec(SHELL_COMMAND, args, outputFile, outputFile);
ps.executeProcess();
} catch (Exception e) {
logEvent(this, Log.ERROR, "Failed to execute " + installScript);
logEvent(this, Log.ERROR, e);
}
}
}