简体   繁体   中英

Java 8 Apache HttpClient - slow connection

I have an ExecutorService which runs Runnables like that:

public class Rexecutor implements Runnable {

    @Override
    public void run() {
        while(thisUseAmount.intValue() <= amountToGoal) {
            try {
                requests();
            } catch (Exception ex) {
                //blah blah
            }
        }
    }

    private void requests() throws Exception {
        <<HttpRequests (Gets, Posts, Puts) through HttpClient>>
    }

}

Those requests are executing via proxy (choosen random 1-1000 proxy addresses) and sometimes it hits "lazy" proxy which is veery slow. To solve this, I'm reseting proxy IP for thread which was running more than few minutes (average thread loop is 30 sec). The problem is that even when I changed proxy address for this thread more than x times it is still running slow. Could you tell me why? Is there any HttpClient option that doesn't allow to change connection speed or something?

PS. I can't provide you working example because I can't give you access to the proxies.

I suggest you to use Apache HttpClient instead default provided with JDK.https://hc.apache.org/httpcomponents-client-ga/examples.html

After hours of searching I found a solution:

private HttpClientBuilder client = HttpClientBuilder.create().setRetryHandler(new DefaultHttpRequestRetryHandler(Integer.MAX_VALUE, true));

Just add retry handler :)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM