当前位置: 首页>后端>正文

java项目修改properties java项目修改xml需要重启吗

昨天去面试,面试官问了一个关于Tomcat是否能够自动重启的问题,他问的简单我答的含糊。说实话当时并不知道能不能自动启动,也没搞明白他问的是怎么个自动启动,不过无所谓了。回来上网查了一下,关于自动启动的文章很多,方法也很多,基本有以下几种:

1. 定时重启Tomcat服务
2. 程序升级后强制Tomcat不重启,或某些条件下重启
3. Tomcat服务出问题后,自动重启(我感觉那个面试官是要问这个问题)

好吧,简单的记录一下网上的方法。

#################定时重启Tomcat服务###########################
http://www.lnicp.com/htmlFiles/20100416190350031.html

(1)如果你用的是windows的话,把tomcat设置成系统服务,就可以在服务属性里面的恢复选项里面,选择第一次失败后,重新启动服务,第二次....,......时间设置至少一分钟

  一旦监控到tomcat   死了,或者数据库连接断了就让软件控制tomcat自动重启  
  上网搜索一下吧,很多企业用的都是这种webmonitor  

(3)下面是一段vbs脚本,把它保存成文件(后缀名为vbs)后双击运行就行了

on   error   resume   Next      
     Dim   a    
     a   =   True    
     
     set   WshShell   =   WScript.CreateObject("WScript.Shell")    
   
     Do   While   a    
     set   http   =   CreateObject("Microsoft.XMLHTTP")    
     http.open   "POST","http://youwebsite",false    
     http.send    
     if   http.Status   >   300   then    
           WshShell.Run("net   stop   tomcat5")    
           WScript.Sleep(10000)    
           WshShell.Run("net   start   tomcat5")    
     end   if    
     WScript.Sleep(30000)    
     loop




#################程序升级后强制Tomcat不重启,或某些条件下重启###########################
http://blog.sina.com.cn/s/blog_417424300100m69d.html

修改web.xml的内容以后,Tomcat会自动重启使我们的修改生效,但是这种情况只发生在当我们部署方式是向Tomcat的app目录里拷贝项目的时候,Tomcat貌似会监视这个目录下的所有应用,当web.xml发生变化时,便会重启。

当我们通过配置虚拟目录的方式来部署应用程序时,Tomcat只会监视应用的WEB-INF目录的classes和lib两个子目录,当有jar包替换或者类替换时,tomcat才会自动重启,这种部署方式下修改web.xml,Tomcat不会自动重启。



#################Tomcat服务出问题后,自动重启###########################
Tomcat在linux下自动启动脚本
http://hi.baidu.com/hrhero/blog/item/38152c17719a530cc83d6d59.html


PATH=/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin 

 
 

 

  DATE=`date +%Y%m%d` 

 

  TIME=`date +%Y%m%d-%H%M` 

 
 

 
 

 

  num=`tail -5000 /opt/bokee/httpd/logs/photo_access_log.${DATE}|awk '{print ,}'|grep "UploadPicCtrl" | grep 500 | wc -l` 

 

  num1=`tail -5000 /opt/bokee/httpd/logs/photo_access_log.${DATE}|awk '{print ,,}'|grep PhotoBlogCtrl|grep 404 | wc -l` 

 
 

 

  ps ax|grep "java"|grep -v "grep" >> /dev/null 

 

  re=$? 

 

  if [ $re -eq 1 ] ; 

 

  then 

 

  #    /opt/bokee/tomcat5/bin/startup.sh >> /dev/null 

 

      source /etc/profile 

 

      /opt/bokee/tomcat5/bin/catalina.sh start 

 

      echo ${TIME} tomcat down...start >> /root/check.log 

 

      ps ax|grep tomcat >> /root/check.log 

 

      exit 

 

  fi





##############################################





if [ ${num} -eq 0 ] 

 

  then 

 

     if [ ${num1} -eq 0 ] 

 

        then 

 

  #      echo 0 >> /root/check.log 

 

        exit 

 

     fi 

 

  fi 

 
 

 

  if [ ${num} -gt 3 ]; then 

 

         /opt/bokee/tomcat5/bin/shutdown.sh 

 

         sleep 2 

 

         killall -9 java 

 

         sleep 2 

 

         killall -9 java 

 

         sleep 2 

 

  #       /opt/bokee/tomcat5/bin/startup.sh 

 

         source /etc/profile 

 

         /opt/bokee/tomcat5/bin/catalina.sh start 

 

          echo ${num} 500 -- ${TIME} restart >> /root/check.log 

 

          cp /opt/bokee/httpd/logs/photo_access_log.${DATE} /opt/bokee/httpd/logs/bak/photo_access_log.${TIME} 

 

          ps ax|grep tomcat|grep -v grep >> /root/check.log 

 

          echo > /opt/bokee/httpd/logs/photo_access_log.${DATE} 

 

          exit 

 

       else 

 

           echo ${num} 500 -- ${TIME} ok >> /root/check.log         

 

  fi 

 
 

 
 

 
 

 
 

 

  if [ ${num1} -gt 5 ]; then 

 

         /opt/bokee/tomcat5/bin/shutdown.sh 

 

         sleep 2 

 

         killall -9 java 

 

         sleep 2 

 

         killall -9 java 

 

         sleep 2 

 

  #       /opt/bokee/tomcat5/bin/startup.sh 

 

         source /etc/profile 

 

         /opt/bokee/tomcat5/bin/catalina.sh start 

 

          echo ${num1} 404 -- ${TIME} restart >> /root/check.log 

 

          cp /opt/bokee/httpd/logs/photo_access_log.${DATE} /opt/bokee/httpd/logs/bak/photo_access_log.${TIME} 

 

          ps ax|grep tomcat|grep -v grep >> /root/check.log 

 

          echo > /opt/bokee/httpd/logs/photo_access_log.${DATE} 

 

          exit 

 

       else 

 

           echo ${num1} 404 -- ${TIME} ok >> /root/check.log 

 
fi

Tomcat死掉自动重启的脚本

 http://dadloveu.blog.51cto.com/715500/229289/ 

#!/bin/sh 

t=`date` 

COM1="/usr/local/tomcat1/bin/catalina.sh" 

COM2="/usr/local/tomcat2/bin/catalina.sh" 

T1PID=`ps -ef | grep tomcat1 | grep -v "grep tomcat1" | cut -c 9-15` 

T2PID=`ps -ef | grep tomcat2 | grep -v "grep tomcat2" | cut -c 9-15` 

#echo $T1PID 

#echo $T2PID 

if [ -z $T1PID ] 

then 

        ${COM1} start 

        #echo "tomcat1 done" 

        echo "$t tomcat1 restart" >> /var/log/tomcat.log 

fi 

if [ -z $T2PID ] 

then 

        ${COM2} start 

        #echo "tomcat2 done" 

        echo "$t tomcat2 restart" >> /var/log/tomcat.log 

fi

使用JAVA类来检测 Tomcat状态,并实现自动重启

http://hi.baidu.com/hrhero/blog/item/38152c17719a530cc83d6d59.html

先是主程序:


import java.io.BufferedReader;
 import java.io.FileReader;
 import java.net.URL;
 import java.net.URLConnection;
 import java.util.Date; public class CheckTomcat {
  private static String tomcatroot="";
  private static String monitorurl="";
  private static void checkTomcatIsAlive(String myurl) throws NullPointerException {
   String s;
   boolean isTomcatAlive = false;
   java.io.BufferedReader in;
   try {
    System.out.println(">>>>>>检测URL:"+myurl);
    URL url = new URL(myurl);
    URLConnection con = url.openConnection();
    in = new java.io.BufferedReader(new java.io.InputStreamReader(con.getInputStream()));
    con.setConnectTimeout(1000);
    con.setReadTimeout(4000);
    while ((s = in.readLine()) != null) {
     if (s.length() > 0) {// 如果能够读取到页面则证明可用,tomcat正常,否则继续后面的重启tomcat操作。
      return;
      }
     }
    in.close();
   }catch (Exception ex) {
    //ex.printStackTrace();
    System.out.println("*************该URL有误或不可访问!");
   }
   
   /*if (isTomcatAlive) {
    System.out.println("<" + new Date()+ "> Tomcat is alive but not response!");
    stopTomcat();
   }*/
   RunTomcat runt=new RunTomcat();
   runt.startTomcat(tomcatroot);
  }
  
  /*public static void stopTomcat() {
   try {
    //java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
    java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
    String s;
    String t = "Using JRE_HOME";
    boolean restart = false;
    while ((s = in.readLine()) != null) {
     if (s.indexOf(t) != -1) {
      restart = true;
      break;
     }
    }
    System.out.println("<" + new Date() + "> Tomcat is stop "+ (restart ? "OK" : "ERROR"));
   } catch (Exception e) {
    e.printStackTrace();
   }
  }*/
  
  public static void main(String[] args) {
   System.out.println("********************************************************");
   System.out.println("====本程序自动检测Tomcat运行状况,必要时自动重启Tomcat。====");
   System.out.println("********************************************************");
   
   init_config();
   if(monitorurl.equals(""))monitorurl="http://localhost:8080/ExchangeWeb/checkTomcat/monitor.jsp";
   if(tomcatroot.equals(""))tomcatroot="F://tomcat-6.0.20//";
   if(!tomcatroot.endsWith("//"))tomcatroot+="//";
   while (true) {
    try {
     String random="?random="+Math.random() * 65535;//=====处理数据缓存问题======
     CheckTomcat.checkTomcatIsAlive(monitorurl+random);
     Thread.sleep(5000);
     System.out.println("========================checking at <"+new Date()+">");
    } catch (Exception ex) {
     ex.printStackTrace();
    }
   }
  }
  
  static private void init_config() { 
   try{
    CheckTomcat me=new CheckTomcat();
    String maindir=me.getClass().getResource("/").toURI().getPath();
    System.out.println(">>>>>>配置文件目录:"+maindir);
    String sLine;
    String filename=maindir+"config.xml";
    BufferedReader buffReader = new BufferedReader(new FileReader(filename));
    while((sLine = buffReader.readLine())!=null)
    { 
     sLine = sLine.trim();
     if(sLine.trim()!="" && !sLine.equals("")){
      if(sLine.toLowerCase().startsWith("tomcatroot")){
       int npos=sLine.indexOf("tomcatroot");
       npos+="tomcatroot".length();
       tomcatroot=sLine.substring(npos).trim();
       if(tomcatroot.startsWith("="))tomcatroot=tomcatroot.substring(1);
      }
      else if(sLine.toLowerCase().startsWith("monitorurl")){
       int npos=sLine.indexOf("monitorurl");
       npos+="monitorurl".length();
       monitorurl=sLine.substring(npos).trim();
       if(monitorurl.startsWith("="))monitorurl=monitorurl.substring(1);
      }
     }
    }
    buffReader=null;
   }catch(Exception e){
    e.printStackTrace();
    System.out.println("********************************************************");
    System.out.println("====读取配置文件失败!系统无法运行,请与供应商联系。====");
    System.out.println("********************************************************");
    System.exit(0);
   }
  }


}

 

再是自动重启Tomcat线程类:

import java.util.Date;
 
 public class RunTomcat extends Thread {  private static String tomcatroot="";
  
  public void startTomcat(String root) {
   this.tomcatroot=root;
   
   System.out.println(">>>>>>Tomcat即将启动。。。");
   System.out.println(">>>>>>Tomcat根目录:"+tomcatroot);
   try {
    //java.lang.Process p = java.lang.Runtime.getRuntime().exec("net stop /"Apache Tomcat/"");
    java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//shutdown.bat");
   } catch (Exception e) {
     e.printStackTrace();
   }
   
   try {
    Thread.sleep(3000);//等待shutdown结束
    //RunTomcat me=new RunTomcat();
    //String maindir=me.getClass().getResource("/").toURI().getPath();
    //java.lang.Process p = java.lang.Runtime.getRuntime().exec(maindir+"checkTomcat.bat");
    java.lang.Process p = java.lang.Runtime.getRuntime().exec(tomcatroot+"bin//startup.bat");
    java.io.BufferedReader in = new java.io.BufferedReader(new java.io.InputStreamReader(p.getInputStream()));
    
    /*String s;
    boolean restart = false;
    String t = "Server startup in";
    while ((s = in.readLine()) != null) {
     System.out.println(s);
     if (s.indexOf(t) != -1) {
      restart = true;
      break;
     }
    }*/
    System.out.println(">>>>>>Tomcat start at <" + new Date() + ">");
   } catch (Exception e) {
    e.printStackTrace();
   }
  } }
 接着是检测tomcat是否活动moni

tor.jsp文件:

<%@ page language="java" import="java.util.*" pageEncoding="ISO-8859-1"%>
 <%
 String path = request.getContextPath();
 String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
 out.println("path=" + path + "<br>");
 out.println("basePath=" + basePath + "<br>");
 out.println("ok<br>");
 %>

然后是config.xml配置文件:

tomcatroot=F:/tomcat-6.0.20/
 monitorurl=http://localhost:8080/ExchangeWeb/checkTomcat/monitor.jsp

最后是checkTomcat.bat批处理文件:

@echo off
 rem=========第一步:配置下面的JAVA_HOME为JDK目录==========#
 @set JAVA_HOME=C:/Program Files/Java/jdk1.6.0_14 rem=========第二步:配置下面的CATALINA_HOME为Tomcat目录==========#
 @set CATALINA_HOME=F:/tomcat-6.0.20 @set PATH=%JAVA_HOME%/bin/;
 @set CLASSPATH=%JAVA_HOME%/lib/rt.jar;%JAVA_HOME%/lib/tools.jar;%CATALINA_HOME%/lib/servlet-api.jar;%CATALINA_HOME%/lib/jsp-api.jar; 
 java CheckTomcat  
 测试时,只要双击执行checkTomcat.bat文件即可。

####################################################################

注:以上的方法都没有经过测试,以后有时间一个一个测试一下吧。


https://www.xamrdz.com/backend/3hh1960926.html

相关文章: