(1)设置setDefaultMaxPerRoute,如果多个线程访问一个url,不设置maxPerRoute的话,呵呵,卡爆你。
RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(1000).setConnectTimeout(1000) .setSocketTimeout(1000).setStaleConnectionCheckEnabled(false).build(); SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();
PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(); poolingHttpClientConnectionManager.setMaxTotal(800); poolingHttpClientConnectionManager.setDefaultMaxPerRoute(700);
CloseableHttpClient httpClientBuilder = HttpClientBuilder.create() .setConnectionManager(poolingHttpClientConnectionManager).setDefaultRequestConfig(requestConfig) .setDefaultSocketConfig(socketConfig).build();
(2)通过代理访问,会一直read超时,线程都block住。 BasicHttpClientConnectionManager connManager = new BasicHttpClientConnectionManager(); connManager.setSocketConfig(SocketConfig.custom().setSoTimeout(2000).build()); 要在socket层面设置读超时。
不好使啊!!!怎么回事
貌似是jdk的bug: https://bugs.java.com/bugdatabase/view_bug.do?bug_id=8182665 SocketInputStream.socketRead0 can hang even with soTimeout set 升级版本应该可以。
|