httpclient連線池的配置方法
HttpClient和Lucene一樣,每個版本的API都變化很大,這有點讓人頭疼。
筆者碰到的情況最早在hadoop環境用httpclient發起httpjson請求,在本地除錯的時候用的4.5.2版本,放到hadoop環境中執行報錯,無奈只好將httpclient降級成4.2.5版本。最後終於用了低版本的httpclient完成了任務。
如下是我這邊整理了一下兩個版本連線池的方法:
1、httpclient4.X版本以上,httpclient4.3版本過時:
/**
*
* 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常
* 不用匯入SSL證書 私有函式,初始化連線池
*因大部分的hadoop本身帶的httpclient4.2.5版本,故將4.X的部分放到這個地方
* @param maxTotalConn
* 最大連線數
* @param maxRouteConn
* 每路由最大連線數
*/
private synchronized void initConnManager(int maxTotalConn, int maxRouteConn) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, null);
SSLSocketFactory ssf = new SSLSocketFactory(ctx,
SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory
.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, ssf.getSocketFactory()));
cm = new ThreadSafeClientConnManager(schemeRegistry);
cm.setMaxTotal(maxTotalConn);
cm.setDefaultMaxPerRoute(maxRouteConn);
} catch (KeyManagementException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (NoSuchAlgorithmException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
2、httpclient4.3以上版本:
/**
* 獲得https的連線
* 避免HttpClient的”SSLPeerUnverifiedException: peer not authenticated”異常
* 不用匯入SSL證書.
* 私有函式,初始化連線池
*
* @param maxTotalConn
* 最大連線數
* @param maxRouteConn
* 每路由最大連線數
*/
private synchronized void initConnManager(int maxTotalConn, int maxRouteConn) {
try {
SSLContext ctx = SSLContext.getInstance("TLS");
X509TrustManager tm = new X509TrustManager() {
@Override
public void checkClientTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
@Override
public void checkServerTrusted(
java.security.cert.X509Certificate[] arg0, String arg1)
throws java.security.cert.CertificateException {
}
@Override
public java.security.cert.X509Certificate[] getAcceptedIssuers() {
return null;
}
};
ctx.init(null, new TrustManager[] { tm }, new SecureRandom());
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
ctx, NoopHostnameVerifier.INSTANCE);
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory> create()
.register("http", plainsf).register("https", socketFactory)
.build();
cm = new PoolingHttpClientConnectionManager(registry);
// 將最大連線數增加
cm.setMaxTotal(maxTotalConn);
// 將每個路由基礎的連線增加
cm.setDefaultMaxPerRoute(maxRouteConn);
} catch (Exception e) {
e.printStackTrace();
throw new RuntimeException();
}
}