📄 listener.java
字号:
package test;
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 ( "tomcat5.exe" ) ;
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);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -