httpclient的池化
我們經常在應用系統中使用httpclient來傳送http請求,我們通常會new httpclient建立物件,這樣,當請求併發量很大的時候,頻繁的建立、銷燬httpclient會導致不必要的效能開銷,所以有必要對httpclient這個重量級物件進行池化。
在此,我們使用apache提供的common-pool包做相應的事情。
重要程式碼如下:
點選(此處)摺疊或開啟
-
public class HttpClientPool extends GenericObjectPool<HttpClient>{
-
//構造方法接受一個PoolableObjectFactory,來定義物件的建立
-
public HttpClientPool(PoolableObjectFactory<HttpClient> factory) {
-
super(factory);
-
}
-
-
//自定義
-
public <T> T doPost(HttpMethod method, HttpClientDataCallback<T> callback) {
-
HttpClient toUse = null;
-
try {
-
toUse = borrowObject();
-
toUse.executeMethod(method);
-
T rel = callback.handleResponse(method.getResponseBodyAsString());
-
return rel;
-
} catch (Exception e) {
-
logger.error(\"failed to execute http request.\", e);
-
return null;
-
} finally {
-
try {
-
method.releaseConnection();
-
} catch (Exception e) {
-
//in case fail, ignore and return object
-
}
-
if (toUse != null) {
-
try {
-
returnObject(toUse);
-
} catch (Exception e) {
-
logger.warn(\"failed to return http client\", e);
-
}
-
}
-
}
-
}
- }
PoolableHttpClientFactory:該類實現對池化物件的初始建立動作
點選(此處)摺疊或開啟
- public class PoolableHttpClientFactory implements PoolableObjectFactory<HttpClient> {
-
private int contimeout; //Connection time out
-
private int sotimeout; //so time out
-
-
public PoolableHttpClientFactory(int contimeout, int sotimeout) {
-
this.contimeout = contimeout;
-
this.sotimeout = sotimeout;
-
}
-
-
/*物件的生成在此定義
-
*(non-Javadoc)
-
* @see org.apache.commons.pool.PoolableObjectFactory#makeObject()
-
*/
-
@Override
-
public HttpClient makeObject() throws Exception {
-
HttpClient httpClient = new HttpClient();
-
HttpConnectionManagerParams configParams = httpClient.getHttpConnectionManager().getParams();
-
configParams.setConnectionTimeout(contimeout);
-
configParams.setSoTimeout(sotimeout);
-
httpClient.getParams().setConnectionManagerTimeout(contimeout);
-
httpClient.getParams().setParameter(HttpMethodParams.HTTP_CONTENT_CHARSET, \"UTF-8\");
-
return httpClient;
-
}
- }
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/28912557/viewspace-1223241/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- Http持久連線與HttpClient連線池HTTPclient
- Httpclient 介面自動化HTTPclient
- HttpClient4.X 升級 入門 + http連線池使用HTTPclient
- HttpclientHTTPclient
- C# HttpClient使用和注意事項,.NET Framework連線池併發限制C#HTTPclientFramework
- 工具篇:apache-httpClient 和 jdk11-HttpClient的使用ApacheHTTPclientJDK
- 聊聊jdk httpclient的executorJDKHTTPclient
- HttpClient 不能做的事情HTTPclient
- HttpClient的簡單使用HTTPclient
- 解密httpclient,dbcp,jedis,c3p0,druid,okhttp都在使用的連線池技術解密HTTPclientUI
- httpclient 4.5.3HTTPclient
- 影像的卷積和池化操作卷積
- 影象的卷積和池化操作卷積
- 物件池Pools優化物件優化
- HttpURLConnection和HttpClient的使用HTTPclient
- Apache之HttpClientApacheHTTPclient
- HttpClient介紹HTTPclient
- HttpClient 完整教程HTTPclient
- 【HttpClient】httpclient之post 方法(引數為Map型別)HTTPclient型別
- Apache httpclient的execute方法除錯ApacheHTTPclient除錯
- 聊聊jdk httpclient的retry引數JDKHTTPclient
- WebApi系列~HttpClient的效能隱患WebAPIHTTPclient
- HttpClient 基本功能的使用HTTPclient
- 受HttpClient困繞的問題HTTPclient
- 池化技術總結
- 共享池最佳化思路
- Go - httpclient 常用操作GoHTTPclient
- HttpClient--入門HTTPclient
- HttpClient 簡單例子HTTPclient單例
- 小心 HttpClient 中的 FormUrlEncodeContent 的 bugHTTPclientORM
- openGauss 資源池化效能最佳化
- [case39]聊聊jdk httpclient的executorJDKHTTPclient
- Java BasePooledObjectFactory 物件池化技術JavaObject物件
- HttpClient請求工具類HTTPclient
- httpclient基礎知識HTTPclient
- Web介面測試-HttpClientWebHTTPclient
- HttpClient初步使用方法HTTPclient
- HttpClient 下載檔案HTTPclient