com.ksyun.api.sdk.http.ClientHttpRequestFactoryUtils Maven / Gradle / Ivy
package com.ksyun.api.sdk.http;
import org.apache.http.impl.NoConnectionReuseStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
public class ClientHttpRequestFactoryUtils {
private static Integer maxConnTotal = 200000;
private static Integer maxConnPerRoute = 5000;
private static Integer connectTimeOut=1000;
private static Integer readTimeout=30000;
public static ClientHttpRequestFactory getHttpComponentsClientHttpRequestFactory() {
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setMaxConnPerRoute(maxConnPerRoute);
builder.setMaxConnTotal(maxConnTotal);
builder.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
// 设置重试一次
CloseableHttpClient request = builder.build();
HttpComponentsClientHttpRequestFactory factory = new HttpComponentsClientHttpRequestFactory(
request);
factory.setConnectTimeout(connectTimeOut);
factory.setReadTimeout(readTimeout);
// 连接不够用的等待时间,不宜过长,必须设置,比如连接不够用时,时间过长将是灾难性的
factory.setConnectionRequestTimeout(50);
return factory;
}
public static void init(ClientHttpRequestFactoryConfig config){
maxConnTotal=config.getMaxConnTotal();
maxConnPerRoute=config.getMaxConnPerRoute();
connectTimeOut=config.getConnectTimeOut();
readTimeout=config.getReadTimeout();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy