A Handler that initiates a Shutdown of the Jetty Server it belongs to.
Used to trigger shutdown of a Jetty Server instance
exitJvm is set to true a hard System#exit(int) call will be performed.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);
}
}
| Modifier and Type | Field and Description |
|---|---|
| private boolean | |
| private final String | |
| private final String | |
| private static final Logger |
| Access | Constructor and Description |
|---|---|
| public | ShutdownHandler(String
a secret password to avoid unauthorized shutdown attempts shutdownToken)Creates a Handler that lets the server be shut down remotely (but only from localhost). |
| public | ShutdownHandler(String
a secret password to avoid unauthorized shutdown attempts shutdownToken, boolean If true, when the shutdown is executed, the handler class System.exit() exitJVM)Creates a Handler that lets the server be shut down remotely (but only from localhost). |
| public | ShutdownHandler(Handler
the handler to wrap handler, String the path to respond to shutdown requests against (default is " shutdownPath, String /shutdown")a secret password to avoid unauthorized shutdown attempts shutdownToken, boolean If true, when the shutdown is executed, the handler class System.exit() exitJVM)Creates a Handler that lets the server be shut down remotely (but only from localhost). |
| Modifier and Type | Method and Description |
|---|---|
| public boolean | handle(Request
the HTTP request to handle request, Response the HTTP response to handle response, Callback the callback to complete when the handling is complete callback)Overrides org. Implements org. Invoked to handle the passed HTTP request and response. |
| private boolean | |
| public boolean | |
| private boolean | |
| public void | |
| private void |
| _exitJvm | back to summary |
|---|---|
| private boolean _exitJvm | |
| _shutdownPath | back to summary |
|---|---|
| private final String _shutdownPath | |
| _shutdownToken | back to summary |
|---|---|
| private final String _shutdownToken | |
| LOG | back to summary |
|---|---|
| private static final Logger LOG | |
| ShutdownHandler | back to summary |
|---|---|
| public ShutdownHandler(String shutdownToken) Creates a Handler that lets the server be shut down remotely (but only from localhost).
| |
| ShutdownHandler | back to summary |
|---|---|
| public ShutdownHandler(String shutdownToken, boolean exitJVM) Creates a Handler that lets the server be shut down remotely (but only from localhost).
| |
| ShutdownHandler | back to summary |
|---|---|
| public ShutdownHandler(Handler handler, String shutdownPath, String shutdownToken, boolean exitJVM) Creates a Handler that lets the server be shut down remotely (but only from localhost).
| |
| handle | back to summary |
|---|---|
| public boolean handle(Request request, Response response, Callback callback) throws Exception Overrides org. Implements org. Doc from org. Invoked to handle the passed HTTP request and response. The request is accepted by returning true, then handling must be concluded by completing the passed callback. The handling may be asynchronous, i.e. this method may return true and complete the given callback later, possibly from a different thread. If this method returns false, then the callback must not be invoked and any mutation on the response reversed. Exceptions thrown by this method may be subsequently handled by an error The simplest implementation is:
public boolean handle(Request request, Response response, Callback callback)
{
callback.succeeded();
return true;
}
A HelloWorld implementation is:
public boolean handle(Request request, Response response, Callback callback)
{
response.write(true, ByteBuffer.wrap("Hello World\n".getBytes(StandardCharsets.UTF_8)), callback);
return true;
}
| |
| hasCorrectSecurityToken | back to summary |
|---|---|
| private boolean hasCorrectSecurityToken(Request request) | |
| isExitJvm | back to summary |
|---|---|
| public boolean isExitJvm() | |
| requestFromLocalhost | back to summary |
|---|---|
| private boolean requestFromLocalhost(Request request) | |
| setExitJvm | back to summary |
|---|---|
| public void setExitJvm(boolean exitJvm) | |
| shutdownServer | back to summary |
|---|---|
| private void shutdownServer() | |