diff --git a/spring-boot/src/main/java/com/baeldung/gracefulshutdown/GracefulShutdownApplication.java b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/GracefulShutdownApplication.java new file mode 100644 index 0000000000..b7a840b38c --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/GracefulShutdownApplication.java @@ -0,0 +1,16 @@ +package com.baeldung.gracefulshutdown; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.EnableAutoConfiguration; +import org.springframework.boot.autoconfigure.SpringBootApplication; +import org.springframework.context.annotation.ComponentScan; + +@SpringBootApplication +@EnableAutoConfiguration +@ComponentScan(basePackages = {"com.baeldung.gracefulshutdown"}) +public class GracefulShutdownApplication { + + public static void main(String args[]) { + SpringApplication.run(GracefulShutdownApplication.class, args); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/gracefulshutdown/beans/LongRunningProcessBean.java b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/beans/LongRunningProcessBean.java new file mode 100644 index 0000000000..e21ddfe021 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/beans/LongRunningProcessBean.java @@ -0,0 +1,36 @@ +package com.baeldung.gracefulshutdown.beans; + +import javax.annotation.PostConstruct; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.core.task.TaskExecutor; +import org.springframework.stereotype.Component; + +@Component +public class LongRunningProcessBean { + + private static final Logger LOG = LoggerFactory.getLogger(LongRunningProcessBean.class); + + @Autowired + private TaskExecutor taskExecutor; + + @PostConstruct + public void runTaskOnStartup() { + LOG.info("runTaskOnStartup entering"); + for (int i = 0; i < 3; i++) { + final int processNumber = i; + taskExecutor.execute(() -> { + try { + LOG.info("Long running process {} using threadpool started", processNumber); + Thread.sleep(60_000); + LOG.info("Long running process {} using threadpool completed", processNumber); + } catch (Exception e) { + LOG.error("Error while executing task", e); + } + }); + } + LOG.info("runTaskOnStartup exiting"); + } +} diff --git a/spring-boot/src/main/java/com/baeldung/gracefulshutdown/config/SpringConfiguration.java b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/config/SpringConfiguration.java new file mode 100644 index 0000000000..1f27163215 --- /dev/null +++ b/spring-boot/src/main/java/com/baeldung/gracefulshutdown/config/SpringConfiguration.java @@ -0,0 +1,34 @@ +package com.baeldung.gracefulshutdown.config; + +import javax.annotation.PreDestroy; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; +import org.springframework.core.task.TaskExecutor; +import org.springframework.scheduling.annotation.EnableScheduling; +import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor; + +@Configuration +@EnableScheduling +public class SpringConfiguration { + + private static final Logger LOG = LoggerFactory.getLogger(SpringConfiguration.class); + + @Bean + public TaskExecutor taskExecutor() { + ThreadPoolTaskExecutor taskExecutor = new ThreadPoolTaskExecutor(); + taskExecutor.setCorePoolSize(2); + taskExecutor.setMaxPoolSize(2); + taskExecutor.setWaitForTasksToCompleteOnShutdown(true); + taskExecutor.setAwaitTerminationSeconds(30); + taskExecutor.initialize(); + return taskExecutor; + } + + @PreDestroy + public void destroy() { + LOG.info("Shutdown initiated"); + } +}