public static InetSocketAddress createWithResolveRetry ( String hostname , int port , int delayMillis , int maxAttempt ) { InetSocketAddress socketAddress ; int attempts = 0 ; do { socketAddress = new InetSocketAddress ( hostname , port ) ; // if dns failed, try one more time if ( socketAddress . isUnresolved ( ) ) { attempts ++ ; LOG . info ( String . format ( "failed to resolve host %s, attempt %d" , hostname , attempts ) ) ; try { Thread . sleep ( delayMillis ) ; } catch ( InterruptedException e ) { Thread . currentThread ( ) . interrupt ( ) ; } } else if ( attempts > 0 ) { LOG . info ( String . format ( "successful resolution on attempt %d" , attempts ) ) ; } } while ( socketAddress . isUnresolved ( ) && attempts < maxAttempt ) ; return socketAddress ; }
Utility function to create an InetSocketAddress that has been resolved . Retries with a small sleep in between