1

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.

4

2 回答 2

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.

于 2013-08-22T15:18:08.393 回答
0

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();
}
于 2013-08-23T19:04:25.810 回答