gpt4 book ai didi

spring - 如何以编程方式在 Tomcat 上自行关闭 Spring 应用程序

转载 作者:行者123 更新时间:2023-11-28 22:50:39 25 4
gpt4 key购买 nike

我们如何以编程方式自行关闭或停止 Spring 应用程序?

我们目前正在使用 ConfigurableApplicationContext#close() ,是的,它会关闭应用程序但不会关闭或停止应用程序。

从tomcat manager来看,application的状态仍然是running,所以client仍然可以连接到web app,获取前端资源。此外,所有 servlet 似乎仍在工作并且可以接受请求。

下面的代码展示了我们目前的做法。

@Autowired
private ConfigurableApplicationContext configurableApplicationContext;

@Scheduled(cron="0 0/5 * * * ?")
private void validateLicenseExpired()
{
try
{
LicenseUtils.isLicensingPeriodValid();
} catch (LicenseInvalidException lie) {
logger.error("\t The licensing is expired", lie);
configurableApplicationContext.close();
}
}

感谢任何帮助。

最佳答案

如果它是启动应用程序,您可以使用以下链接停止应用程序。

Programmatically shut down Spring Boot application

否则,您可以像下面这样以编程方式调用 shutdown.sh 来停止服务器。

String command = "<path-to-tomcat>shutdown.sh"; // for windows use bat
Process child = Runtime.getRuntime().exec(command);

[编辑]

这个线程解释了如何停止像 tomcat 管理器这样的应用程序

Start / stop a web application from itself?

关于spring - 如何以编程方式在 Tomcat 上自行关闭 Spring 应用程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40829170/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com