Gracefully Terminating a Grizzly Transport

As of Grizzly 2.3.4, we’ve added the ability to gracefully shutdown the HttpServer (i.e., the server will shutdown once all of the current requests have been processed).  However, it seemed that this functionality could be generalized to Grizzly transports so that custom servers could have this feature.  So, as of this week, it is possible to test this feature in the current 2.3.5-SNAPSHOT release.

So, how could you leverage this in your custom server?  Well, it’s pretty simple.  There are two interfaces you’ll need to be aware of.  The first, which anyone wanting to use this feature will have to implement is the GracefulShutdownListener interface.  This interface provides two callbacks.  The shutdownRequested(ShutdownContext) callback will be invoked when a graceful shutdown has been initiated by calling Transport.shutdown() or Transport.shutdown(long, TimeUnit).  The other callback, shutdownForced(), will be invoked if a graceful shutdown was initiated and the grace period expired or Transport.shutdownNow() was called.

The other interface is ShutdownContext.  An instance of this will be passed to each GracefulShutdownListener.

As you can see, once your GracefulShutdownListener has determined it is safe to terminate the trasport, it will need to call ready() on the provided ShutdownContext.

In order to be notified that a graceful shutdown is happening, at some point the GracefulShutdownListener will need to be registered with the transport of interest by calling Transport.addShutdownListener().


This entry was posted in Java and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">