1. Overview
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:
server.shutdown=graceful
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, t****he 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:
spring.lifecycle.timeout-per-shutdown-phase=1m
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.
3. Conclusion
In this short tutorial, we saw how we could take advantage of the new graceful shutdown feature in Spring Boot 2.3.