In this quick tutorial, we're going to see how we can configure Spring Boot applications to handle shutdowns more gracefully.
2. Graceful Shutdown
As of Spring Boot 2.3, Spring Boot now supports the graceful shutdown feature for all four embedded web servers (Tomcat, Jetty, Undertow, and Netty) on both servlet and reactive platforms.
To enable the graceful shutdown, all we have to do is to set the server.shutdown property to graceful in our application.properties file:
Then, Tomcat, Netty, and Jetty will stop accepting new requests at the network layer. Undertow, on the other hand, will continue to accept new requests but send an immediate 503 Service Unavailable response to the clients.
By default, the value of this property is equal to immediate, which means the server gets shut down immediately.
Some requests might get accepted just before the graceful shutdown phase begins. In that case, the server will wait for those active requests to finish their work up to a specified amount of time. We can configure this grace period using the spring.lifecycle.timeout-per-shutdown-phase configuration property:
If we add this, the server will wait up to one minute for active requests to complete. The default value for this property is 30 seconds.
In this short tutorial, we saw how we could take advantage of the new graceful shutdown feature in Spring Boot 2.3.