@ -108,33 +108,37 @@ public class TimeoutRequest<E> {
* @param uri
* @param timeout
* @return true if the server exists and replies within the given time - out
* @throws ExecutionException
* /
public static boolean ping ( final String host , final int port , final int timeout ) throws ExecutionException {
return new TimeoutRequest < Boolean > ( new Callable < Boolean > ( ) {
@Override
public Boolean call ( ) {
//long time = System.currentTimeMillis();
try {
final Socket socket = new Socket ( ) ;
//System.out.println("PING socket create = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
socket . connect ( new InetSocketAddress ( host , port ) , timeout ) ;
//System.out.println("PING socket connect = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
if ( socket . isConnected ( ) ) {
socket . close ( ) ;
return Boolean . TRUE ;
public static boolean ping ( final String host , final int port , final int timeout ) {
try {
return new TimeoutRequest < Boolean > ( new Callable < Boolean > ( ) {
@Override
public Boolean call ( ) {
//long time = System.currentTimeMillis();
try {
final Socket socket = new Socket ( ) ;
//System.out.println("PING socket create = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
socket . connect ( new InetSocketAddress ( host , port ) , timeout ) ;
//System.out.println("PING socket connect = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
if ( socket . isConnected ( ) ) {
socket . close ( ) ;
return Boolean . TRUE ;
}
//System.out.println("PING socket close = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
} catch ( final UnknownHostException e ) {
//System.out.println("PING socket UnknownHostException = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
} catch ( final IOException e ) {
//System.out.println("PING socket IOException = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
}
//System.out.println("PING socket close = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
} catch ( final UnknownHostException e ) {
//System.out.println("PING socket UnknownHostException = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
} catch ( final IOException e ) {
//System.out.println("PING socket IOException = " + (System.currentTimeMillis() - time) + " ms (" + host + ":" + port + ")"); time = System.currentTimeMillis();
return Boolean . FALSE ;
}
}
} ) . call ( timeout ) . booleanValue ( ) ;
} ) . call ( timeout ) . booleanValue ( ) ;
} catch ( ExecutionException ex ) { // may happen on Timeout (see call)
return false ;
}
}
/ * *