diff --git a/server/src/com/cloud/cluster/ClusterServiceServletContainer.java b/server/src/com/cloud/cluster/ClusterServiceServletContainer.java index 40947465d72..b1b281484ce 100644 --- a/server/src/com/cloud/cluster/ClusterServiceServletContainer.java +++ b/server/src/com/cloud/cluster/ClusterServiceServletContainer.java @@ -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();