public static Proxy getProxy ( final IProxyProvider provider , final boolean test ) throws IOException { LOGGER . info ( "Getting proxy..." ) ; List < Proxy > proxies = provider . getProxies ( - 1 , false , true ) ; LOGGER . info ( "Total Proxies: " + proxies . size ( ) ) ; final CountDownLatch countDown = new CountDownLatch ( proxies . size ( ) ) ; final AtomicReference < Proxy > useProxy = new AtomicReference <> ( ) ; int threadCount = proxies . size ( ) > 10 ? 10 : proxies . size ( ) ; if ( threadCount <= 0 ) { threadCount = 1 ; } final ExecutorService testService = Executors . newFixedThreadPool ( threadCount ) ; for ( final Proxy proxy : proxies ) { Thread validate = new Thread ( ) { @ Override public void run ( ) { try { if ( test ) { if ( proxy . isBlackListed ( ) ) { return ; } proxy . updateStatus ( ) ; if ( ! proxy . isOnline ( ) ) { return ; } proxy . updateAnonymity ( ) ; if ( ! proxy . isAnonymous ( ) ) { return ; } } if ( useProxy . get ( ) == null ) { LOGGER . info ( "Using Proxy: " + proxy . toString ( ) ) ; useProxy . set ( proxy ) ; // count to 0 while ( countDown . getCount ( ) > 0 ) { countDown . countDown ( ) ; } } } catch ( Exception e ) { LOGGER . info ( "Proxy validation returned error: " + e . getClass ( ) . getName ( ) + " - " + e . getMessage ( ) ) ; } finally { countDown . countDown ( ) ; } } } ; testService . submit ( validate ) ; } try { countDown . await ( ) ; } catch ( InterruptedException e ) { e . printStackTrace ( ) ; } testService . shutdownNow ( ) ; if ( useProxy . get ( ) == null ) { //if null, use another provider return getProxy ( test ) ; } return useProxy . get ( ) ; }
Get a Proxy using the given provider