简体   繁体   English

Java 8 Apache HttpClient - 连接缓慢

[英]Java 8 Apache HttpClient - slow connection

I have an ExecutorService which runs Runnables like that:我有一个 ExecutorService 运行 Runnables 像这样:

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.这些请求是通过代理执行的(选择随机的 1-1000 个代理地址),有时它会遇到非常慢的“懒惰”代理。 To solve this, I'm reseting proxy IP for thread which was running more than few minutes (average thread loop is 30 sec).为了解决这个问题,我正在为运行超过几分钟的线程重置代理 IP(平均线程循环为 30 秒)。 The problem is that even when I changed proxy address for this thread more than x times it is still running slow.问题是,即使我更改此线程的代理地址超过 x 次,它仍然运行缓慢。 Could you tell me why?你能告诉我为什么吗? Is there any HttpClient option that doesn't allow to change connection speed or something?是否有任何 HttpClient 选项不允许更改连接速度之类的?

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.我建议您使用 Apache HttpClient 而不是 JDK 提供的默认值。https://hc.apache.org/httpcomponents-client-ga/examples.htmlhttps://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 :)只需添加重试处理程序:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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