private HttpURLConnection openConnection ( String uri , String method ) { for ( String host : hosts ) { try { URL templateUrl = new URL ( "http://" + host + uri ) ; HttpURLConnection connection = ( HttpURLConnection ) templateUrl . openConnection ( ) ; connection . setRequestMethod ( method ) ; connection . setConnectTimeout ( timeout ) ; connection . setUseCaches ( false ) ; if ( method . equalsIgnoreCase ( "POST" ) || method . equalsIgnoreCase ( "PUT" ) ) { connection . setDoOutput ( true ) ; } connection . connect ( ) ; return connection ; } catch ( IOException e ) { LOGGER . error ( "Error connecting to {}: {}" , host , e ) ; } } return null ; }
Open a new HttpUrlConnection in case it fails it tries for the next host in the configured list