shutdownCookie應該是你如果處理程序鏈是否正確,這將工作配置配置了
關機處理程序的祕密......
public void start() throws Exception
{
Server server = new Server(10100);
DebugHandler dh = new DebugHandler();
dh.setHandler(new ShutdownHandler(server,"foo"));
server.setHandler(dh);
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();
}
catch (Exception e)
{
throw new RuntimeException(e);
}
}
public static void main(String[] args)
{
try
{
Shutdown s = new Shutdown();
s.start();
Shutdown.attemptShutdown(10100,"foobar");
}
catch (Exception e)
{
e.printStackTrace();
}
}
我們得到以下日誌:
Aug 22, 2013 9:31:25 AM org.eclipse.jetty.server.Server doStart
INFO: jetty-8.1.11.v20130520
Aug 22, 2013 9:31:25 AM org.eclipse.jetty.server.AbstractConnector doStart
INFO: Started [email protected]:10100
Aug 22, 2013 9:31:25 AM org.eclipse.jetty.server.handler.ShutdownHandler handle
WARNING: Unauthorized shutdown attempt from 127.0.0.1
將shutdownCookie從'foobar'更改爲'foo'產生:
2013-08-22 10:13:00.829:INFO:oejsh.ShutdownHandler:Shutting down by request from 127.0.0.1
您可以使用上述代碼並從另一個JVM調用靜態方法,並且第一臺服務器將按預期停止。