从内部重启Tomcat

时间:2012-12-04 05:34:23

标签: java tomcat

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

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

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

5 个答案:

答案 0 :(得分: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流程

答案 1 :(得分:0)

如果您对shell脚本感到满意,一旦内存关闭,请编写一个脚本来重启tomcat。 您将无法使用Java代码完成任务,因为其他方法是从您的Java应用程序触发重新启动脚本。

答案 2 :(得分:0)

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

答案 3 :(得分:0)

经过几天的研究和试验,错误得到了从我们的网络应用程序工作重启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);
    }

}

答案 4 :(得分:0)

您还可以使用类似Monit的名称。这是一个简单的守护程序,您可以配置该守护程序来检查JVM内存状态并触发tomcat服务重启。