Hi Brad!
The GracefulShutdownHandler is a handler that acts when your server is shutting down. It acts in two ways when shutdown starts: it prevents new requests from being served , and it also allows the requests being served to complete without errors. This happens in two steps:
1. It receives a notification from your code when the server is shutting down. After it receives this notification from your code, it will prevent new requests from being handled by returning a SERVICE UNAVAILABLE 503 response code to any request that the server receives during shutdown. Notifying the handler that your server is shutting down is mandatory, or else it is of no use.
2. It notifies you when the server is ready to conclude shutdown, which is when you should clear your resources, stop any services you use, and you're finally ready to stop the server jdk instance. This guarantees that you wait for the completion of requests being handled when shutdown starts as I pointed out above. There are three ways to do that, and we will see each one of them in the quick tutorial below.
So, here is how you use GracefulShutdownHandler:
1. You set the root handler to be a GracefulShutdownHandler when creating the server. Notice that you want it to be the outermost handler in your chain, simply because you don't want requests reaching other handlers before denying a request that was received after the server started shutting down.
```
GracefulShutdownHandler shutdownHandler = Handlers.gracefulShutdown(nextHandler);
```
2. You invoke start() before starting the server, that way, you know the handler is ready to accept requests, with everything set up, before the server actually starts receiving requests. Notice you don't add any special configuration to the handler.
```
shutdownHandler.start();
```
3. Simply leave the server running as it normally would, until you decide you need to shutdown.
4. When ready to start shutting down your server, you need to flag to the shutdown handler that the server is going to shutdown. For that, invoke the shutdown method(). At this step, you are executing the step (1) above:
```
shutdownHandler.shutdown();
```
5.Now we are going to move to the step (2) above. You don't actually shutdown the server at the previous step. After invoking shutdown() you wait until all requests that were being handled when you invoked shutdown have been fully responded to. Only after that you are free to clear your resources, stop services, stop the jdk, and anything else that you need to do. You know for a certain that no new requests are going to come in because the GracefulShutdownHandler is blocking all new requests. To know when all requests have been fully served, after invoking shutdown(), you can:
a. Invoke awaitShutdown(): ```shutdownHandler.awaitShutdown();```
This method will simply block your thread until no requests are being served. When it returns, you know it is safe to stop the server.
b. Invoke awaitShutdown (milis): ```shutdownHandler.awaitShutdown(60000);```
This method is similar to the previous one. It will block your thread until either no requests are being served, or the milis timeout expires. This one is useful if you notice that the server waits for a long time because it takes too long to serve some requests. So, you can say something like: "60 s is the maximum time I will wait for the server shutdown; after that I'm done you can continue shutdown even if there are requests still being served"
c. Invoke addShutdownListener: ```shutdownHandler.addShutdownListener((shutdownSuccessful) -> { if (shutdownSuccessful) { cleanResourcesAndExit();}}); ```
This method will receive a listener that is notified when all requests have completed. Notice that this method, differently from the previous two, will not block the invoking thread. So, you need to make sure the listener is responsible for triggering the "clear the resouces, stop services, finally stop the jdk instance, etc" step.
For the time being, I hope, though, that my explanation above sorts out your questions. Please let me know if you have further questions on this topic.
Best regards,
Flavia