java使用HttpClient线程池支持大量并发发起http请求

import org.apache.commons.lang3.StringUtils;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.*;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
	* @ClassName: HttpClientPoolUtil 
	* @Description: 链接池
	* @author lisc 
	* @date 2019年7月9日 上午11:35:26 
	* @version V1.0 
 */
public class HttpClientPoolUtil {
	private static Logger logger = LoggerFactory.getLogger(HttpClientPoolUtil.class);
	public static PoolingHttpClientConnectionManager cm = null;
	public static CloseableHttpClient httpClient = null;
	/** * 默认content 类型 */
	private static final String DEFAULT_CONTENT_TYPE = "application/json";
	/** * 默认请求超时时间30s */
	private static final int DEFAUL_TTIME_OUT = 15000;
	private static final int count = 32;
	private static final int totalCount = 1000;
	private static final int Http_Default_Keep_Time =15000;

	/** * 初始化连接池 */
	public static synchronized void initPools() {
		if (httpClient == null) {
			cm = new PoolingHttpClientConnectionManager();
			cm.setDefaultMaxPerRoute(count);
			cm.setMaxTotal(totalCount);
			httpClient = HttpClients.custom().setKeepAliveStrategy(defaultStrategy).setConnectionManager(cm).build();
		}
	}

	/**
	 *  Http connection keepAlive 设置 
	 */
	public static ConnectionKeepAliveStrategy defaultStrategy = new ConnectionKeepAliveStrategy() {
		public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
			HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
			int keepTime = Http_Default_Keep_Time;
			while (it.hasNext()) {
				HeaderElement he = it.nextElement();
				String param = he.getName();
				String value = he.getValue();
				if (value != null && param.equalsIgnoreCase("timeout")) {
					try {
						return Long.parseLong(value) * 1000;
					} catch (Exception e) {
						e.printStackTrace();
						logger.error("format KeepAlive timeout exception, exception:" + e.toString());
					}
				}
			}
			return keepTime * 1000;
		}
	};

	public static CloseableHttpClient getHttpClient() {
		return httpClient;
	}

	public static PoolingHttpClientConnectionManager getHttpConnectionManager() {
		return cm;
	}

	/**
	 * 执行http post请求 
	 * 默认采用Content-Type:application/json,Accept:application/json
	 * @param uri 请求地址 
	 * @param data 请求数据 
	 * @return
	 */
	public static String execute(String uri, String data) {
		long startTime = System.currentTimeMillis();
		HttpEntity httpEntity = null;
		HttpEntityEnclosingRequestBase method = null;
		String responseBody = "";
		try {
			if (httpClient == null) {
				initPools();
			}
			method = (HttpEntityEnclosingRequestBase) getRequest(uri, HttpPost.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
			method.setEntity(new StringEntity(data));
			HttpContext context = HttpClientContext.create();
			CloseableHttpResponse httpResponse = httpClient.execute(method, context);
			httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				responseBody = EntityUtils.toString(httpEntity, "UTF-8");
			}
		} catch (Exception e) {
			if (method != null) {
				method.abort();
			}
			e.printStackTrace();
			logger.error("execute post request exception, url:" + uri + ", exception:" + e.toString()
					+ ", cost time(ms):" + (System.currentTimeMillis() - startTime));
		} finally {
			if (httpEntity != null) {
				try {
					EntityUtils.consumeQuietly(httpEntity);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("close response exception, url:" + uri + ", exception:" + e.toString()
							+ ", cost time(ms):" + (System.currentTimeMillis() - startTime));
				}
			}
		}
		return responseBody;
	}

	/**
	 * * 创建请求
	 * @param uri 请求url 
	 * @param methodName 请求的方法类型
	* 	@param contentType contentType类型 
	*  @param timeout 超时时间 
	* @param @return    入参
	* @return HttpRequestBase    返回类型
	* @author lisc 
	* @throws
	* @date 2019年7月9日 上午11:37:00 
	* @version V1.0   
	 */
	public static HttpRequestBase getRequest(String uri, String methodName, String contentType, int timeout) {
		if (httpClient == null) {
			initPools();
		}
		HttpRequestBase method = null;
		if (timeout <= 0) {
			timeout = DEFAUL_TTIME_OUT;
		}
		RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(timeout * 1000)
				.setConnectTimeout(timeout * 1000).setConnectionRequestTimeout(timeout * 1000)
				.setExpectContinueEnabled(false).build();
		if (HttpPut.METHOD_NAME.equalsIgnoreCase(methodName)) {
			method = new HttpPut(uri);
		} else if (HttpPost.METHOD_NAME.equalsIgnoreCase(methodName)) {
			method = new HttpPost(uri);
		} else if (HttpGet.METHOD_NAME.equalsIgnoreCase(methodName)) {
			method = new HttpGet(uri);
		} else {
			method = new HttpPost(uri);
		}
		if (StringUtils.isBlank(contentType)) {
			contentType = DEFAULT_CONTENT_TYPE;
		}
		method.addHeader("Content-Type", contentType);
		method.addHeader("Accept", contentType);
		method.setConfig(requestConfig);
		return method;
	}

	/** 
	 * 执行GET 请求 
	 *  @param uri 
	 *  @return
	 */
	public static String execute(String uri) {
		long startTime = System.currentTimeMillis();
		HttpEntity httpEntity = null;
		HttpRequestBase method = null;
		String responseBody = "";
		try {
			if (httpClient == null) {
				initPools();
			}
			method = getRequest(uri, HttpGet.METHOD_NAME, DEFAULT_CONTENT_TYPE, 0);
			HttpContext context = HttpClientContext.create();
			CloseableHttpResponse httpResponse = httpClient.execute(method, context);
			httpEntity = httpResponse.getEntity();
			if (httpEntity != null) {
				responseBody = EntityUtils.toString(httpEntity, "UTF-8");
				logger.info("请求URL: " + uri + "+ 返回状态码:" + httpResponse.getStatusLine().getStatusCode());
			}
		} catch (Exception e) {
			if (method != null) {
				method.abort();
			}
			e.printStackTrace();
			logger.error("execute get request exception, url:" + uri + ", exception:" + e.toString() + ",cost time(ms):"
					+ (System.currentTimeMillis() - startTime));
		} finally {
			if (httpEntity != null) {
				try {
					EntityUtils.consumeQuietly(httpEntity);
				} catch (Exception e) {
					e.printStackTrace();
					logger.error("close response exception, url:" + uri + ", exception:" + e.toString()
							+ ",cost time(ms):" + (System.currentTimeMillis() - startTime));
				}
			}
		}
		return responseBody;
	}
}

 

转载于:https://my.oschina.net/lisc2016/blog/3071491

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值