3

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

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

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

4

5 回答 5

3

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

Tomcat 启动/停止的批处理/shell 脚本:

cd tomcat_dir\bin
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过程

于 2012-12-04T06:45:42.460 回答
0

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

于 2012-12-04T06:40:52.563 回答
0

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

于 2012-12-04T06:20:54.557 回答
0

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

本质上,您需要创建自己的脚本,在后台启动 tomcat 脚本(参见下面的步骤#1。该脚本将通过 Process 对象从您的 Web 应用程序调用(参见下面的步骤#2)。我们在方法的 javadoc 中被提及。

以下是步骤:

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

#! /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 app中,调用如下方法:

/**
 * 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);
    }

}
于 2013-07-26T22:23:03.520 回答
0

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

于 2019-01-16T06:00:52.887 回答