Httpclient多线程连接池封装
来源:互联网 发布:手机淘宝怎样发送链接 编辑:程序博客网 时间:2024/05/18 01:15
1、封装
package com.suning.search.utils;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.HttpVersion;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.scheme.PlainSocketFactory;
import org.apache.http.conn.scheme.Scheme;
import org.apache.http.conn.scheme.SchemeRegistry;
import org.apache.http.conn.ssl.SSLSocketFactory;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.impl.conn.PoolingClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.BasicHttpParams;
import org.apache.http.params.CoreConnectionPNames;
import org.apache.http.params.HttpParams;
import org.apache.http.params.HttpProtocolParams;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class CommonHttpClient {
private static final Logger log = LoggerFactory.getLogger(CommonHttpClient.class);
private static final String CHARSET = "UTF-8";
private static Map<ClientName, HttpClient> clientMap = new HashMap<ClientName, HttpClient>(
ClientName.values().length);
public static int HTTP_CONNECT_TIMEOUT = 5000;
public static int HTTP_SO_TIMEOUT = 5000;
public static int HTTP_POOL_TIMEOUT = 1000;
public static int HTTP_MAX_ROUTE_CONNECTIONS = 128;
public static int HTTP_MAX_TOTAL_CONNECTIONS = 10;
private CommonHttpClient() {
}
public static void initHttpClients() {
clientMap.clear();
}
/**
* http client模块名称的枚举类
*/
public static enum ClientName {
DEFAULT_CLIENT
}
public static class ClientParam {
private int poolTimeout;
private int connectionTimeout;
private int soTimeout;
private int maxRouteConnections;
private int maxTotalConnections;
public ClientParam(int poolTimeout, int connectionTimeout,
int soTimeout, int maxRouteConnections, int maxTotalConnections) {
this.poolTimeout = poolTimeout;
this.connectionTimeout = connectionTimeout;
this.soTimeout = soTimeout;
this.maxRouteConnections = maxRouteConnections;
this.maxTotalConnections = maxTotalConnections;
}
public int getPoolTimeout() {
return poolTimeout;
}
public void setPoolTimeout(int poolTimeout) {
this.poolTimeout = poolTimeout;
}
public int getConnectionTimeout() {
return connectionTimeout;
}
public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
}
public int getSoTimeout() {
return soTimeout;
}
public void setSoTimeout(int soTimeout) {
this.soTimeout = soTimeout;
}
public int getMaxRouteConnections() {
return maxRouteConnections;
}
public void setMaxRouteConnections(int maxRouteConnections) {
this.maxRouteConnections = maxRouteConnections;
}
public int getMaxTotalConnections() {
return maxTotalConnections;
}
public void setMaxTotalConnections(int maxTotalConnections) {
this.maxTotalConnections = maxTotalConnections;
}
@Override
public String toString() {
return "ClientParam [poolTimeout=" + poolTimeout
+ ", connectionTimeout=" + connectionTimeout
+ ", soTimeout=" + soTimeout + ", maxRouteConnections="
+ maxRouteConnections + ", maxTotalConnections="
+ maxTotalConnections + "]";
}
}
/**
* 根据http client名称获取对应的参数
*
* @param clientName CommonHttpClient.HTTP_SPES_INTERFACE_CLIENT,
* CommonHttpClient.SUNING_DOMAIN_CLIENT
* @return ClientParam
*/
private static ClientParam getClientParam(ClientName clientName) {
ClientParam param = null;
switch (clientName) {
// default httpclient 初始化参数
case DEFAULT_CLIENT:
param = new ClientParam(HTTP_POOL_TIMEOUT,
HTTP_CONNECT_TIMEOUT,
HTTP_SO_TIMEOUT,
HTTP_MAX_ROUTE_CONNECTIONS,
HTTP_MAX_TOTAL_CONNECTIONS);
break;
default:
param = new ClientParam(HTTP_POOL_TIMEOUT,
HTTP_CONNECT_TIMEOUT,
HTTP_SO_TIMEOUT,
HTTP_MAX_ROUTE_CONNECTIONS,
HTTP_MAX_TOTAL_CONNECTIONS);
break;
}
return param;
}
/**
* 创建httpclient
*/
@SuppressWarnings("deprecation")
public static synchronized HttpClient createHttpClient(ClientName clientName) {
HttpClient httpClient = clientMap.get(clientName);
if (httpClient != null) {
return httpClient;
}
ClientParam clientParam = getClientParam(clientName);
HttpParams params = new BasicHttpParams();
// 设置一些基本参数
HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
HttpProtocolParams.setContentCharset(params, CHARSET);
HttpProtocolParams.setUseExpectContinue(params, true);
HttpProtocolParams
.setUserAgent(
params,
"Mozilla/5.0(Linux;U;Android 2.2.1;en-us;Nexus One Build.FRG83) "
+ "AppleWebKit/553.1(KHTML,like Gecko) Version/4.0 Mobile Safari/533.1");
// 超时设置
/* 从连接池中取连接的超时时间 */
/* 连接超时 */
params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT,
clientParam.getConnectionTimeout());
/* 请求超时 */
params.setParameter(CoreConnectionPNames.SO_TIMEOUT,
clientParam.getSoTimeout());
params.setParameter(CoreConnectionPNames.SO_KEEPALIVE,"true");
// 设置我们的HttpClient支持HTTP和HTTPS两种模式
SchemeRegistry schReg = new SchemeRegistry();
schReg.register(new Scheme("http", 80, PlainSocketFactory
.getSocketFactory()));
schReg.register(new Scheme("https", 443, SSLSocketFactory
.getSocketFactory()));
// 使用线程安全的连接管理来创建HttpClient
PoolingClientConnectionManager pccm = new PoolingClientConnectionManager(
schReg);
// MAX_ROUTE_CONNECTIONS为要设置的每个路由最大连接数
pccm.setDefaultMaxPerRoute(clientParam.getMaxRouteConnections());
pccm.setMaxTotal(clientParam.getMaxTotalConnections());
HttpClient client = new DefaultHttpClient(pccm, params);
clientMap.put(clientName, client);
return client;
}
/**
* 根据http客户端名获取相应的连接实例
*
* @param clientName
* @return
*/
public static HttpClient getHttpClient(ClientName clientName) {
HttpClient httpClient = clientMap.get(clientName);//由于在类的初始化时并没有赋值,所以必定为null
if (null == httpClient) {
httpClient = createHttpClient(clientName);
}
return httpClient;
}
public static String get(String url, ClientName clientName) {
try {
HttpGet httpget = new HttpGet(url);
HttpResponse response = getHttpClient(clientName).execute(httpget);
HttpEntity httpEntity = response.getEntity();
String result = null;
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity);
EntityUtils.consume(httpEntity);
}
return result;
} catch (UnsupportedEncodingException e) {
log.error("http encoding error!url:" + url, e.getMessage());
return null;
} catch (ClientProtocolException e) {
log.error("http client protocol error!url:" + url, e.getMessage());
return null;
} catch (IOException e) {
log.error("连接失败!url:" + url, e.getStackTrace());
return null;
// throw new RuntimeException("连接失败" + e.getMessage(), e);
}
}
/**
* 发送form类型的post请求
*
* @param url
* @param map
* @return
*/
@SuppressWarnings("rawtypes")
public static String post(String url, Map map, ClientName clientName) {
try {
// 编码参数
List<NameValuePair> nvps = new ArrayList<NameValuePair>();
Iterator it = map.entrySet().iterator();
while (it.hasNext()) {
Map.Entry entry = (Map.Entry) it.next();
String key = (String) entry.getKey();
String value = (String) entry.getValue();
NameValuePair nvp = new BasicNameValuePair(key, value);
nvps.add(nvp);
}
UrlEncodedFormEntity entity = new UrlEncodedFormEntity(nvps,
CHARSET);
// 创建POST请求
HttpPost request = new HttpPost(url);
request.setEntity(entity);
// 发送请求;
HttpResponse response = getHttpClient(clientName).execute(request);
if (response.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
throw new RuntimeException("请求失败");
}
HttpEntity resEntity = response.getEntity();
String result = null;
if (resEntity != null) {
EntityUtils.toString(resEntity, CHARSET);
// resEntity.consumeContent();
EntityUtils.consume(resEntity);
}
return result;
} catch (UnsupportedEncodingException e) {
log.error("http encoding error!url:" + url, e.getMessage());
return null;
} catch (ClientProtocolException e) {
log.error("http client protocol error!url:" + url, e.getMessage());
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
/**
* 发送字符串做为参数的post请求
*
* @param url
* @param str
* @return
*/
public static String post(String url, String str, ClientName clientName) {
try {
HttpPost httppost = new HttpPost(url);
StringEntity reqEntity = new StringEntity(str, CHARSET);
httppost.setEntity(reqEntity);
HttpResponse response = getHttpClient(clientName).execute(httppost);
HttpEntity httpEntity = response.getEntity();
String result = null;
if (httpEntity != null) {
result = EntityUtils.toString(httpEntity);
// httpEntity.consumeContent();
EntityUtils.consume(httpEntity);
}
return result;
} catch (UnsupportedEncodingException e) {
log.error("http encoding error!!url:" + url, e.getMessage());
return null;
} catch (ClientProtocolException e) {
log.error("http client protocol error!!url:" + url, e.getMessage());
return null;
} catch (IOException e) {
log.error("连接失败!url:" + url, e.getMessage());
return null;
}
}
}
2、应用:
private final static String QUERY_URL = "http://10.10.10.10/qa?rewriteq=";
private List<Term> getNorthParticiple(String input){
List<Term> terms = new ArrayList<Term>();
String word_utf;
try {
word_utf = URLEncoder.encode(input, "UTF8");
String query_str = QUERY_URL.concat(word_utf);
String response = CommonHttpClient.get(query_str, CommonHttpClient.ClientName.DEFAULT_CLIENT);
if (response != null) {
JsonNode jsonNode = objectMapper.readTree(response);
ArrayNode rewriteResult = (ArrayNode) jsonNode.get("query_seg");
if (rewriteResult.size() > 0){
int len = rewriteResult.size();
int tempLength = 0;
for(int i=0;i<len;i++){
Term term = new Term();
JsonNode tempNode2 = rewriteResult.get(i);
JsonNode tempWrite = tempNode2.get("t");
String termResult = tempWrite.asText();
int startPos = tempLength;
int endPos = tempLength+termResult.length();
tempLength = endPos;
term.setStartPos(startPos);
term.setEndPos(endPos);
term.setTerm(termResult);
term.setType("wordNorth");
terms.add(term);
}
}
}
} catch (Exception e) {
logger.error(e.getMessage(), e);
}
return terms;
}
- Httpclient多线程连接池封装
- 封装多线程的HttpClient
- 轻松把玩HttpClient之封装HttpClient工具类(八),优化启用Http连接池策略
- httpclient http连接池
- httpClient连接池配置
- HttpClient连接池使用
- HttpClient的连接池
- HttpClient 连接池使用
- httpclient使用连接池
- HttpClient连接池配置文件
- 使用httpclient连接池
- HttpClient封装
- httpclient连接池的使用
- httpclient连接池的使用
- httpclient http连接池 demo
- httpClient 多线程
- httpclient 备注 控制连接时间及多线程错误
- Http请求连接池 - HttpClient 连接池
- Linux学习掌握(三):CentOS中安装(卸载)Mysql以及数据库的导入导出
- Scikit-learn使用总结
- Hrbust 2302 Another Tree【思维+Dfs】
- 一个理解装饰器很好的例子
- java实现https请求
- Httpclient多线程连接池封装
- MySQL 5.6 for Windows 解压缩版配置安装(win 10 64位亲测)附安装包下载链接
- 网贷信息强制披露又升级,零逾期率造假平台无所遁形!!
- 机器学习实战笔记(3.2)-朴素贝叶斯算法(贝努力模型代码实现)
- 对磁盘扇区数据的访问
- 几个常用的命令
- ASP.NET页面之间传值的方式之Application
- Spring boot + Spring Security 实现用户登录管理
- 鬼才卡夫卡随笔录