Search in sources :

Example 1 with Graceful

use of org.eclipse.jetty.util.component.Graceful in project jetty.project by eclipse.

the class Server method doStop.

/* ------------------------------------------------------------ */
@Override
protected void doStop() throws Exception {
    if (isDumpBeforeStop())
        dumpStdErr();
    if (LOG.isDebugEnabled())
        LOG.debug("doStop {}", this);
    MultiException mex = new MultiException();
    // list if graceful futures
    List<Future<Void>> futures = new ArrayList<>();
    // First close the network connectors to stop accepting new connections
    for (Connector connector : _connectors) futures.add(connector.shutdown());
    // Then tell the contexts that we are shutting down
    Handler[] gracefuls = getChildHandlersByClass(Graceful.class);
    for (Handler graceful : gracefuls) futures.add(((Graceful) graceful).shutdown());
    // Shall we gracefully wait for zero connections?
    long stopTimeout = getStopTimeout();
    if (stopTimeout > 0) {
        long stop_by = System.currentTimeMillis() + stopTimeout;
        if (LOG.isDebugEnabled())
            LOG.debug("Graceful shutdown {} by ", this, new Date(stop_by));
        // Wait for shutdowns
        for (Future<Void> future : futures) {
            try {
                if (!future.isDone())
                    future.get(Math.max(1L, stop_by - System.currentTimeMillis()), TimeUnit.MILLISECONDS);
            } catch (Exception e) {
                mex.add(e);
            }
        }
    }
    // Cancel any shutdowns not done
    for (Future<Void> future : futures) if (!future.isDone())
        future.cancel(true);
    // Now stop the connectors (this will close existing connections)
    for (Connector connector : _connectors) {
        try {
            connector.stop();
        } catch (Throwable e) {
            mex.add(e);
        }
    }
    // And finally stop everything else
    try {
        super.doStop();
    } catch (Throwable e) {
        mex.add(e);
    }
    if (getStopAtShutdown())
        ShutdownThread.deregister(this);
    //Unregister the Server with the handler thread for receiving
    //remote stop commands as we are stopped already
    ShutdownMonitor.deregister(this);
    mex.ifExceptionThrow();
}
Also used : Graceful(org.eclipse.jetty.util.component.Graceful) ArrayList(java.util.ArrayList) CopyOnWriteArrayList(java.util.concurrent.CopyOnWriteArrayList) ErrorHandler(org.eclipse.jetty.server.handler.ErrorHandler) StatisticsHandler(org.eclipse.jetty.server.handler.StatisticsHandler) ContextHandler(org.eclipse.jetty.server.handler.ContextHandler) Date(java.util.Date) ServletException(javax.servlet.ServletException) MultiException(org.eclipse.jetty.util.MultiException) IOException(java.io.IOException) Future(java.util.concurrent.Future) MultiException(org.eclipse.jetty.util.MultiException)

Aggregations

IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1 CopyOnWriteArrayList (java.util.concurrent.CopyOnWriteArrayList)1 Future (java.util.concurrent.Future)1 ServletException (javax.servlet.ServletException)1 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)1 ErrorHandler (org.eclipse.jetty.server.handler.ErrorHandler)1 StatisticsHandler (org.eclipse.jetty.server.handler.StatisticsHandler)1 MultiException (org.eclipse.jetty.util.MultiException)1 Graceful (org.eclipse.jetty.util.component.Graceful)1