java.lang.IllegalStateException: Connection pool shut down 和HttpClientTool工具类

本文介绍了在使用HttpClient时遇到线程池偶发错误的问题,通过设置`setConnectionManagerShared(true)`和调整连接管理器配置来解决。提供了一个基于httpclient4.5版本的HTTP工具类示例,包括GET和POST请求的方法,同时处理了HTTPS请求的证书忽略。优化了连接池管理,确保了线程安全和资源的有效利用。
摘要由CSDN通过智能技术生成

问题

线程池使用多了后,发现偶尔会报这个错,报错了,很影响线上的业务。

在这里插入图片描述

   private static final CloseableHttpClient httpClient;
    public static final String CHARSET = "UTF-8";

    // 采用静态代码块,初始化超时时间配置,再根据配置生成默认httpClient对象
    static {

      RequestConfig config = RequestConfig.custom().setConnectTimeout(60000).setSocketTimeout(15000).build();
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(connManager).setConnectionManagerShared(true).build();
        httpClient = HttpClientBuilder.create().setDefaultRequestConfig(config).build();
    }

查原因

有个大兄弟贴出的解决办法
https://stackoverflow.com/questions/41744410/executorservice-performing-rest-requests
说是要加一个设置 setConnectionManagerShared(true)

大概意思是
使用连接管理配置没有设置共享,设置以后就好了,clientBuilder.setConnectionManagerShared(true)

解决

  private static final CloseableHttpClient httpClient;
    public static final String CHARSET = "UTF-8";

    // 采用静态代码块,初始化超时时间配置,再根据配置生成默认httpClient对象
    static {

        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(1000);
        connManager.setDefaultMaxPerRoute(1000);
        httpClient = HttpClients.custom().setConnectionManager(connManager).setConnectionManagerShared(true).build();
    }

最后贴一个httpClient工具类


import lombok.extern.log4j.Log4j2;
import org.apache.commons.lang3.StringUtils;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.ParseException;
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.ssl.SSLConnectionSocketFactory;
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.BasicNameValuePair;
import org.apache.http.ssl.SSLContextBuilder;
import org.apache.http.ssl.TrustStrategy;
import org.apache.http.util.EntityUtils;

import javax.net.ssl.SSLContext;
import java.io.IOException;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.cert.CertificateException;
import java.security.cert.X509Certificate;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 基于 httpclient 4.5版本的 http工具类
 *
 * @author
 */
@Log4j2
public class HttpClientTool {

    private static final CloseableHttpClient httpClient;
    public static final String CHARSET = "UTF-8";

    // 采用静态代码块,初始化超时时间配置,再根据配置生成默认httpClient对象
    static {

        PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setMaxTotal(1000);
        connManager.setDefaultMaxPerRoute(1000);
        httpClient = HttpClients.custom().setConnectionManager(connManager).setConnectionManagerShared(true).build();

    }


    /**
     * HTTP Post 获取内容
     *
     * @param url     请求的url地址 ?之前的地址
     * @param params  请求的参数
     * @return 页面内容
     * @throws IOException
     */
    public static String doGetAdapt(String url, Map<String, String> params, Map<String, String> head)
            throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        try {
            if (params != null && !params.isEmpty()) {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    String value = entry.getValue();
                    if (value != null) {
                        pairs.add(new BasicNameValuePair(entry.getKey(), value));
                    }
                }
                // 将请求参数和url进行拼接
                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, CHARSET));
            }
            HttpGet httpGet = new HttpGet(url);

            if (head != null && !head.isEmpty()) {
                for (Map.Entry<String, String> entry : head.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }


            CloseableHttpResponse response = httpClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, CHARSET);
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * HTTP Post 获取内容
     *
     * @param url     请求的url地址 ?之前的地址
     * @param params  请求的参数
     * @return 页面内容
     * @throws IOException
     */
    public static String doGetSSLAdapt(String url, Map<String, String> params, Map<String, String> head)
            throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        try {
            if (params != null && !params.isEmpty()) {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    String value = entry.getValue();
                    if (value != null) {
                        pairs.add(new BasicNameValuePair(entry.getKey(), value));
                    }
                }
                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, CHARSET));
            }
            HttpGet httpGet = new HttpGet(url);
            if (head != null && !head.isEmpty()) {
                for (Map.Entry<String, String> entry : head.entrySet()) {
                    httpGet.addHeader(entry.getKey(), entry.getValue());
                }
            }
            // https  注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
            CloseableHttpClient httpsClient = HttpClientTool.createSSLClientDefault();
            CloseableHttpResponse response = httpsClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }



    /**
     * HTTP Post 获取内容
     *
     * @param url     请求的url地址 ?之前的地址
     * @param params  请求的参数
     * @return 页面内容
     * @throws IOException
     */
    public static String doPostadapt(String url, Map<String, String> params, Map<String, String> head)
            throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        List<NameValuePair> pairs = null;
        if (params != null && !params.isEmpty()) {
            pairs = new ArrayList<NameValuePair>(params.size());
            for (Map.Entry<String, String> entry : params.entrySet()) {
                String value = String.valueOf(entry.getValue());
                if (value != null) {
                    pairs.add(new BasicNameValuePair(entry.getKey(), value));
                }
            }
        }
        HttpPost httpPost = new HttpPost(url);
        if (head != null && !head.isEmpty()) {
            for (Map.Entry<String, String> entry : head.entrySet()) {
                httpPost.addHeader(entry.getKey(), entry.getValue());
            }
        }

        if (pairs != null && pairs.size() > 0) {
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, CHARSET));
        }
        CloseableHttpResponse response = null;
        try {
            response = httpClient.execute(httpPost);
            System.out.println(response);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpPost.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;
    }







    /* json
     * @param url
     * @param params
     * @param charset
     * @param token
     * @return java.lang.String
     * @author ybb
     * @date 2021/4/10 21:48
     */
    public static String doPostForJson(String url, String json, String token)
            throws IOException {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        HttpPost httpPost = new HttpPost(url);
        httpPost.addHeader("Content-Type", "application/json");
        httpPost.addHeader("Accept", "application/json");
        //温州的
        if (!StringUtils.isEmpty(token)) {
            httpPost.addHeader("openToken", token);
        }
        CloseableHttpResponse response = null;
        try {

            if (!"".equals(json)) {
                // 这里是关键,后面要跟上字符集格式
                StringEntity params = new StringEntity(json, "utf-8");
                params.setContentEncoding("utf-8");
                // 发送json数据需要设置contentType
                params.setContentType("application/json");
                httpPost.setEntity(params);
            }


            response = httpClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpPost.abort();
                log.error(response);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            return result;
        } catch (ParseException e) {
            e.printStackTrace();
        } finally {
            if (response != null) {
                response.close();
            }
        }
        return null;

    }


    /**
     * HTTPS Get 获取内容
     *
     * @param url     请求的url地址 ?之前的地址
     * @param params  请求的参数
     * @param charset 编码格式
     * @return 页面内容
     */
    public static String doGetSSL(String url, Map<String, String> params, String charset) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        try {
            if (params != null && !params.isEmpty()) {
                List<NameValuePair> pairs = new ArrayList<NameValuePair>(params.size());
                for (Map.Entry<String, String> entry : params.entrySet()) {
                    String value = entry.getValue();
                    if (value != null) {
                        pairs.add(new BasicNameValuePair(entry.getKey(), value));
                    }
                }
                url += "?" + EntityUtils.toString(new UrlEncodedFormEntity(pairs, charset));
            }
            HttpGet httpGet = new HttpGet(url);
            // https  注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
            CloseableHttpClient httpsClient = HttpClientTool.createSSLClientDefault();
            CloseableHttpResponse response = httpsClient.execute(httpGet);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpGet.abort();
                throw new RuntimeException("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }


    /**
     * HTTPS Get 获取内容
     *
     * @param url   请求的url地址 ?之前的地址
     * @param json  请求的参数
     * @param token 编码格式
     * @return 页面内容
     */
    public static String doPostSSL(String url, String json, String token) {
        if (StringUtils.isBlank(url)) {
            return null;
        }
        try {
            HttpPost httpPost = new HttpPost(url);
            if (!"".equals(json)) {
                // 这里是关键,后面要跟上字符集格式
                StringEntity params = new StringEntity(json, "utf-8");
                params.setContentEncoding("utf-8");
                // 发送json数据需要设置contentType
                httpPost.setEntity(params);
                httpPost.addHeader("Content-Type", "application/json");
                if (!StringUtils.isEmpty(token)) {
                    httpPost.addHeader("openToken", token);
                }
            }

            // https  注意这里获取https内容,使用了忽略证书的方式,当然还有其他的方式来获取https内容
            CloseableHttpClient httpsClient = HttpClientTool.createSSLClientDefault();
            CloseableHttpResponse response = httpsClient.execute(httpPost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode != 200) {
                httpPost.abort();
                log.error(response);
                log.error("HttpClient,error status code :" + statusCode);
            }
            HttpEntity entity = response.getEntity();
            String result = null;
            if (entity != null) {
                result = EntityUtils.toString(entity, "utf-8");
            }
            EntityUtils.consume(entity);
            response.close();
            return result;
        } catch (Exception e) {
            log.info("post推送有问题");
            log.info(e);
            e.printStackTrace();
        }
        return null;
    }


    /**
     * 这里创建了忽略整数验证的CloseableHttpClient对象
     *
     * @return
     */
    public static CloseableHttpClient createSSLClientDefault() {
        try {
            SSLContext sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
                // 信任所有
                @Override
                public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                    return true;
                }
            }).build();
            SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext);
            return HttpClients.custom().setSSLSocketFactory(sslsf).build();
        } catch (KeyManagementException e) {
            e.printStackTrace();
        } catch (NoSuchAlgorithmException e) {
            e.printStackTrace();
        } catch (KeyStoreException e) {
            e.printStackTrace();
        }
        return HttpClients.createDefault();
    }
  
}
  • 1
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值