This is a followup question to How to shutdown a Spring Boot Application in a correct way?
Suppose we have a server that has 3 clients currently connected and each client is running a long job. We then shutdown the server with the command:
curl -X POST localhost:port/shutdown
Does that:
A) Let the server finish the jobs that the 3 clients are running before shutting down?
B) Disallow any other connections from happening such that the server will eventually shut down?
The Spring Boot shutdown endpoint invokes this class:
org.springframework.boot.actuate.endpoint.ShutdownEndpoint
which invokesclose()
on yourApplicationContext
. This in turn ...If your beans are ordered and have carefully written shutdown methods, then this should be fine. But if not, then at some point in that shutdown cycle "the jobs that the 3 clients are running" are likely to be interrupted. In addition, it is possible that new connections may be made in the small time window between you invoking shutdown and the shutdown cycle kicking in.
Spring provides application events and listener hooks which allow you to participate in the shutdown cycle. The
ContextClosedEvent
is published before beans are destroyed, the embedded container is shutdown, etc, so you can use this to implement your own shutdown behaviour before the default behaviour kicks in. For example:You could implement this listener such that it
You register this listener in the same way as you register any ApplicationListener in Spring Boot e.g.