0

I am running my Spring boot web application inside the docker container and every time we safely stop the container, it abruptly shut down the running spring-boot service(Default behavior). My requirement is to process the existing requests without any issues. I have tried the below approach something like adding sleep logic for some minutes in ContextClosedEvent, so that the existing requests have time to serve the response, but the following approach doesn't stop the requests incoming even after the shutdown got initiated. is there any specific way to achieve my requirement?

@Slf4j
@SpringBootApplication
public class Application {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(Application.class);
        application.addListeners((ApplicationListener<ContextClosedEvent>) event -> {
            log.info("Shutdown initiated...");
            try {
                Thread.sleep(TimeUnit.MINUTES.toMillis(5));
            } catch (InterruptedException e) {
                log.error("Exception is thrown", e);
            }
            log.info("Graceful Shutdown is processed succesfully");
        });
        application.run(args);
    }

Prasanth Rajendran
  • 1,596
  • 1
  • 20
  • 26

1 Answers1

0

Sharing my approach. Let's say you are using the Springboot application as a web app, and you want to process the existing requests without abruptly terminating them, spring boot provides the out-of-box solution.

An excerpt from Spring doc

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. It occurs as part of closing the application context and is performed in the earliest phase of stopping SmartLifecycle beans. This stop processing uses a timeout which provides a grace period during which existing requests will be allowed to complete but no new requests will be permitted. The exact way in which new requests are not permitted varies depending on the web server that is being used. Jetty, Reactor Netty, and Tomcat will stop accepting requests at the network layer. Undertow will accept requests but respond immediately with a service unavailable (503) response.

NOTE: Graceful shutdown with Tomcat requires Tomcat 9.0.33 or later.

Code Snippet

To enable graceful shutdown

server.shutdown=graceful

To configure the timeout period(here I set the time values as 1minute)

spring.lifecycle.timeout-per-shutdown-phase=1m

Console output:

[INFO] [SpringContextShutdownHook] [org.springframework.boot.web.embedded.tomcat.GracefulShutdown:53] Comme
ncing graceful shutdown. Waiting for active requests to complete

...logs of existing running API endpoints

[tomcat-shutdown]   [org.springframework.boot.web.embedded.tomcat.GracefulShutdown:78] Graceful shut
down complete
[SpringContextShutdownHook] [org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor:218] S
hutting down ExecutorService 'applicationTaskExecutor'

From the above logs, you could see that the spring boot waits for all the running requests to process and shut the server gracefully

Prasanth Rajendran
  • 1,596
  • 1
  • 20
  • 26