HttpClientPool 发请求的池子,感觉快不了多少,谁知道为什么?

import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.pool.PoolStats;
import org.apache.http.util.EntityUtils;

public class HttpClientPool {
	private static final int DEFAULT_POOL_MAX_TOTAL = 200;
	private static final int DEFAULT_POOL_MAX_PER_ROUTE = 100;

	private static final int DEFAULT_CONNECT_TIMEOUT = 500;
	private static final int DEFAULT_CONNECT_REQUEST_TIMEOUT = 500;
	private static final int DEFAULT_SOCKET_TIMEOUT = 2000;

	private PoolingHttpClientConnectionManager gcm = null;

	private CloseableHttpClient httpClient = null;

	private IdleConnectionMonitorThread idleThread = null;

	// 连接池的最大连接数
	private final int maxTotal;
	// 连接池按route配置的最大连接数
	private final int maxPerRoute;

	// tcp connect的超时时间
	private final int connectTimeout;
	// 从连接池获取连接的超时时间
	private final int connectRequestTimeout;
	// tcp io的读写超时时间
	private final int socketTimeout;
	
	private static class HolderClass {
		private static final HttpClientPool CLAZZ = new HttpClientPool();
	}
	
	public static HttpClientPool getInstance() {
		return HolderClass.CLAZZ;
	}
	
	private HttpClientPool() {
		this(HttpClientPool.DEFAULT_POOL_MAX_TOTAL, 
				HttpClientPool.DEFAULT_POOL_MAX_PER_ROUTE,
				HttpClientPool.DEFAULT_CONNECT_TIMEOUT,
				HttpClientPool.DEFAULT_CONNECT_REQUEST_TIMEOUT,
				HttpClientPool.DEFAULT_SOCKET_TIMEOUT);
	}

	private HttpClientPool(int maxTotal, int maxPerRoute, 
			int connectTimeout, int connectRequestTimeout, int socketTimeout) {
		this.maxTotal = maxTotal;
		this.maxPerRoute = maxPerRoute;
		this.connectTimeout = connectTimeout;
		this.connectRequestTimeout = connectRequestTimeout;
		this.socketTimeout = socketTimeout;

//		Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
//				.register("http", PlainConnectionSocketFactory.getSocketFactory())
//				.register("https", SSLConnectionSocketFactory.getSocketFactory()).build();
//		this.gcm = new PoolingHttpClientConnectionManager(registry);
		// default ok
		this.gcm = new PoolingHttpClientConnectionManager();
		this.gcm.setMaxTotal(this.maxTotal);
		this.gcm.setDefaultMaxPerRoute(this.maxPerRoute);

		RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(this.connectTimeout) // 设置连接超时
				.setSocketTimeout(this.socketTimeout) // 设置读取超时
				.setConnectionRequestTimeout(this.connectRequestTimeout) // 设置从连接池获取连接实例的超时
				.build();

		HttpClientBuilder httpClientBuilder = HttpClients.custom();
		httpClient = httpClientBuilder.setConnectionManager(this.gcm).setDefaultRequestConfig(requestConfig).build();

		idleThread = new IdleConnectionMonitorThread(this.gcm);
		idleThread.start();
	}
	
	public String getPoolStats() {
		PoolStats totalStats = this.gcm.getTotalStats();
		return totalStats.toString();
	}
	
	public void getAsync() {
		
	}

	@SuppressWarnings("unchecked")
	public String doGet(String url) {
		return this.doGet(url, Collections.EMPTY_MAP, Collections.EMPTY_MAP);
	}
	
	@SuppressWarnings("unchecked")
	public String doGet(String url, Map<String, Object> params) {
		return this.doGet(url, Collections.EMPTY_MAP, params);
	}

	public String doGet(String url, Map<String, String> headers, Map<String, Object> params) {
		// *) 构建GET请求头
		String apiUrl = getUrlWithParams(url, params);
		HttpGet httpGet = new HttpGet(apiUrl);

		// *) 设置header信息
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				httpGet.addHeader(entry.getKey(), entry.getValue());
			}
		}

		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response == null || response.getStatusLine() == null) {
				return null;
			}

			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity entityRes = response.getEntity();
				if (entityRes != null) {
					return EntityUtils.toString(entityRes, "UTF-8");
				}
			}
			return null;
		} catch (IOException e) {
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
				}
			}
		}
		return null;
	}
	
	
	@SuppressWarnings("unchecked")
	public int doGetStatus(String url) {
		System.out.println("douzi:" + getPoolStats());
		return this.doGetStatus(url, Collections.EMPTY_MAP, Collections.EMPTY_MAP);
	}
	
	@SuppressWarnings("unchecked")
	public int doGetStatus(String url, Map<String, Object> params) {
		return this.doGetStatus(url, Collections.EMPTY_MAP, params);
	}
	/**
	 * <p><b>Title:</b> doGetStatus</p>
	 * <p><b>Description:</b> 特殊场景使用,只需要知道请求的结果是否是200</p>
	 * @author douzi
	 * @param url
	 * @return
	 */
	public int doGetStatus(String url, Map<String, String> headers, Map<String, Object> params) {
		// *) 构建GET请求头
		String apiUrl = getUrlWithParams(url, params);
		HttpGet httpGet = new HttpGet(apiUrl);

		// *) 设置header信息
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				httpGet.addHeader(entry.getKey(), entry.getValue());
			}
		}

		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpGet);
			if (response == null || response.getStatusLine() == null) {
				return 0;
			}

			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				return 1;
			}
			return 0;
		} catch (IOException e) {
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
				}
			}
		}
		return 0;
	}

	@SuppressWarnings("unchecked")
	public String doPost(String apiUrl, Map<String, Object> params) {
		return this.doPost(apiUrl, Collections.EMPTY_MAP, params);
	}

	public String doPost(String apiUrl, Map<String, String> headers, Map<String, Object> params) {
		HttpPost httpPost = new HttpPost(apiUrl);
		// *) 配置请求headers
		if (headers != null && headers.size() > 0) {
			for (Map.Entry<String, String> entry : headers.entrySet()) {
				httpPost.addHeader(entry.getKey(), entry.getValue());
			}
		}

		// *) 配置请求参数
		if (params != null && params.size() > 0) {
			HttpEntity entityReq = getUrlEncodedFormEntity(params);
			httpPost.setEntity(entityReq);
		}

		CloseableHttpResponse response = null;
		try {
			response = httpClient.execute(httpPost);
			if (response == null || response.getStatusLine() == null) {
				return null;
			}

			int statusCode = response.getStatusLine().getStatusCode();
			if (statusCode == HttpStatus.SC_OK) {
				HttpEntity entityRes = response.getEntity();
				if (entityRes != null) {
					return EntityUtils.toString(entityRes, "UTF-8");
				}
			}
			return null;
		} catch (IOException e) {
		} finally {
			if (response != null) {
				try {
					response.close();
				} catch (IOException e) {
				}
			}
		}
		return null;

	}

	private HttpEntity getUrlEncodedFormEntity(Map<String, Object> params) {
		List<NameValuePair> pairList = new ArrayList<NameValuePair>(params.size());
		for (Map.Entry<String, Object> entry : params.entrySet()) {
			NameValuePair pair = new BasicNameValuePair(entry.getKey(), entry.getValue().toString());
			pairList.add(pair);
		}
		return new UrlEncodedFormEntity(pairList, Charset.forName("UTF-8"));
	}

	private String getUrlWithParams(String url, Map<String, Object> params) {
		boolean first = true;
		StringBuilder sb = new StringBuilder(url);
		for (String key : params.keySet()) {
			char ch = '&';
			if (first == true) {
				ch = '?';
				first = false;
			}
			String value = params.get(key).toString();
			try {
				String sval = URLEncoder.encode(value, "UTF-8");
				sb.append(ch).append(key).append("=").append(sval);
			} catch (UnsupportedEncodingException e) {
			}
		}
		return sb.toString();
	}

	public void shutdown() {
		idleThread.shutdown();
	}

	// 监控有异常的链接
	private class IdleConnectionMonitorThread extends Thread {
		private final HttpClientConnectionManager connMgr;
		private volatile boolean exitFlag = false;

		public IdleConnectionMonitorThread(HttpClientConnectionManager connMgr) {
			this.connMgr = connMgr;
			setDaemon(true);
		}

		@Override
		public void run() {
			while (!this.exitFlag) {
				synchronized (this) {
					try {
						this.wait(2000);
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
				// 关闭失效的连接
				connMgr.closeExpiredConnections();
				// 可选的, 关闭30秒内不活动的连接
				connMgr.closeIdleConnections(30, TimeUnit.SECONDS);
			}
		}

		public void shutdown() {
			this.exitFlag = true;
			synchronized (this) {
				notify();
			}
		}

	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值