关于 httpclient4.5 连接池的做法
- httpclient 4.5 连接池管理器的应用
<small> PS:今天终天有空写一个给自己备份</small>
public PoolingHttpClientConnectionManager getPoolCm(){
// SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
// sslContextBuilder.loadTrustMaterial(null,
// new TrustSelfSignedStrategy());
// SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
// sslContextBuilder.build());
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加到200
cm.setMaxTotal(200);
// 将每个路由基础的连接增加到20
cm.setDefaultMaxPerRoute(20);
//请求重试处理
return cm;
}
public CloseableHttpClient gethttpClient(){
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception,int executionCount, HttpContext context) {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// ssl握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler)
.build();
return httpClient;
}
public String poolHttpsPost(String url, JSONObject json) {
CloseableHttpResponse response = null;
HttpPost post = null;
try {
// 设置代理 可省略
HttpHost proxy = new HttpHost(InetAddressStr, InetPort);
// connectTimeout设置服务器请求超时时间
// socketTimeout设置服务器响应超时时间
RequestConfig requestConfig = RequestConfig.custom()
.setProxy(proxy).setSocketTimeout(SERVER_REQUEST_TIME_OUT)
.setConnectTimeout(SERVER_RESPONSE_TIME_OUT).build();
post = new HttpPost(url);
post.setConfig(requestConfig);
if (json != null) {
StringEntity se = new StringEntity(json.toString(), CHAR_SET);
se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE,
"application/json;"));
post.setEntity(se);
}
response = getHttpsClient(requestConfig).execute(post);
int status = response.getStatusLine().getStatusCode();
String result = null;
if (status == 200) {
result = EntityUtils.toString(response.getEntity(), CHAR_SET);
}
EntityUtils.consume(response.getEntity());
response.close();
return result;
} catch (Exception e) {
if (e instanceof SocketTimeoutException) {
// 服务器请求超时
} else if (e instanceof ConnectTimeoutException) {
// 服务器响应超时(已经请求了)
}
} finally {
post.releaseConnection();
if (response != null) {
try {
EntityUtils.consume(response.getEntity());
response.close();
} catch (IOException e) {
}
}
}
// 超时或者网络不通时返回值
return null;
}