How to gracefully shutdown spring boot app using configuration and programatically?
Asked Answered
T

0

2

I want to gracefully shutdown my spring boot application. I wanted to know what is the difference between configuring it via application.yaml file and programmatically ?

in my application yaml file, i have added this from reference https://www.baeldung.com/spring-boot-web-server-shutdown:

server:
  shutdown: graceful
spring:
  lifecycle:
    timeout-per-shutdown-phase: 30s

and in my code i am also using threadPoolTaskExecuter because I am using @EnableAsync annotation:

@Bean("threadPoolTaskExecutor")
public TaskExecutor getAsyncExecutor() {
    ThreadPoolTaskExecutor executor = new ThreadPoolTaskExecutor();
    executor.setCorePoolSize(20);
    executor.setMaxPoolSize(200);
    executor.setWaitForTasksToCompleteOnShutdown(true);
    executor.setAwaitTerminationSeconds(30);
    executor.setThreadNamePrefix("async-");
    executor.setQueueCapacity(50);
    executor.initialize();
    return executor;
}

where i added executor.setAwaitTerminationSeconds(30) so my question is do we need both ways to configure it or not? and what is the difference? Also I found that we have other configuration available from spring from here: Spring Boot: gracefully shutdown by controlling termination order involving MongoClient

spring.task.execution.shutdown.await-termination=true
spring.task.execution.shutdown.await-termination-period=60

this is confusing to me. Can someone please explain the different ways ? Thanks in advance

Twine answered 9/11, 2022 at 14:14 Comment(1)
its interesting perhaps there are two types. One for the controllers and other for the async processes.Dyun

© 2022 - 2024 — McMap. All rights reserved.