Spring Boot 2.3 gracefully closes the new pose, which is really fragrant!

The latest Spring Boot 2.3 is almost half a month old:


One of the new features is: Graceful shutdown

I've shared such articles before, but now I've even produced the official gesture. For the new function, I have to go to the official for a look. Here's the official description:

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 a grace period is configured, upon shutdown, the web server will no longer permit new requests and will wait for up to the grace period for active requests to complete.

Next, the stack leader will summarize:

Graceful shutdown supports all four embedded Web servers: jetty, reactor netty, Tomcat, undercow, and responsive and Servlet based Web applications.

When an elegant shutdown buffer time is configured, the Web server is no longer allowed to receive new requests until the application is shut down. The buffer time is to wait for all active requests currently in progress to be processed.

It should be noted that Tomcat and Jetty will immediately stop receiving requests at the network layer, while Undertow can continue to receive requests, but will immediately return 503 service unavailable error.

Note: Tomcat effective version requires: 9.0.33+

How to open and close gracefully?

The following is an example configuration of the Yaml file:

# Open and close gracefully
  shutdown: graceful

# Buffer time off  
    timeout-per-shutdown-phase: 10s

Source code analysis

The above describes the configuration mode of graceful shutdown parameters. Next, we can see what the default configuration is through the source code.

First, let's see the first parameter configuration receiving class:


public enum Shutdown {

    private Shutdown() {

As shown in the source code, the default is: IMMEDIATE, so elegant closing is manually opened according to business needs.

Let's look at the second parameter configuration receiving class:


As shown in the source code, the default buffer time is: 30 seconds.

Take a look at the elegant closed source code:

According to Graceful, we can find several related classes. Let's go to Tomcat's:


public enum GracefulShutdownResult {

	 * Requests remained active at the end of the grace period.

	 * The server was idle with no active requests at the end of the grace period.

	 * The server was shutdown immediately, ignoring any active requests.


REQUESTS_ACTIVE means that the connection remains active before the end of the buffer period, that is, although the official default gives you 30 seconds of cache time to process the backlog request, if the processing is not completed after 30 seconds, Spring Boot will also force the application to close.

Therefore, it should be noted that when closing gracefully, the business processing volume at that time must be considered, and whether the set buffer time can handle the business in process.

In addition, Spring Boot graceful shutdown needs to be triggered in conjunction with the / shutdown endpoint of the activator. Please refer to this article for details: Several methods of Spring Boot to stop service gracefully.

Well, today, the head of the stack has shared it. You can learn about it, and friends who are interested in the new feature can also test it.

Recommend to my blog to read more:

1.Java JVM, collection, multithreading, new features series

2.Spring MVC, Spring Boot, Spring Cloud series tutorials

3.Maven, Git, Eclipse, Intellij IDEA series tools tutorial

4.Latest interview questions of Java, backend, architecture, Alibaba and other large factories

Feel good, don't forget to like + forward!

Tags: Java Spring Tomcat Jetty Netty

Posted on Thu, 28 May 2020 23:47:37 -0400 by dr4296