3

特定の条件に基づいて Tomcat を自動的に再起動したいと考えています。基本的に、私は 32 MB の JVM 内で動作しており、スペースが不足する可能性があります。そのため、定期的に空きメモリの状態を確認しています。

たとえば、空きメモリの割合が 50% を下回った場合、Tomcat を内部から再起動したいと考えています。

これが優雅なアプローチではないことはわかっています。そして、メモリリークの修正にもっと取り組む必要があります! しかし、それを行うまでは、この問題に対する戦術的な解決策があるかどうかを知りたい.

4

5 に答える 5

3

これはあなたを助けるかもしれません。必要に応じて構成します。

Tomcat の起動/停止用のバッチ/シェル スクリプト:

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ファイルを作成し、OSの起動時に実行し、定期的にチェックします.2分間、ヒープサイズの消費量が多いとしますか?

それ以上の場合は、上記のCallBatchFile.javaプロセスを実行します

于 2012-12-04T06:45:42.460 に答える
0

たぶん、あなたはjsp / jarファイルに触れることができ、それはあなたのアプリをリロードするためにtomcatをトリガーします。うまくいけば、古いアプリインスタンスを完全にアンロードし、すべてのメモリを解放します。

于 2012-12-04T06:40:52.563 に答える
0

シェルスクリプトに慣れている場合は、メモリがダウンしたときに tomcat を再起動するスクリプトを作成してください。他の方法は、Java アプリから再起動スクリプトをトリガーすることであるため、Java コードを使用するだけではタスクを達成できません。

于 2012-12-04T06:20:54.557 に答える
0

数日間の調査と試行錯誤の後、Web アプリから Tomcat を再起動する機能が動作するようになりました。

基本的に、バックグラウンドで tomcat スクリプトを開始する独自のスクリプトを作成する必要があります (以下のステップ 1 を参照してください。このスクリプトは、Web アプリから Process オブジェクトを介して呼び出されます (以下のステップ 2 を参照)。 us は、メソッドの javadoc に記載されています。

手順は次のとおりです。

(1) バックグラウンドで 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 アプリでは、次のメソッドが呼び出されます。

/**
 * 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 に答える