416577 enhanced shutdown handler to send shutdown at startup
This commit is contained in:
parent
c6a29339ab
commit
ba5a709245
|
@ -19,11 +19,16 @@
|
||||||
package org.eclipse.jetty.server.handler;
|
package org.eclipse.jetty.server.handler;
|
||||||
|
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.net.HttpURLConnection;
|
||||||
|
import java.net.SocketException;
|
||||||
|
import java.net.URL;
|
||||||
|
|
||||||
import javax.servlet.ServletException;
|
import javax.servlet.ServletException;
|
||||||
import javax.servlet.http.HttpServletRequest;
|
import javax.servlet.http.HttpServletRequest;
|
||||||
import javax.servlet.http.HttpServletResponse;
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.eclipse.jetty.server.Connector;
|
||||||
|
import org.eclipse.jetty.server.NetworkConnector;
|
||||||
import org.eclipse.jetty.server.Request;
|
import org.eclipse.jetty.server.Request;
|
||||||
import org.eclipse.jetty.server.Server;
|
import org.eclipse.jetty.server.Server;
|
||||||
import org.eclipse.jetty.util.log.Log;
|
import org.eclipse.jetty.util.log.Log;
|
||||||
|
@ -42,7 +47,7 @@ import org.eclipse.jetty.util.log.Logger;
|
||||||
Server server = new Server(8080);
|
Server server = new Server(8080);
|
||||||
HandlerList handlers = new HandlerList();
|
HandlerList handlers = new HandlerList();
|
||||||
handlers.setHandlers(new Handler[]
|
handlers.setHandlers(new Handler[]
|
||||||
{ someOtherHandler, new ShutdownHandler(server,"secret password") });
|
{ someOtherHandler, new ShutdownHandler("secret password") });
|
||||||
server.setHandler(handlers);
|
server.setHandler(handlers);
|
||||||
server.start();
|
server.start();
|
||||||
</pre>
|
</pre>
|
||||||
|
@ -64,18 +69,15 @@ import org.eclipse.jetty.util.log.Logger;
|
||||||
}
|
}
|
||||||
</pre>
|
</pre>
|
||||||
*/
|
*/
|
||||||
public class ShutdownHandler extends AbstractHandler
|
public class ShutdownHandler extends HandlerWrapper
|
||||||
{
|
{
|
||||||
private static final Logger LOG = Log.getLogger(ShutdownHandler.class);
|
private static final Logger LOG = Log.getLogger(ShutdownHandler.class);
|
||||||
|
|
||||||
private final String _shutdownToken;
|
private final String _shutdownToken;
|
||||||
|
private boolean _sendShutdownAtStart;
|
||||||
private final Server _server;
|
|
||||||
|
|
||||||
private boolean _exitJvm = false;
|
private boolean _exitJvm = false;
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Creates a listener that lets the server be shut down remotely (but only from localhost).
|
* Creates a listener that lets the server be shut down remotely (but only from localhost).
|
||||||
*
|
*
|
||||||
|
@ -84,16 +86,86 @@ public class ShutdownHandler extends AbstractHandler
|
||||||
* @param shutdownToken
|
* @param shutdownToken
|
||||||
* a secret password to avoid unauthorized shutdown attempts
|
* a secret password to avoid unauthorized shutdown attempts
|
||||||
*/
|
*/
|
||||||
|
@Deprecated
|
||||||
public ShutdownHandler(Server server, String shutdownToken)
|
public ShutdownHandler(Server server, String shutdownToken)
|
||||||
{
|
{
|
||||||
this._server = server;
|
this(shutdownToken);
|
||||||
this._shutdownToken = shutdownToken;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public ShutdownHandler(String shutdownToken)
|
||||||
|
{
|
||||||
|
this(shutdownToken,false,false);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* @param shutdownToken
|
||||||
|
* @param sendShutdownAtStart If true, a shutdown is sent as a HTTP post
|
||||||
|
* during startup, which will shutdown any previously running instances of
|
||||||
|
* this server with an identically configured ShutdownHandler
|
||||||
|
*/
|
||||||
|
public ShutdownHandler(String shutdownToken, boolean exitJVM, boolean sendShutdownAtStart)
|
||||||
|
{
|
||||||
|
this._shutdownToken = shutdownToken;
|
||||||
|
setExitJvm(exitJVM);
|
||||||
|
setSendShutdownAtStart(sendShutdownAtStart);
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public void sendShutdown() throws IOException
|
||||||
|
{
|
||||||
|
URL url = new URL(getServerUrl() + "/shutdown?token=" + _shutdownToken);
|
||||||
|
try
|
||||||
|
{
|
||||||
|
HttpURLConnection connection = (HttpURLConnection)url.openConnection();
|
||||||
|
connection.setRequestMethod("POST");
|
||||||
|
connection.getResponseCode();
|
||||||
|
LOG.info("Shutting down " + url + ": " + connection.getResponseMessage());
|
||||||
|
}
|
||||||
|
catch (SocketException e)
|
||||||
|
{
|
||||||
|
LOG.debug("Not running");
|
||||||
|
// Okay - the server is not running
|
||||||
|
}
|
||||||
|
catch (IOException e)
|
||||||
|
{
|
||||||
|
throw new RuntimeException(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@SuppressWarnings("resource")
|
||||||
|
private String getServerUrl()
|
||||||
|
{
|
||||||
|
NetworkConnector connector=null;
|
||||||
|
for (Connector c: getServer().getConnectors())
|
||||||
|
{
|
||||||
|
if (c instanceof NetworkConnector)
|
||||||
|
{
|
||||||
|
connector=(NetworkConnector)c;
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
if (connector==null)
|
||||||
|
return "http://localhost";
|
||||||
|
|
||||||
|
return "http://localhost:" + connector.getPort();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
protected void doStart() throws Exception
|
||||||
|
{
|
||||||
|
super.doStart();
|
||||||
|
if (_sendShutdownAtStart)
|
||||||
|
sendShutdown();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
public void handle(String target, Request baseRequest, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
|
||||||
{
|
{
|
||||||
if (!target.equals("/shutdown"))
|
if (!target.equals("/shutdown"))
|
||||||
{
|
{
|
||||||
|
super.handle(target,baseRequest,request,response);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -119,6 +191,7 @@ public class ShutdownHandler extends AbstractHandler
|
||||||
|
|
||||||
new Thread()
|
new Thread()
|
||||||
{
|
{
|
||||||
|
@Override
|
||||||
public void run ()
|
public void run ()
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
|
@ -156,7 +229,7 @@ public class ShutdownHandler extends AbstractHandler
|
||||||
|
|
||||||
private void shutdownServer() throws Exception
|
private void shutdownServer() throws Exception
|
||||||
{
|
{
|
||||||
_server.stop();
|
getServer().stop();
|
||||||
|
|
||||||
if (_exitJvm)
|
if (_exitJvm)
|
||||||
{
|
{
|
||||||
|
@ -169,4 +242,24 @@ public class ShutdownHandler extends AbstractHandler
|
||||||
this._exitJvm = exitJvm;
|
this._exitJvm = exitJvm;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public boolean isSendShutdownAtStart()
|
||||||
|
{
|
||||||
|
return _sendShutdownAtStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setSendShutdownAtStart(boolean sendShutdownAtStart)
|
||||||
|
{
|
||||||
|
_sendShutdownAtStart = sendShutdownAtStart;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getShutdownToken()
|
||||||
|
{
|
||||||
|
return _shutdownToken;
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean isExitJvm()
|
||||||
|
{
|
||||||
|
return _exitJvm;
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue