Graceful shutdown in Undertow

1.4k views Asked by At

I'm building a light Java app using light4j and Undertow and I need a mechanism for waiting for existing requests to finish before shutting down the server. I discovered the existence of GracefulShutdownHandler and I managed to wrap it around a regular io.undertow.server.HttpHandler, inside a com.networknt.server.HandlerProvider, like this:

public class PathHandlerProvider implements HandlerProvider {

@Override
public HttpHandler getHandler() {
    return Handlers.routing()
            .add(Methods.POST, "/something", Handlers.gracefulShutdown(new PostHandler()));
}

The problem is that io.undertow.server.handlers.GracefulShutdownHandler#awaitShutdown needs to be called explicitly when shutting down the server, and this means that I need the instance of the GracefulShutdownHandler wrapping my PostHandler, that was created when the server started. As a shutdown hook, I'm using a custom implementation of com.networknt.server.ShutdownHookProvider, so this is where I should be able to call the awaitShutdown method. Any ideas on how I can access the existing handlers (I wouldn't like to manage the handlers myself)? Or is there another way to use GracefulShutdownHandler?

1

There are 1 answers

0
Steve Hu On

light-4j has its own ShutdownHook for users to inject clean up logic during server shutdown.

https://github.com/networknt/light-4j/blob/master/server/src/main/java/com/networknt/server/ShutdownHookProvider.java