Class ShutdownHandler

All Implemented Interfaces:
Handler, HandlerContainer, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, 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: Usage:
 Server server = new Server(8080);
 HandlerList handlers = new HandlerList();
 handlers.setHandlers(new Handler[]
 { someOtherHandler, new ShutdownHandler("secret password", false, true) });
 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.getResponseCode();"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);
  • Constructor Details

    • ShutdownHandler

      public ShutdownHandler(String shutdownToken)
      Creates a listener that lets the server be shut down remotely (but only from localhost).
      shutdownToken - a secret password to avoid unauthorized shutdown attempts
    • ShutdownHandler

      public ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)
      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 Details

    • sendShutdown

      public void sendShutdown() throws IOException
    • doStart

      protected void doStart() throws Exception
      Description copied from class: ContainerLifeCycle
      Starts the managed lifecycle beans in the order they were added.
      doStart in class AbstractHandler
      AbstractLifeCycle.StopException - If thrown, the lifecycle will immediately be stopped.
      Exception - If there was a problem starting. Will cause a transition to FAILED state
    • handle

      public void handle(String target, Request baseRequest, jakarta.servlet.http.HttpServletRequest request, jakarta.servlet.http.HttpServletResponse response) throws IOException, jakarta.servlet.ServletException
      Description copied from interface: Handler
      Handle a request.
      Specified by:
      handle in interface Handler
      handle in class HandlerWrapper
      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.
      IOException - if unable to handle the request or response processing
      jakarta.servlet.ServletException - if unable to handle the request or response due to underlying servlet issue
    • doShutdown

      protected void doShutdown(Request baseRequest, jakarta.servlet.http.HttpServletResponse response) throws IOException
    • setExitJvm

      public void setExitJvm(boolean exitJvm)
    • isSendShutdownAtStart

      public boolean isSendShutdownAtStart()
    • setSendShutdownAtStart

      public void setSendShutdownAtStart(boolean sendShutdownAtStart)
    • getShutdownToken

      public String getShutdownToken()
    • isExitJvm

      public boolean isExitJvm()