我们有一个多线程的Spring Boot应用程序,它在Linux机器上作为守护进程运行。当我尝试像这样通过start- stop -daemon停止应用程序时
start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --name $NAME发送SIGTERM信号,应用程序立即结束。但是,我希望应用程序等待,直到每个线程完成它的工作。
当收到SIGTERM信号时,有没有办法,如何管理发生的事情?
发布于 2016-04-18 18:59:51
Spring Boot应用程序向JVM注册一个关闭钩子,以确保ApplicationContext在退出时优雅地关闭。创建一个或多个实现DisposableBean或具有@PreDestroy注释的has方法的bean。此bean将在应用程序关闭时调用。
http://docs.spring.io/spring-boot/docs/current-SNAPSHOT/reference/htmlsingle/#boot-features-application-exit
发布于 2018-11-19 15:32:32
@Evgeny提到的示例
@SpringBootApplication
@Slf4j
public class SpringBootShutdownHookApplication {
public static void main(String[] args) {
SpringApplication.run(SpringBootShutdownHookApplication.class, args);
}
@PreDestroy
public void onExit() {
log.info("###STOPing###");
try {
Thread.sleep(5 * 1000);
} catch (InterruptedException e) {
log.error("", e);;
}
log.info("###STOP FROM THE LIFECYCLE###");
}
}https://stackoverflow.com/questions/36691355
复制相似问题