public void stopAsync() { // If we're in the middle of starting then go no further... for now. synchronized (this) { pendingStop = true; if (starting) { // log return; } } if (stopping.compareAndSet(false, true)) { if (context != null) { context.getStopping().set(true); } try { stopTaskRunnerFactory.execute( new Runnable() { @Override public void run() { serviceLock.writeLock().lock(); try { doStop(); } catch (Throwable e) { // LOG } finally { stopped.countDown(); serviceLock.writeLock().unlock(); } } }); } catch (Throwable t) { // LOG stopped.countDown(); } } }
public void delayedStop(final int waitTime, final String reason, Throwable cause) { if (waitTime > 0) { synchronized (this) { pendingStop = true; stopError = cause; } try { stopTaskRunnerFactory.execute( new Runnable() { @Override public void run() { try { Thread.sleep(waitTime); stopAsync(); } catch (InterruptedException e) { } } }); } catch (Throwable t) { // log error } } }
@Override protected void doStop(ServiceStopper stopper) throws Exception { if (LOG.isDebugEnabled()) { LOG.debug("Stopping transport " + this); } // Closing the streams flush the sockets before closing.. if the socket // is hung.. then this hangs the close. // closeStreams(); if (socket != null) { if (closeAsync) { // closing the socket can hang also final CountDownLatch latch = new CountDownLatch(1); // need a async task for this final TaskRunnerFactory taskRunnerFactory = new TaskRunnerFactory(); taskRunnerFactory.execute( new Runnable() { public void run() { LOG.trace("Closing socket {}", socket); try { socket.close(); LOG.debug("Closed socket {}", socket); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug( "Caught exception closing socket " + socket + ". This exception will be ignored.", e); } } finally { latch.countDown(); } } }); try { latch.await(1, TimeUnit.SECONDS); } catch (InterruptedException e) { Thread.currentThread().interrupt(); } finally { taskRunnerFactory.shutdownNow(); } } else { // close synchronously LOG.trace("Closing socket {}", socket); try { socket.close(); LOG.debug("Closed socket {}", socket); } catch (IOException e) { if (LOG.isDebugEnabled()) { LOG.debug( "Caught exception closing socket " + socket + ". This exception will be ignored.", e); } } } } }