PoolHttpConnectionManager

package cn.xlink.parkinglots.client.utils;

import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * http连接池管理
 */
public class PoolHttpConnectionManager {

   private static final Logger                    logger    = LoggerFactory.getLogger(PoolHttpConnectionManager.class);
   private static final PoolHttpConnectionManager singleton = new PoolHttpConnectionManager();


   private final CloseableHttpClient httpClient;
   private final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

   public static final PoolHttpConnectionManager instance() {
      return singleton;
   }

   public void init(int max, int per) {
      cm.setMaxTotal(max);
      cm.setDefaultMaxPerRoute(per);
   }

   private PoolHttpConnectionManager() {
      httpClient = HttpClients.custom().setConnectionManager(cm).build();
   }

   public CloseableHttpClient getHttpClient() {
      return httpClient;
   }


}

// private static       PoolingHttpClientConnectionManager clientConnectionManager = null;
// private static       CloseableHttpClient                httpClient              = null;
// private static       RequestConfig                      config                  = RequestConfig.custom().setCookieSpec(CookieSpecs
//       .STANDARD_STRICT).build();
// private final static Object                             syncLock                = new Object();
//
// /**
//  * 创建httpclient连接池并初始化
//  */
// @PostConstruct
// private void init() {
//    try {
//       //添加对https的支持,该sslContext没有加载客户端证书
//       // 如果需要加载客户端证书,请使用如下sslContext,其中KEYSTORE_FILE和KEYSTORE_PASSWORD分别是你的证书路径和证书密码
//       //KeyStore keyStore  =  KeyStore.getInstance(KeyStore.getDefaultType()
//       //FileInputStream instream =   new FileInputStream(new File(KEYSTORE_FILE));
//       //keyStore.load(instream, KEYSTORE_PASSWORD.toCharArray());
//       //SSLContext sslContext = SSLContexts.custom().loadKeyMaterial(keyStore,KEYSTORE_PASSWORD.toCharArray())
//       // .loadTrustMaterial(null, new TrustSelfSignedStrategy())
//       //.build();
//       SSLContext sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build();
//       SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, SSLConnectionSocketFactory
//             .getDefaultHostnameVerifier());
//       Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("https",
//             sslsf).register("http", PlainConnectionSocketFactory.getSocketFactory()).build();
//       clientConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
//       clientConnectionManager.setMaxTotal(50);
//       clientConnectionManager.setDefaultMaxPerRoute(25);
//    } catch (Exception e) {
//       logger.warn("httpUtils init get exception:", e);
//    }
// }
//
// public static CloseableHttpClient getHttpClient() {
//    if (httpClient == null) {
//       synchronized (syncLock) {
//          if (httpClient == null) {
//             CookieStore       cookieStore = new BasicCookieStore();
//             BasicClientCookie cookie      = new BasicClientCookie("sessionID", "######");
//             cookie.setDomain("#####");
//             cookie.setPath("/");
//             cookieStore.addCookie(cookie);
//             httpClient = HttpClients.custom().setConnectionManager(clientConnectionManager).setDefaultCookieStore(cookieStore)
//                   .setDefaultRequestConfig(config).build();
//          }
//       }
//    }
//    return httpClient;
// }
//
// /**
//  * get请求
//  *
//  * @param url
//  * @param headers
//  * @return
//  */
// public static HttpEntity httpGet(String url, Map<String, Object> headers) {
//    CloseableHttpClient httpClient = getHttpClient();
//    HttpRequest         httpGet    = new HttpGet(url);
//    if (headers != null && !headers.isEmpty()) {
//       httpGet = setHeaders(headers, httpGet);
//    }
//    CloseableHttpResponse response = null;
//    try {
//       response = httpClient.execute((HttpGet) httpGet);
//       HttpEntity entity = response.getEntity();
//       return entity;
//    } catch (Exception e) {
//       e.printStackTrace();
//
//    }
//    return null;
// }
//
// /**
//  * post请求,使用json格式传参
//  *
//  * @param url
//  * @param headers
//  * @param data
//  * @return
//  */
// public static HttpEntity httpPost(String url, Map<String, Object> headers, String data) {
//    CloseableHttpClient httpClient = getHttpClient();
//    HttpRequest         request    = new HttpPost(url);
//    if (headers != null && !headers.isEmpty()) {
//       request = setHeaders(headers, request);
//    }
//    CloseableHttpResponse response = null;
//
//    try {
//       HttpPost httpPost = (HttpPost) request;
//       httpPost.setEntity(new StringEntity(data, ContentType.create("application/json", "UTF-8")));
//       response = httpClient.execute(httpPost);
//       HttpEntity entity = response.getEntity();
//       return entity;
//    } catch (IOException e) {
//       e.printStackTrace();
//    }
//    return null;
// }
//
// /**
//  * 使用表单键值对传参
//  */
// public static HttpEntity PostForm(String url, Map<String, Object> headers, List<NameValuePair> data) {
//    CloseableHttpClient httpClient = getHttpClient();
//    HttpRequest         request    = new HttpPost(url);
//    if (headers != null && !headers.isEmpty()) {
//       request = setHeaders(headers, request);
//    }
//    CloseableHttpResponse response = null;
//    UrlEncodedFormEntity  uefEntity;
//    try {
//       HttpPost httpPost = (HttpPost) request;
//       uefEntity = new UrlEncodedFormEntity(data, "UTF-8");
//       httpPost.setEntity(uefEntity);
//       // httpPost.setEntity(new StringEntity(data, ContentType.create("application/json", "UTF-8")));
//       response = httpClient.execute(httpPost);
//       HttpEntity entity = response.getEntity();
//       return entity;
//    } catch (IOException e) {
//       e.printStackTrace();
//
//    }
//    return null;
// }
//
// /**
//  * 设置请求头信息
//  *
//  * @param headers
//  * @param request
//  * @return
//  */
// private static HttpRequest setHeaders(Map<String, Object> headers, HttpRequest request) {
//    for (Map.Entry entry : headers.entrySet()) {
//       if (!entry.getKey().equals("Cookie")) {
//          request.addHeader((String) entry.getKey(), (String) entry.getValue());
//       } else {
//          Map<String, Object> Cookies = (Map<String, Object>) entry.getValue();
//          for (Map.Entry entry1 : Cookies.entrySet()) {
//             request.addHeader(new BasicHeader("Cookie", (String) entry1.getValue()));
//          }
//       }
//    }
//    return request;
// }
//
// public static Map<String, String> getCookie(String url) {
//    CloseableHttpClient   httpClient = getHttpClient();
//    HttpRequest           httpGet    = new HttpGet(url);
//    CloseableHttpResponse response   = null;
//    try {
//       response = httpClient.execute((HttpGet) httpGet);
//       Header[]            headers = response.getAllHeaders();
//       Map<String, String> cookies = new HashMap<String, String>();
//       for (Header header : headers) {
//          cookies.put(header.getName(), header.getValue());
//       }
//       return cookies;
//    } catch (Exception e) {
//       e.printStackTrace();
//
//    }
//    return null;
// }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值