users@grizzly.java.net

RE: What is the best way to gracefully shutdown an embedded Grizzly server?

From: Rempel, Paul <paul.rempel_at_subserveo.com>
Date: Tue, 9 Oct 2012 14:12:12 +0000

Thanks very much for the quick response. I filed an improvement ticket:

http://java.net/jira/browse/GRIZZLY-1345

This is the first Grizzly ticket I’ve ever filed, so I wasn’t sure which version to mark, time estimates, etc.. I’m using version 1.9.46, but I imagine this would be useful for all versions. Maybe it is the excuse I need to upgrade ☺

In the meantime, I’ll try out your code sample.

Thanks,
Paul

From: Oleksiy Stashok [mailto:oleksiy.stashok_at_oracle.com]
Sent: Tuesday, October 09, 2012 6:48 AM
To: users_at_grizzly.java.net
Subject: Re: What is the best way to gracefully shutdown an embedded Grizzly server?

Hi Paul,

pls. file an issue, we'll implement this asap.
Meanwhile you can do something like this:

public class Test {
    public static void main(String[] args) throws Exception {
        HttpServer httpServer = new HttpServer();
        NetworkListener listener = new NetworkListener("graceful-shutdown", "localhost", 9090);
        httpServer.addListener(listener);

        final TCPNIOTransport transport = listener.getTransport();

        httpServer.getServerConfiguration().addHttpHandler(new HttpHandler() {

            @Override
            public void service(Request request, Response response) throws Exception {
                Thread.sleep(30000);
                response.getWriter().write("Done");
            }
        }, "/");

        try {
            httpServer.start();

            System.out.println("Press enter to stop");
            System.in.read();

            System.out.println("Shutting down....");

            transport.unbindAll();

            final long t1 = System.currentTimeMillis();

            transport.getWorkerThreadPool().shutdown();
            transport.getWorkerThreadPool().awaitTermination(60, TimeUnit.SECONDS);

            System.out.println("Shutdown took: " + (System.currentTimeMillis() - t1) + " millis");
        } finally {
            httpServer.stop();
        }
    }
}

Thanks.

WBR,
Alexey.
On 10/09/2012 03:33 PM, Rempel, Paul wrote:
What is the best way to gracefully shutdown an embedded Grizzly server that processes long running synchronous requests?

I would like to tell the server to stop accepting new connections, while allowing existing requests to finish processing before finally shutting down the server.

If this functionality is not implemented, are there some API hooks that could be used to implement this in my application?

I also saw a similar unanswered question on stackoverflow:

http://stackoverflow.com/questions/12478709/is-it-possible-to-setup-grizzly-for-graceful-shutdown

Thanks,

Paul