bug 14618: Protect servlet container from shutting down on unexpected runtime exceptions

Reviewed-By: Kelven
This commit is contained in:
Kelven Yang 2012-04-04 16:52:51 -07:00
parent 93a5b7c0bc
commit c112088fc5
1 changed files with 25 additions and 15 deletions

View File

@ -13,7 +13,6 @@
package com.cloud.cluster;
import java.io.IOException;
import java.io.InterruptedIOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
@ -45,7 +44,7 @@ import com.cloud.utils.concurrency.NamedThreadFactory;
public class ClusterServiceServletContainer {
private static final Logger s_logger = Logger.getLogger(ClusterServiceServletContainer.class);
private ListenerThread listenerThread;
private ListenerThread listenerThread;
public ClusterServiceServletContainer() {
}
@ -58,17 +57,15 @@ public class ClusterServiceServletContainer {
return true;
}
@SuppressWarnings("deprecation")
public void stop() {
if(listenerThread != null) {
listenerThread.interrupt();
listenerThread.stop();
public void stop() {
if(listenerThread != null) {
listenerThread.stopRunning();
}
}
static class ListenerThread extends Thread {
private HttpService _httpService = null;
private ServerSocket _serverSocket = null;
private volatile ServerSocket _serverSocket = null;
private HttpParams _params = null;
private ExecutorService _executor;
@ -105,13 +102,23 @@ public class ClusterServiceServletContainer {
_httpService = new HttpService(httpproc, new DefaultConnectionReuseStrategy(), new DefaultHttpResponseFactory());
_httpService.setParams(_params);
_httpService.setHandlerResolver(reqistry);
}
public void stopRunning() {
if(_serverSocket != null) {
try {
_serverSocket.close();
} catch (IOException e) {
}
_serverSocket = null;
}
}
public void run() {
if(s_logger.isInfoEnabled())
s_logger.info("Cluster service servlet container listening on port " + _serverSocket.getLocalPort());
while (!Thread.interrupted()) {
while (_serverSocket != null) {
try {
// Set up HTTP connection
Socket socket = _serverSocket.accept();
@ -149,12 +156,15 @@ public class ClusterServiceServletContainer {
}
});
} catch (InterruptedIOException ex) {
break;
} catch (IOException e) {
s_logger.error("Exception when initializing cluster service servlet container : ", e);
break;
}
} catch (Throwable e) {
s_logger.error("Unexpected exception ", e);
// back off to avoid spinning if the exception condition keeps coming back
try {
Thread.sleep(1000);
} catch (InterruptedException e1) {
}
}
}
_executor.shutdown();