httpClient 4.5.2 实现连接池
来源:互联网 发布:软件著作权的申请 编辑:程序博客网 时间:2024/05/17 23:10
如下代码- public class PoolHttpsClientService {
-
-
-
- private static final SimpleLogger LOGGER = SimpleLogger.getLogger(PoolHttpsClientService.class);
-
-
- private static final String CHAR_SET = "UTF-8";
-
-
-
- @Value("${InetAddressStr}")
- private String InetAddressStr;
-
-
-
- @Value("${InetPort}")
- private int InetPort;
-
-
-
-
-
- private static int MAX_CONNECTION_NUM = 400;
-
-
-
-
-
- private static int MAX_PER_ROUTE = 80;
-
-
-
-
-
- private static int SERVER_REQUEST_TIME_OUT = 2000;
-
-
-
-
-
- private static int SERVER_RESPONSE_TIME_OUT = 2000;
-
-
-
-
-
- private PoolHttpsClientService() {
- }
-
-
- private static Object LOCAL_LOCK = new Object();
-
-
-
-
- PoolingHttpClientConnectionManager cm = null;
-
-
-
-
-
-
-
-
-
-
- private PoolingHttpClientConnectionManager getPoolManager() {
- final String methodName = "getPoolManager";
- LOGGER.enter(methodName, "initPoolManager");
- if (null == cm) {
- synchronized (LOCAL_LOCK) {
- if (null == cm) {
- SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
- try {
- sslContextBuilder.loadTrustMaterial(null, new TrustSelfSignedStrategy());
- SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(
- sslContextBuilder.build());
- Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory> create()
- .register("https", socketFactory)
- .register("http", new PlainConnectionSocketFactory())
- .build();
- cm = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
- cm.setMaxTotal(MAX_CONNECTION_NUM);
- cm.setDefaultMaxPerRoute(MAX_PER_ROUTE);
- } catch (Exception e) {
- LOGGER.error(methodName, "init PoolingHttpClientConnectionManager Error" + e);
- }
-
-
- }
- }
- }
- LOGGER.exit(methodName, "initPoolManager");
- return cm;
- }
-
-
-
-
-
-
-
-
-
- public CloseableHttpClient getHttpsClient(RequestConfig config) {
- final String methodName = "getHttpsClient";
- LOGGER.enter(methodName, "initHttpsClient");
- CloseableHttpClient httpClient = HttpClients.custom().setDefaultRequestConfig(config)
- .setConnectionManager(this.getPoolManager())
- .build();
- LOGGER.exit(methodName, "initHttpsClient");
- return httpClient;
- }
-
-
-
-
-
-
-
-
- public String poolHttpsPost(String url, JSONObject json) {
- final String methodName = "poolHttpsPost";
- LOGGER.enter(methodName, json);
- CloseableHttpResponse response = null;
- HttpPost post = null;
- try {
-
- HttpHost proxy = new HttpHost(InetAddressStr, InetPort);
-
-
- RequestConfig requestConfig = RequestConfig.custom().setProxy(proxy)
- .setSocketTimeout(SERVER_REQUEST_TIME_OUT).setConnectTimeout(SERVER_RESPONSE_TIME_OUT).build();
- post = new HttpPost(url);
- post.setConfig(requestConfig);
-
-
- if (json != null) {
- StringEntity se = new StringEntity(json.toString(), CHAR_SET);
- se.setContentEncoding(new BasicHeader(HTTP.CONTENT_TYPE, "application/json;"));
- post.setEntity(se);
- }
-
-
- LOGGER.info(methodName, "start post to weixin");
- response = getHttpsClient(requestConfig).execute(post);
- LOGGER.info(methodName, "end post to weixin");
- int status = response.getStatusLine().getStatusCode();
- LOGGER.info(methodName, "return status:" + status);
-
-
- String result = null;
- if (status == 200) {
- result = EntityUtils.toString(response.getEntity(), CHAR_SET);
- }
- EntityUtils.consume(response.getEntity());
- response.close();
- LOGGER.exit(methodName);
- return result;
- } catch (Exception e) {
- if (e instanceof SocketTimeoutException) {
-
- LOGGER.error(methodName, "server request time out");
- } else if (e instanceof ConnectTimeoutException) {
-
- LOGGER.error(methodName, "server response time out");
- }
- LOGGER.error(methodName, e.getMessage());
- } finally {
- post.releaseConnection();
- if (response != null) {
- try {
- EntityUtils.consume(response.getEntity());
- response.close();
- } catch (IOException e) {
- LOGGER.error(methodName, e.getMessage());
- }
- }
- }
- LOGGER.exit(methodName);
-
- return null;
- }
0 0