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);
}