Reputation: 89
I would like to restart Tomcat automatically based on a certain condition. Basically I am operating within a 32 MB JVM and may run out of space. Hence I periodically check the status of free memory.
I would like to restart Tomcat from within itself if, say, the free memory percentage goes below 50%.
I know this is not a graceful approach. And I should be working more on fixing the memory leaks! But until I do that, I would like to know if there is a tactical solution to this problem.
Upvotes: 3
Views: 2724
Reputation: 147
You could also use something like Monit. It is a simple daemon which you can configure to check the JVM memory stats and trigger a tomcat service restart.
Upvotes: 0
Reputation: 579
After a few days of research and trial and error got the function of restarting tomcat from our web app working.
Essntially, you need to create your own script that kicks off the tomcat script in the background (see step# 1 below. This script will be called from your web app through the Process object (see step# 2 below). The gotchas that bit us are mentioned in the method's javadoc.
Here's are the steps:
(1) Create a shell script that calls the tomcat script in the background:
#! /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) In the web app, the following method is called:
/**
* 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);
}
}
Upvotes: 0
Reputation: 928
This might help you. Configure as per your need:
Batch/shell script for Tomcat start/stop:
cd tomcat_dir\bin
catalina stop
catalina start
Running batch file http://www.roseindia.net/answers/viewqa/Java-Beginners/5286-Running-Batch-files-in-java.html
For memory check http://viralpatel.net/blogs/getting-jvm-heap-size-used-memory-total-memory-using-java-runtime
Create Jar file and execute when OS starts and check at regular interval, say for 2 mins that heap size consumption is more ?
If more then run the above CallBatchFile.java
process
Upvotes: 3
Reputation: 45433
Maybe you can touch a jsp/jar file, that will trigger tomcat to reload you app. Hopefully it will completely unload your old app instance and free all the memory.
Upvotes: 0
Reputation: 10997
If you are comfortable with shell scripting write a script to restart tomcat once memory come down. You wont be able to accomplish the task, just using java code, as other way is to trigger a restart script from your java app.
Upvotes: 0