数日間の調査と試行錯誤の後、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);
}
}