Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot graceful shutdown

I am developing a Spring Boot application backed by embedded Tomcat and I need to develop a graceful shutdown with the following steps:

  1. stop processing new HTTP requests (stop web container)
  2. process all already accepted requests
  3. shutdown Spring ApplicationContext

*do the steps above sequentially (one by one)

How can I achieve this?

P.S. Spring Boot 1.5.20.RELEASE, Java 8

like image 476
Matthew I. Avatar asked May 08 '19 12:05

Matthew I.


People also ask

How do I enable shutdown grace period in Spring Boot?

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.

What is grace period in Spring Boot?

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.

What is the default shutdown time in Spring Boot?

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.

What is graceful shutdown in servlet?

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.


1 Answers

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:

  • stop accepting new requests
  • will wait for some configurable time to process already accepted requests
  • stop container
  • stop embedded server

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 using spring.lifecycle.timeout-per-shutdown-phase.


  • 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.

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.

like image 135
narendra-choudhary Avatar answered Sep 17 '22 23:09

narendra-choudhary