Break it! What's new in Spring Boot 2.3.0: how to stop gracefully?

1. What is elegant shutdown

Let's start with a simple code, as follows:

@RestController
public class DemoController {
 @GetMapping("/demo")
 public String demo() throws InterruptedException {
     //Simulate business time-consuming processing flow
  Thread.sleep(20 * 1000L);
  return "hello";
 }
}

When we request the traffic to execute the business logic on this interface, if the server executes the shutdown (kill) at this time, the spring boot will directly close the container (tomcat, etc.) by default, resulting in the failure of the execution of the business logic. In some business scenarios, data inconsistency will occur, and transaction logic will not be rolled back.

2.graceful shutdown

 

In the latest version of spring boot 2.3, this function is built-in and does not need to expand the container thread pool to handle it. At present, the web servers (Jetty, Reactor Netty, Tomcat and undercow) and reactive and Servlet based web applications supported by spring boot embedded support elegant shutdown function. Let's look at how to use:

When using server.shutdown=graceful When enabled, when the web container is closed, the web server will no longer receive new requests and will wait for the buffer period for the active request to complete.

3. Configuration experience

 

The shutdown behavior supported here is as follows:

/**
 * Configuration for shutting down a {@link WebServer}.
 *
 * @author Andy Wilkinson
 * @since 2.3.0
 */
public enum Shutdown {
 /**
  * Elegant shutdown (deadline shutdown)
  *
  */
 GRACEFUL,

 /**
  * Stop immediately
  */
 IMMEDIATE;

}

Buffer timeout per shutdown phase configuration

  • The default time is 30S, which means that the maximum waiting time is 30S. In case of overtime, whether the thread task is completed or not, it will stop processing. It must be set reasonably.

Effect experience

  1. Request server interface

 

  1. Execute close app

 

  1. Server receives shutdown command
2020-05-17 18:28:28.940  INFO 60341 --- [extShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown        : Commencing graceful shutdown. Waiting for active requests to complete
2020-05-17 18:28:45.923  INFO 60341 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown        : Graceful shutdown complete
  1. Interface request execution completed

4. Relevant knowledge

4.1 execute kill-2 instead of kill-9 here

  • kill -2 is equivalent to the shortcut key Ctrl + C that triggers Java's shutdown hook event handling (refer to the following source code for elegant shutdown or some post-processing)
//ApplicationContext
 @Override
 public void registerShutdownHook() {
  if (this.shutdownHook == null) {
   // No shutdown hook registered yet.
   this.shutdownHook = new Thread(SHUTDOWN_HOOK_THREAD_NAME) {
    @Override
    public void run() {
     synchronized (startupShutdownMonitor) {
      doClose();
     }
    }
   };
   Runtime.getRuntime().addShutdownHook(this.shutdownHook);
  }
 }
  • Kill-9, violent aesthetics forces the killing process, and does not execute shutdown hook

4.2 elegant shutdown through actor endpoint

POST requests / Actor / shutdown to perform an elegant shutdown.

Source code analysis

@Endpoint(id = "shutdown", enableByDefault = false)
public class ShutdownEndpoint implements ApplicationContextAware {

 @WriteOperation
 public Map<String, String> shutdown() {
  Thread thread = new Thread(this::performShutdown);
  thread.setContextClassLoader(getClass().getClassLoader());
  thread.start();
 }

 private void performShutdown() {
  try {
   Thread.sleep(500L);
  }
  catch (InterruptedException ex) {
   Thread.currentThread().interrupt();
  }

  //The close logic here is the same as the shutdown hook above
  this.context.close();
 }
}

4.3 elegant downtime behavior differences between different web containers

Container downtime behavior depends on the specific web container behavior

 

Finally, a set of technical data compiled by the editor can not only eliminate technical blind spots accurately and accumulate interview experience, but also overcome technical problems such as JVM, Spring, distributed and micro services.

 

Mass e-books, collector's Edition

 

 

 

Collection steps

1. Wechat access

Tags: Java Tomcat Spring Jetty Netty

Posted on Fri, 29 May 2020 04:47:24 -0400 by jaylee