Class ShutdownHandler
java.lang.Object
org.eclipse.jetty.util.component.AbstractLifeCycle
org.eclipse.jetty.util.component.ContainerLifeCycle
org.eclipse.jetty.server.handler.AbstractHandler
org.eclipse.jetty.server.handler.AbstractHandlerContainer
org.eclipse.jetty.server.handler.HandlerWrapper
org.eclipse.jetty.server.handler.ShutdownHandler
- All Implemented Interfaces:
Handler, HandlerContainer, Container, Destroyable, Dumpable, Dumpable.DumpableContainer, LifeCycle
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 AbstractHandler
AbstractHandler.ErrorDispatchHandlerNested classes/interfaces inherited from class AbstractLifeCycle
AbstractLifeCycle.AbstractLifeCycleListenerNested classes/interfaces inherited from interface Container
Container.InheritedListener, Container.ListenerNested classes/interfaces inherited from interface Dumpable
Dumpable.DumpableContainerNested classes/interfaces inherited from interface LifeCycle
LifeCycle.Listener -
Field Summary
FieldsModifier and TypeFieldDescriptionprivate booleanprivate booleanprivate final Stringprivate static final LoggerFields inherited from class HandlerWrapper
_handler -
Constructor Summary
ConstructorsConstructorDescriptionShutdownHandler(String shutdownToken) ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart) ShutdownHandler(Server server, String shutdownToken) Deprecated. -
Method Summary
Modifier and TypeMethodDescriptionprotected voiddoShutdown(Request baseRequest, javax.servlet.http.HttpServletResponse response) protected voiddoStart()Starts the managed lifecycle beans in the order they were added.private Stringvoidhandle(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) Handle a request.private booleanhasCorrectSecurityToken(javax.servlet.http.HttpServletRequest request) booleanbooleanprivate booleanrequestFromLocalhost(Request request) voidvoidsetExitJvm(boolean exitJvm) voidsetSendShutdownAtStart(boolean sendShutdownAtStart) private voidshutdownServer(Server server) Methods inherited from class HandlerWrapper
destroy, expandChildren, getHandler, getHandlers, insertHandler, setHandlerMethods inherited from class AbstractHandlerContainer
doShutdown, expandHandler, findContainerOf, getChildHandlerByClass, getChildHandlers, getChildHandlersByClass, setServerMethods inherited from class AbstractHandler
doError, doStop, getServerMethods inherited from class 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, updateBeansMethods inherited from class AbstractLifeCycle
addLifeCycleListener, getState, getState, getStopTimeout, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop, toStringMethods inherited from class Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface Dumpable.DumpableContainer
isDumpableMethods inherited from interface LifeCycle
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
-
Field Details
-
LOG
-
_shutdownToken
-
_sendShutdownAtStart
private boolean _sendShutdownAtStart -
_exitJvm
private boolean _exitJvm
-
-
Constructor Details
-
ShutdownHandler
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 downshutdownToken- a secret password to avoid unauthorized shutdown attempts
-
ShutdownHandler
-
ShutdownHandler
- Parameters:
shutdownToken- a secret password to avoid unauthorized shutdown attemptsexitJVM- 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
- Throws:
IOException
-
getServerUrl
-
doStart
Description copied from class:ContainerLifeCycleStarts the managed lifecycle beans in the order they were added.- Overrides:
doStartin classAbstractHandler- Throws:
Exception
-
handle
public void handle(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException Description copied from interface:HandlerHandle a request.- Specified by:
handlein interfaceHandler- Overrides:
handlein classHandlerWrapper- Parameters:
target- The target of the request - either a URI or a name.baseRequest- The original unwrapped request object.request- The request either as theRequestobject or a wrapper of that request. Themethod can be used access the Request object if required.HttpConnection.getCurrentConnection().getHttpChannel().getRequest()response- The response as theResponseobject or a wrapper of that request. Themethod can be used access the Response object if required.HttpConnection.getCurrentConnection().getHttpChannel().getResponse()- Throws:
IOException- if unable to handle the request or response processingjavax.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 IOException - Throws:
IOException
-
requestFromLocalhost
-
hasCorrectSecurityToken
private boolean hasCorrectSecurityToken(javax.servlet.http.HttpServletRequest request) -
shutdownServer
-
setExitJvm
public void setExitJvm(boolean exitJvm) -
isSendShutdownAtStart
public boolean isSendShutdownAtStart() -
setSendShutdownAtStart
public void setSendShutdownAtStart(boolean sendShutdownAtStart) -
getShutdownToken
-
isExitJvm
public boolean isExitJvm()
-