有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java程序关闭Spring Boot应用程序后关闭pc

我有一个特殊的用例。我的Raspberry上有一个Spring boot应用程序。此应用程序在Raspberry启动时自动启动。我想发送一个远程命令来重新启动或关闭覆盆子,但我需要首先正确关闭我的Spring Boot应用程序

此应用程序使用H2数据库和SmsLib,需要一点时间才能关闭(10/20秒)

我的想法是拦截远程请求,然后尝试通过以下方式以编程方式关闭应用程序:

applicationContext.close();

然后,在my custom ApplicationListener中,我读取了应该执行的命令(重新启动或停止),然后执行以下操作:

Runtime.getRuntime().exec("reboot");

这是我的上下文closedevent的相关部分:

@Component
public class ContextClosedHandler implements        ApplicationListener<ContextClosedEvent> {
private String commandToExecute;
public void onApplicationEvent(ContextClosedEvent event) {
try {
        if (commandToExecute != null) {
            Runtime.getRuntime().exec(commandToExecute);
        }
    } catch (Throwable e) {
        log.error("Errore durante l'esecuzione del comando <" + commandToExecute + ">", e);
    }

}

然而,我认为这不是100%的正确方法。只有当我的应用程序中的所有内容都被终止时(与db、侦听器、处理程序、ecc的连接),我才应该确保重新启动或关闭我的Raspberry。你有更好的主意吗

谢谢


共 (0) 个答案