I am developing a Spring Boot application backed by embedded Tomcat and I need to develop a graceful shutdown with the following steps:
*do the steps above sequentially (one by one)
How can I achieve this?
P.S. Spring Boot 1.5.20.RELEASE, Java 8
To enable graceful shutdown, add server.shutdown=graceful to properties (by default it is set to immediate ). Grace period can be configured using spring.lifecycle.timeout-per-shutdown-phase property (example: spring.lifecycle.timeout-per-shutdown-phase=1m.
During a graceful shutdown Spring Boot allows some grace period to the application to finish all the current requests or processes. Once, the grace period is over the unfinished processes or requests are just killed. By default, Spring Boot allows a 30 seconds graceful shutdown timeout.
By default, Spring Boot allows a 30 seconds graceful shutdown timeout. However, we can configure it by using application properties or yaml file. Yaml file. Like any other Spring properties, we can also externalise this property.
This allows active requests to complete before closing the context, and shutting down container. Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications.
Graceful shutdown support was added in Spring Boot 2.3 (release in May, 2020). This allows active requests to complete before closing the context, and shutting down container.
When graceful shutdown is enabled, application will perform following steps sequentially upon shutdown:
From release notes:
Graceful shutdown is supported with all four embedded web servers (Jetty, Reactor Netty, Tomcat, and Undertow) and with both reactive and Servlet-based web applications. When enabled using
server.shutdown=graceful
, upon shutdown, the web server will no longer permit new requests and will wait for a grace period for active requests to complete. The grace period can be configured usingspring.lifecycle.timeout-per-shutdown-phase
.
server.shutdown=graceful
to properties (by default it is set to immediate
).spring.lifecycle.timeout-per-shutdown-phase
property (example: spring.lifecycle.timeout-per-shutdown-phase=1m
.For Spring Boot < 2.3, you'll take to tinker with server's connector to stop accepting new requests as explained in this Spring GitHub issue.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With