从自身内部重新启动 Tomcat



我想根据特定条件自动重新启动Tomcat。基本上,我在 32 MB JVM 内运行,可能会用完空间。因此,我会定期检查可用内存的状态。

我想从内部重新启动 Tomcat,例如,如果可用内存百分比低于 50%。

我知道这不是一个优雅的方法。我应该更多地致力于修复内存泄漏!但在我这样做之前,我想知道这个问题是否有战术解决方案。

这可能会对您有所帮助。根据您的需要进行配置:

用于 Tomcat 启动/停止的批处理/外壳脚本:

cd tomcat_dirbin
catalina stop
catalina start

运行批处理文件 http://www.roseindia.net/answers/viewqa/Java-Beginners/5286-Running-Batch-files-in-java.html

对于内存检查 http://viralpatel.net/blogs/getting-jvm-heap-size-used-memory-total-memory-using-java-runtime

创建 Jar 文件并在操作系统启动时执行并定期检查,例如 2 分钟堆大小消耗更多?

如果更多,则运行上述CallBatchFile.java过程

如果您熟悉 shell 脚本,请编写一个脚本,以便在内存下降后重新启动 tomcat。仅使用 java 代码将无法完成任务,因为另一种方法是从 java 应用程序触发重新启动脚本。

也许你可以触摸一个jsp/jar文件,这将触发tomcat重新加载你的应用程序。 希望它能完全卸载您的旧应用程序实例并释放所有内存。

经过几天的研究和反复试验,获得了从我们的网络应用程序重新启动tomcat的功能。

实际上,您需要创建自己的脚本,在后台启动 tomcat 脚本(请参阅下面的步骤# 1)。此脚本将通过 Process 对象从 Web 应用调用(请参阅下面的步骤 # 2)。咬我们的陷阱在该方法的javadoc中提到。

以下是步骤:

(1) 创建一个在后台调用 tomcat 脚本的 shell 脚本:

#! /bin/sh
# This script simply calls the tomcat script in the background using nohup ... &
echo Calling tomcat shell script in background...
nohup 2>&1 /your/tomcat/shell/script.sh $1 > /your/tomcat/shell/script.log &
echo Done calling the tocat shell script

(2) 在 Web 应用中,调用以下方法:

/**
 * Run shell script.
 * <p>
 * CAUTION: The following could cause problems:
 * <ol>
 * 1) Calling process.waitFor(). This call blocks the thread until the
 * process is terminated. This could be an issue when calling the script to
 * restart tomcat: the JVM is waiting for the process to finish
 * before it stops while the process is waiting for the JVM to stop.
 * <ol>
 * 2) Redirecting the process's streams (which could be obtained by calling
 * process.getInputStream() and process.getErrorStream()) to a separate
 * thread. This could be an issue when calling the script to restart: JVM is
 * waiting for the thread to terminate while the thread is awaiting for more
 * input to come through the stream.
 * <ol>
 * 3) Calling the actual script directly. This could be a similar issue to
 * (1) above when calling the script to restart. Better approach is to have
 * another script that executes the main script in the background (using
 * nohup ... &).
 * 
 */
private void kickOffProcess(String scriptOption) {
    String executeScript = null;
    try {
        File workingDir = new File(WORKING_DIRECTORY_PATH);
        // This script calls the tomcat script in the background
        executeScript = "/your/shell/script.sh";
        logger.info("Executing the following script: [" + executeScript
                + "] ");
        // Call the shell script
        ProcessBuilder processBuilder = new ProcessBuilder(executeScript,
                scriptOption);
        // processBuilder.redirectErrorStream(true);
        processBuilder.directory(workingDir);
        Process process = processBuilder.start();
        int exitValue = process.exitValue();
        if (exitValue != 0) {
            logger.error("Script failed to execute. " + "exitValue["
                    + exitValue + "] ");
        }
        logger.info("Done with calling the script. " + "exitValue["
                + exitValue + "] ");
    } catch (IOException e) {
        String errorMessage = "Failed with IOException trying to run the script. "
                + "executeScript[" + executeScript + "] ";
        logger.error(errorMessage, e);
    }
}

你也可以使用像Monit这样的东西。它是一个简单的守护程序,您可以配置它来检查 JVM 内存统计信息并触发 tomcat 服务重新启动。

最新更新