我需要从 Java 代码重新启动 tomcat。
例如,如果某个查询在某个时间段内没有执行,那么它会自动重启 tomcat。
我试过下面的关机和启动代码,但是当我们关闭tomcat时,java代码不会运行,tomcat也没有启动。
注意:- 我正在从应用程序运行此代码并重新启动同一应用程序正在使用的同一个 tomact。
按照代码
try {
PreparedStatement.setQueryTimeout(10);
rs = PreparedStatement.executeQuery();
} catch (SQLException ex) {
System.out.println("IN CATCH BLOCK FOR THE REFRESH INVOICE");
String shutcommand = "killall java";
Process shutchild = Runtime.getRuntime().exec(shutcommand);
System.out.println("JAVA PROCESS KILLED");
String locationCommand = "cd /root/cluster/tomcat6/bin";
Process locationChild = Runtime.getRuntime().exec(locationCommand);
String strtcommand = "./startup.sh";
Process strtchild = Runtime.getRuntime().exec(strtcommand);
}
最佳答案
在 SQLException 之后杀死 tomcat 这不是处理此异常的好主意。可能问题出在数据库站点上。
但是如果您确定这就是您所需要的,您可以在本节中终止此 java 进程,但是要运行 tomcat,您应该使用例如 bash 和 cron。为什么?因为杀死你的执行代码后会停止,所以你没有达到启动 tomcat 的行。
如何检查tomcat:Is Tomcat running?
关于java - 用 Java 重启 Tomcat,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48109709/