Signal handler
Runtime.getRuntime().addShutdownHook(new Thread(() -> {
server.stopAcceptingNewRequests();
waitForInFlight(Duration.ofSeconds(20));
runtime.shutdown();
}));Advertisement
Steps in order
- Health check goes red.
- Stop accepting new requests.
- Wait for in-flight to complete.
- Flush metrics/traces.
- Close DB connections.
- Exit.
Advertisement
Deadline discipline
K8s gives you 30s by default. Budget accordingly. Slow shutdown = forced kill.