Backend Development 11 min read

Graceful Shutdown Support in Spring Boot 2.3+ for Embedded Web Servers

Spring Boot 2.3.0.RELEASE adds graceful shutdown for all embedded web servers, explaining the underlying SmartLifecycle mechanism, configuration steps, shutdown hook registration, and how timeout handling ensures existing requests finish while new requests are rejected.

政采云技术
政采云技术
政采云技术
Graceful Shutdown Support in Spring Boot 2.3+ for Embedded Web Servers

Spring Boot 2.3.0.RELEASE introduced graceful shutdown support for all four embedded web servers (Jetty, Reactor Netty, Tomcat, Undertow) for both reactive and servlet‑based applications.

Graceful shutdown is performed during the closing of the application context by the earliest SmartLifecycle beans. A timeout defines a grace period during which existing requests may finish while new requests are rejected (network layer for Jetty, Reactor Netty, Tomcat; HTTP 503 for Undertow).

To enable it, add the following configuration to application.yml (or application.properties ):

server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 30s

The shutdown process relies on Java shutdown hooks. Spring Boot registers a shutdown hook that invokes doClose() on the application context, which in turn triggers the LifecycleProcessor to stop all Lifecycle beans.

During web‑server creation, Spring Boot registers a bean that implements SmartLifecycle (e.g., WebServerGracefulShutdownLifecycle ). Its stop(Runnable callback) method calls the embedded server’s shutDownGracefully(...) method.

For Tomcat, the TomcatWebServer holds a GracefulShutdown instance that is invoked only when the shutdown mode is set to GRACEFUL . Other servers have analogous implementations.

The DefaultLifecycleProcessor groups Lifecycle beans by their Phase value, sorts the groups in descending order, and stops each bean. For SmartLifecycle beans the processor uses a CountDownLatch with a timeout (default 30 seconds, configurable via timeout-per-shutdown-phase ) to wait for asynchronous stop callbacks.

In summary, Spring Boot’s graceful shutdown works by:

Registering a JVM shutdown hook that calls doClose() .

Creating a SmartLifecycle bean for the embedded server.

Executing all Lifecycle beans’ stop methods in phase order, with the web‑server bean having the highest priority.

Waiting for asynchronous stop operations to finish within the configured timeout before proceeding to the next phase.

JavaSpring BootGraceful ShutdownSmartLifecycleEmbedded Web Servershutdown-hook
政采云技术
Written by

政采云技术

ZCY Technology Team (Zero), based in Hangzhou, is a growth-oriented team passionate about technology and craftsmanship. With around 500 members, we are building comprehensive engineering, project management, and talent development systems. We are committed to innovation and creating a cloud service ecosystem for government and enterprise procurement. We look forward to your joining us.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.