Class ShutdownHandler

  • All Implemented Interfaces:
    Handler, HandlerContainer, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.LifeCycle

    public class ShutdownHandler
    extends HandlerWrapper
    A handler that shuts the server down on a valid request. Used to do "soft" restarts from Java. If _exitJvm is set to true a hard System.exit() call is being made. If _sendShutdownAtStart is set to true, starting the server will try to shut down an existing server at the same port. If _sendShutdownAtStart is set to true, make an http call to "http://localhost:" + port + "/shutdown?token=" + shutdownCookie in order to shut down the server. This handler is a contribution from Johannes Brodwall: https://bugs.eclipse.org/bugs/show_bug.cgi?id=357687 Usage:
     Server server = new Server(8080);
     HandlerList handlers = new HandlerList();
     handlers.setHandlers(new Handler[]
     { someOtherHandler, new ShutdownHandler("secret password", false, true) });
     server.setHandler(handlers);
     server.start();
     
     public static void attemptShutdown(int port, String shutdownCookie) {
     try {
     URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);
     HttpURLConnection connection = (HttpURLConnection)url.openConnection();
     connection.setRequestMethod("POST");
     connection.getResponseCode();
     logger.info("Shutting down " + url + ": " + connection.getResponseMessage());
     } catch (SocketException e) {
     logger.debug("Not running");
     // Okay - the server is not running
     } catch (IOException e) {
     throw new RuntimeException(e);
     }
     }
     
    • Nested Class Summary

      • Nested classes/interfaces inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        org.eclipse.jetty.util.component.AbstractLifeCycle.AbstractLifeCycleListener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.Container

        org.eclipse.jetty.util.component.Container.InheritedListener, org.eclipse.jetty.util.component.Container.Listener
      • Nested classes/interfaces inherited from interface org.eclipse.jetty.util.component.LifeCycle

        org.eclipse.jetty.util.component.LifeCycle.Listener
    • Field Summary

      • Fields inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
      • Fields inherited from interface org.eclipse.jetty.util.component.Dumpable

        KEY
    • Constructor Summary

      Constructors 
      Constructor Description
      ShutdownHandler​(java.lang.String shutdownToken)  
      ShutdownHandler​(java.lang.String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)  
      ShutdownHandler​(Server server, java.lang.String shutdownToken)
      Deprecated.
    • Method Summary

      All Methods Instance Methods Concrete Methods 
      Modifier and Type Method Description
      protected void doShutdown​(Request baseRequest, javax.servlet.http.HttpServletResponse response)  
      protected void doStart()  
      java.lang.String getShutdownToken()  
      void handle​(java.lang.String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response)
      Handle a request.
      boolean isExitJvm()  
      boolean isSendShutdownAtStart()  
      void sendShutdown()  
      void setExitJvm​(boolean exitJvm)  
      void setSendShutdownAtStart​(boolean sendShutdownAtStart)  
      • Methods inherited from class org.eclipse.jetty.util.component.ContainerLifeCycle

        addBean, addBean, addEventListener, addManaged, contains, dump, dump, dump, dump, dump, dumpBeans, dumpObject, dumpObjects, dumpStdErr, dumpThis, getBean, getBeans, getBeans, getContainedBeans, getContainedBeans, isAuto, isManaged, isUnmanaged, manage, removeBean, removeBeans, removeEventListener, setBeans, setStopTimeout, start, stop, unmanage, updateBean, updateBean, updateBeans
      • Methods inherited from class org.eclipse.jetty.util.component.AbstractLifeCycle

        addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop, toString
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, wait
      • Methods inherited from interface org.eclipse.jetty.util.component.Dumpable

        dumpSelf
      • Methods inherited from interface org.eclipse.jetty.util.component.LifeCycle

        addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
    • Constructor Detail

      • ShutdownHandler

        @Deprecated
        public ShutdownHandler​(Server server,
                               java.lang.String shutdownToken)
        Deprecated.
        Creates a listener that lets the server be shut down remotely (but only from localhost).
        Parameters:
        server - the Jetty instance that should be shut down
        shutdownToken - a secret password to avoid unauthorized shutdown attempts
      • ShutdownHandler

        public ShutdownHandler​(java.lang.String shutdownToken)
      • ShutdownHandler

        public ShutdownHandler​(java.lang.String shutdownToken,
                               boolean exitJVM,
                               boolean sendShutdownAtStart)
        Parameters:
        shutdownToken - a secret password to avoid unauthorized shutdown attempts
        exitJVM - If true, when the shutdown is executed, the handler class System.exit()
        sendShutdownAtStart - If true, a shutdown is sent as an HTTP post during startup, which will shutdown any previously running instances of this server with an identically configured ShutdownHandler
    • Method Detail

      • sendShutdown

        public void sendShutdown()
                          throws java.io.IOException
        Throws:
        java.io.IOException
      • doStart

        protected void doStart()
                        throws java.lang.Exception
        Overrides:
        doStart in class AbstractHandler
        Throws:
        java.lang.Exception
      • handle

        public void handle​(java.lang.String target,
                           Request baseRequest,
                           javax.servlet.http.HttpServletRequest request,
                           javax.servlet.http.HttpServletResponse response)
                    throws java.io.IOException,
                           javax.servlet.ServletException
        Description copied from interface: Handler
        Handle a request.
        Specified by:
        handle in interface Handler
        Overrides:
        handle in class HandlerWrapper
        Parameters:
        target - The target of the request - either a URI or a name.
        baseRequest - The original unwrapped request object.
        request - The request either as the Request object or a wrapper of that request. The HttpConnection.getCurrentConnection().getHttpChannel().getRequest() method can be used access the Request object if required.
        response - The response as the Response object or a wrapper of that request. The HttpConnection.getCurrentConnection().getHttpChannel().getResponse() method can be used access the Response object if required.
        Throws:
        java.io.IOException - if unable to handle the request or response processing
        javax.servlet.ServletException - if unable to handle the request or response due to underlying servlet issue
      • doShutdown

        protected void doShutdown​(Request baseRequest,
                                  javax.servlet.http.HttpServletResponse response)
                           throws java.io.IOException
        Throws:
        java.io.IOException
      • setExitJvm

        public void setExitJvm​(boolean exitJvm)
      • isSendShutdownAtStart

        public boolean isSendShutdownAtStart()
      • setSendShutdownAtStart

        public void setSendShutdownAtStart​(boolean sendShutdownAtStart)
      • getShutdownToken

        public java.lang.String getShutdownToken()
      • isExitJvm

        public boolean isExitJvm()