Class ShutdownHandler

java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
All Implemented Interfaces:
Handler, Handler.Container, Handler.Singleton, Request.Handler, org.eclipse.jetty.util.component.Container, org.eclipse.jetty.util.component.Destroyable, org.eclipse.jetty.util.component.Dumpable, org.eclipse.jetty.util.component.Dumpable.DumpableContainer, org.eclipse.jetty.util.component.LifeCycle, org.eclipse.jetty.util.thread.Invocable

public class ShutdownHandler extends Handler.Wrapper

A Handler that initiates a Shutdown of the Jetty Server it belongs to.

Used to trigger shutdown of a Jetty Server instance

Server Setup Example:

 Server server = new Server(8080);
 String shutdownToken = "secret password";
 boolean exitJvm = false;
 ShutdownHandler shutdown = new ShutdownHandler(shutdownToken, exitJvm));
 shutdown.setHandler(someOtherHandler);
 server.setHandler(someOtherHandlers);
 server.start();
 
Client Triggering Example

 public static void attemptShutdown(int port, String shutdownToken) {
   try {
     String encodedToken = URLEncoder.encode(shutdownToken);
     URI uri = URI.create("http://localhost:%d/shutdown?token=%s".formatted(port, shutdownCookie));
     HttpClient httpClient = HttpClient.newBuilder().build();
     HttpRequest httpRequest = HttpRequest.newBuilder(shutdownURI)
         .POST(HttpRequest.BodyPublishers.noBody())
         .build();
     HttpResponse<String> httpResponse = httpClient.send(httpRequest,
         HttpResponse.BodyHandlers.ofString(StandardCharsets.UTF_8));
     Assertions.assertEquals(200, httpResponse.statusCode());
     System.out.println(httpResponse.body());
     logger.info("Shutting down " + uri + ": " + httpResponse.body());
   } catch (IOException | InterruptedException e) {
     logger.debug("Shutdown Handler not available");
     // Okay - the server is not running
     throw new RuntimeException(e);
   }
 }