你写个java程序,过一段时间(比如一秒)就去取本机上的网页,如果取不到就重起

解决方案 »

  1.   

    写个socket程序,定时去连接tomcat的监听端口,如果失败就重新启动tomcat
      

  2.   

    package com.bes.tomcat ;import java.util.Timer ;
    import java.util.TimerTask ;
    import java.net.Socket ;
    import java.net.* ;
    import java.io.* ;public class Listener
        extends TimerTask
    {
        private int count = 0 ;
        private int defaultPort = 8080 ;
        public void run ()
        {
            count ++ ;
            log("run times:" + count);
            if ( isTomcatRunning () )
            {
                log ( "Running" ) ;
            }
            else
            {
                log ( "Not Running" ) ;
                startup();
            }
        }    public void startup()
        {
            Runtime runtime = Runtime.getRuntime() ;
            Process testProcess = null ;
            try
            {
                testProcess = runtime.exec ( "startup.bat" ) ;
                try
                {
                    testProcess.waitFor () ;
                }
                catch ( InterruptedException ex1 )
                {
                    ex1.printStackTrace();
                }
                InputStream in = testProcess.getInputStream() ;
                byte[] bytes = new byte[in.available()];
                in.read(bytes) ;
                log(" [ OUTPUT ] "+new String(bytes));
            }
            catch ( IOException ex )
            {
                ex.printStackTrace();
            }
        }
        public boolean isTomcatRunning ()
        {
            return isTomcatRunning ( defaultPort ) ;
        }    public boolean isTomcatRunning ( int port )
        {
            boolean returnBoolean = false ;
            Socket socket = null ;
            try
            {
                socket = new Socket ( "localhost" , port ) ;
                returnBoolean = true ;
            }
            catch ( UnknownHostException ex )
            {
                log ( ex.getMessage () ) ;
                returnBoolean = false ;
            }
            catch ( IOException ex )
            {
                log ( ex.getMessage () ) ;
                returnBoolean = false ;
            }
            finally
            {
                if(socket != null)
                {
                    try
                    {
                        socket.close () ;
                    }
                    catch ( IOException ex1 )
                    {
                        log(ex1.getMessage());
                    }
                }
            }
            return returnBoolean ;    }    private void log ( String message )
        {
            System.out.println ( "[ TomcatListener ] :" + message ) ;
        }    public static void main ( String[] args )
        {
            Timer timer = new Timer ( false ) ;
        //        timer.schedule(new Listener(),new Date());
            timer.schedule(new Listener(),0,1000 * 5);
        }
    }
      

  3.   

    你写的程序可能不是很准确,在unix下面按照你的程序去处理,就会出现多个tomcat进程同时运行的情况
      

  4.   

    怎么不准确,tomcat不能有两个或两个以上的进程,因为会端口冲突的我测试过了在windows下没问题,在linux下作简单修改就可以了