在Java領域,談到網絡編程,可能大家腦海里第一反應就是MINA,NETTY,GRIZZLY等優秀的開源框架。沒錯,不過在深入探究這些框架之前,我們需要先從最original的技術探究開始(當然,需要大家先熟悉java.net.*類庫)。這里,我要和大家分享一下HttpComponents項目的部分組件特性。HttpClient,想必大家早都接觸過了吧。HttpComponents和HttpClient的”血緣“有點像guava和google-collection的關系。目前,HttpComponents已經是Apache的頂級項目了,它旨在為我們提供一個Http協議相關的Java平臺工具集。它的代碼組織很精妙,主要分兩部分,一部分是核心工具集(包括HttpCore-bio,HttpCore-nio,HttpClient,HttpMIme,HttpCookie等),一部分是擴展工具集(目前主要包括ssl)
HttpClient主要包括Connection management,Status management,Authentication Management三部分。下面給出對它的二次封裝,經過了線上的接近半年的驗證(這里指的是httpClient 3,httpClient 4還有待檢驗),可以看做是一個高性能的Client封裝吧。感興趣的朋友可以根據apache的MPM IO模型進行部分參數的調整。
先來段httpClient 4的封裝,代碼如下:
[java] view plaincopyprint?
/**
* @author von gosling 2012-3-2
*/
public class HttpComponentsClientExecutor implements DisposableBean {
private static final int DEFAULT_MAX_TOTAL_CONNECTIONS = 100;
private static final int DEFAULT_MAX_CONNECTIONS_PER_ROUTE = 5; //notice IE 6,7,8
private static final int DEFAULT_CONN_TIMEOUT_MILLISECONDS = 5 * 1000;
private static final int DEFAULT_READ_TIMEOUT_MILLISECONDS = 60 * 1000;
private static final String HTTP_HEADER_CONTENT_ENCODING = "Content-Encoding";
private static final String ENCODING_GZIP = "gzip";
private HttpClient httpClient;
/**
* Create a new instance of the HttpComponentsClient with a default
* {@link HttpClient} that uses a default
* {@link org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager}.
*/
public HttpComponentsClientExecutor() {
SchemeRegistry schemeRegistry = new SchemeRegistry();
schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager(
schemeRegistry);
connectionManager.setMaxTotal(DEFAULT_MAX_TOTAL_CONNECTIONS);
connectionManager.setDefaultMaxPerRoute(DEFAULT_MAX_CONNECTIONS_PER_ROUTE);
this.httpClient = new DefaultHttpClient(connectionManager);
setConnectTimeout(DEFAULT_CONN_TIMEOUT_MILLISECONDS);
setReadTimeout(DEFAULT_READ_TIMEOUT_MILLISECONDS);
}
/**
* Create a new instance of the HttpComponentsClient with the given
* {@link HttpClient} instance.
*
* @param httpClient the HttpClient instance to use for this request
*/
public HttpComponentsClientExecutor(HttpClient httpClient) {
Validate.notNull(httpClient, "HttpClient must not be null");
//notice: if you want to custom exception recovery mechanism
//you should provide an implementation of the HttpRequestRetryHandler interface.
this.httpClient = httpClient;
}
/**
* Set the {@code HttpClient} used by this request.
*/
public void setHttpClient(HttpClient httpClient) {
this.httpClient = httpClient;
}
/**
* Return the {@code HttpClient} used by this request.
*/
public HttpClient getHttpClient() {
return this.httpClient;
}
/**
* Set the connection timeout for the underlying HttpClient. A timeout value
* of 0 specifies an infinite timeout.
*
* @param timeout the timeout value in milliseconds
*/
public void setConnectTimeout(int timeout) {
Validate.isTrue(timeout >= 0, "Timeout must be a non-negative value");
getHttpClient().getParams().setIntParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
timeout);
}
/**
* Set the socket timeout (SO_TIMEOUT) in milliseconds, which is the timeout
* for waiting for data or, put differently, a maximum period inactivity
* between two consecutive data packets.A timeout value of 0 specifies an
* infinite timeout.
*
* @param timeout the timeout value in milliseconds
*/
public void setReadTimeout(int timeout) {
Validate.isTrue(timeout >= 0, "Timeout must be a non-negative value");
原文轉自:http://www.anti-gravitydesign.com