1. Introduction
Shutdown can be divided into violent shutdown and elegant shutdown. The difference between them will be illustrated by the following simple example
@GetMapping("/gracefulShutdown")
public String gracefulShutdown(a) throws InterruptedException {
TimeUnit.SECONDS.sleep(20);
log.info("Close the application and the task can continue....");
return "Close the application and the task can continue....";
}
Copy the code
2. Violent shutdown
Start the service, through a browser visit: http://localhost:8080/gracefulShutdown to check 8080 port corresponding pid, use kill 2 pid command to kill the corresponding process
After killing the process, the browser should look like this:
The console should look like this:
2020-11-20 13:40:03.808 INFO 57565 --- [nio-8080-exec-1] com. Boot. Example. Filter. RequestFilter: request path: HTTP://localhost:8080/gracefulShutdown
2020-11-20 13:40:03.823 INFO 57565 --- [nio-8080-exec-1] C.B.E xample. Controller. DebugController: task starts...
Disconnected from the target VM, address: '127.0.0.1:51558', transport: 'socket'
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
Copy the code
As you can see from the above observations, unfinished requests after killing the application process do not continue to execute
3. Turn it off gracefully
Before demonstrating an elegant shutdown, add the following configuration to your configuration file:
server:
shutdown: graceful
Copy the code
After killing the application process, the browser will look like this:
The console should look like this:
2020-11-20 13:51:23.652 INFO 57943 --- [nio-8080-exec-3] com. Boot. Example. Filter. RequestFilter: request path: HTTP://localhost:8080/gracefulShutdown
2020-11-20 13:51:23.653 INFO 57943 --- [nio-8080-exec-3] C.B.E xample. Controller. DebugController: task starts...
2020-11-20 13:51:27.335 INFO 57943 --- [extShutdownHook] o.s.b.w.e.tomcat.GracefulShutdown : Commencing graceful shutdown. Waiting for active requests to complete
2020-11-20 13:51:43.657 INFO 57943 --- [nio-8080-exec-3] C.B.E xample. Controller. DebugController: close the application, the task can continue...
2020-11-20 13:51:43.672 INFO 57943 --- [tomcat-shutdown] o.s.b.w.e.tomcat.GracefulShutdown : Graceful shutdown complete
Disconnected from the target VM, address: '127.0.0.1:52344', transport: 'socket'
Process finished with exit code 130 (interrupted by signal 2: SIGINT)
Copy the code
As you can see, all you need to do to achieve an elegant shutdown is specify the service shutdown type as elegant in the configuration file. Advantage: Active requests continue to be executed after shutdown
4. Official notes
Here’s what the official SpringBoot2.3 documentation says about elegant shutdown:
The graceful shutdown feature applies to web services embedded in 4. When the application is closed, existing requests continue to be executed and no new requests are accepted.
As mentioned above, existing requests will continue to be executed after the application is closed. Will existing requests continue to be executed? Keep reading the official notes:
As you can see, there is a timeout for the execution of existing requests
Timeout configuration:
spring:
lifecycle:
timeout-per-shutdown-phase: 60s
Copy the code
The default timeout period is 30 seconds