public class ShutdownHandler extends AbstractHandler
Server server = new Server(8080); HandlerList handlers = new HandlerList(); handlers.setHandlers(new Handler[] { someOtherHandler, new ShutdownHandler(server,"secret password") }); server.setHandler(handlers); server.start();
public static void attemptShutdown(int port, String shutdownCookie) { try { URL url = new URL("http://localhost:" + port + "/shutdown?cookie=" + 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); } }
AbstractLifeCycle.AbstractLifeCycleListener
LifeCycle.Listener
_listeners, FAILED, RUNNING, STARTED, STARTING, STOPPED, STOPPING
Constructor and Description |
---|
ShutdownHandler(Server server,
String shutdownToken)
Creates a listener that lets the server be shut down remotely (but only from localhost).
|
Modifier and Type | Method and Description |
---|---|
protected String |
getRemoteAddr(javax.servlet.http.HttpServletRequest request) |
void |
handle(String target,
Request baseRequest,
javax.servlet.http.HttpServletRequest request,
javax.servlet.http.HttpServletResponse response)
Handle a request.
|
void |
setExitJvm(boolean exitJvm) |
destroy, doStart, doStop, dumpThis, getServer, setServer
addBean, addBean, contains, dump, dump, dump, dump, dump, dumpObject, dumpStdErr, getBean, getBeans, getBeans, isManaged, manage, removeBean, removeBeans, unmanage
addLifeCycleListener, getState, getState, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
addLifeCycleListener, isFailed, isRunning, isStarted, isStarting, isStopped, isStopping, removeLifeCycleListener, start, stop
public ShutdownHandler(Server server, String shutdownToken)
server
- the Jetty instance that should be shut downshutdownToken
- a secret password to avoid unauthorized shutdown attemptspublic void handle(String target, Request baseRequest, javax.servlet.http.HttpServletRequest request, javax.servlet.http.HttpServletResponse response) throws IOException, javax.servlet.ServletException
Handler
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 AbstractHttpConnection.getCurrentConnection()
method can be used access the Request object if required.response
- The response as the Response
object or a wrapper of that request. The AbstractHttpConnection.getCurrentConnection()
method can be used access the Response object if required.IOException
javax.servlet.ServletException
protected String getRemoteAddr(javax.servlet.http.HttpServletRequest request)
public void setExitJvm(boolean exitJvm)
Copyright © 1995-2012 Mort Bay Consulting. All Rights Reserved.