users@grizzly.java.net

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

From: Oleksiy Stashok <oleksiy.stashok_at_oracle.com>
Date: Tue, 09 Oct 2012 15:47:43 +0200

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
>