【问题标题】:Restart Tomcat from within itself从自身内部重新启动 Tomcat
【发布时间】:2012-12-04 05:34:23
【问题描述】:

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

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

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

【问题讨论】:

标签: java tomcat


【解决方案1】:

这可能会对您有所帮助。根据需要进行配置:

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 进程

【讨论】:

  • 我不明白解决方案。 Call*java 文件位于何处?在 tomcat 中?
  • 当您定期检查空闲内存的状态时,如果空闲内存较少,请在单独的线程中运行 CallBatchFile.java。这将运行批处理文件,它将重新启动您的 tomcat。您需要将此 CallBatchFile.java 文件保存在预定义目录或 user.dir 或 catalina.base 目录中。
【解决方案2】:

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      经过几天的研究和反复试验,我们的网络应用程序重新启动了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);
          }
      
      }
      

      【讨论】:

        【解决方案5】:

        您也可以使用Monit 之类的内容。它是一个简单的守护进程,您可以配置它来检查 JVM 内存统计信息并触发 tomcat 服务重启。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2010-10-21
          • 2016-11-28
          相关资源
          最近更新 更多