Shutting down jetty using ShutdownHandler

时光怂恿深爱的人放手 提交于 2019-12-08 07:57:06

问题


I am using http://www.eclipse.org/jetty/documentation/current/shutdown-handler.html as a guide to try to shut down my jetty server, but I'm getting java.net.SocketException: Unexpected end of file from server when connection.getResponseCode(); is called and I don't know why. I'm using an xml configured server, so the way that the ShutdownHandler is added to the Handler chain is a little different, but it should be fine. I know that the ShutdownHandler is properly wired up to the handler chain because I used dumpAfterStartup to check if it was started.

The thing that I am most unsure of is the line: URL url = new URL("http://localhost:" + port + "/shutdown?token=" + shutdownCookie);. I don't know what I'm supposed to put in the shutdownCookie field; the password specified in the ShutdownHandler, or the STOP key, or what. I've tried both the password and the STOP key with no luck. I'm using POST as my request method, but I tried PUT as well and it does not work either.

Please ask for more information as required.


回答1:


shutdownCookie should be the secret that you configured the shutdown handler with

If the handler chain is configured correctly this will work...

 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();
        }
    }

This yields the following log:

 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 SelectChannelConnector@0.0.0.0:10100
 Aug 22, 2013 9:31:25 AM org.eclipse.jetty.server.handler.ShutdownHandler handle
 WARNING: Unauthorized shutdown attempt from 127.0.0.1

Changing the shutdownCookie from 'foobar' to 'foo' yields:

2013-08-22 10:13:00.829:INFO:oejsh.ShutdownHandler:Shutting down by request from 127.0.0.1

You can take the above code and call the static method from another JVM and the first server will stop as expected.




回答2:


Unfortunately, the ShutdownHandler method did not work for me, but I looked into the source code and found this snippet which did.

Socket s = new Socket(InetAddress.getByName("localhost"), STOP_PORT);
try {
    OutputStream out = s.getOutputStream();
    out.write((STOP_KEY + "\r\nstop\r\n").getBytes());
    out.flush();
} finally {
    s.close();
}


来源:https://stackoverflow.com/questions/18342014/shutting-down-jetty-using-shutdownhandler

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!