美文网首页
java.lang.IllegalStateException:

java.lang.IllegalStateException:

作者: 媛猿YY | 来源:发表于2022-08-01 11:25 被阅读0次

最近项目中压测,利用httpclient访问接口报错。错误信息如下:


image.png
  • 错误原因分析:
    并发调用同一个httpclient去请求数据导致,当上一个post|get请求尚未结束时,又启新的线程再次使用该httpclient请求数据。

  • 解决方案:
    采用线程池,从线程池中获取httpclient。

改用线程池中访问https\http链接类型接口完整代码,使用方法:

  • 工具类
public class HttpClientUtils {
    private static CloseableHttpClient HTTPCLIENT;
    private static Logger logger = LoggerFactory.getLogger(HttpClientUtils.class);
    private static RequestConfig REQUESTCONFIG = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setConnectTimeout(1000)
            .setConnectionRequestTimeout(1000)
            .build();
    static PoolingHttpClientConnectionManager cm;

    static {
        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

        HTTPCLIENT = HttpClientBuilder.create().setRetryHandler((exception, executionCount, context) -> {
            return false;
        }).setDefaultRequestConfig(REQUESTCONFIG).setConnectionManager(cm).build();

    }


    public static String get(String url) throws IOException {
        try {

            HttpGet httpget = new HttpGet(url);
            ResponseHandler<String> responseHandler = new ResponseHandler<String>() {
                @Override
                public String handleResponse(
                        final HttpResponse response) throws ClientProtocolException, IOException {
                    int status = response.getStatusLine().getStatusCode();
                    if (status >= 200 && status < 300) {
                        HttpEntity entity = response.getEntity();
                        return entity != null ? EntityUtils.toString(entity) : null;
                    } else {
                        logger.error("请求{},出现了错误{}", url, status);
                        return "出错了";
                        //throw new ClientProtocolException("Unexpected response status: " + status);
                    }
                }

            };
            String responseBody = HTTPCLIENT.execute(httpget, responseHandler);
            return responseBody;
        } finally {

        }
    }

    public static String post(String url,Map map) throws IOException {
        String result = null;
        HttpPost post = new HttpPost(url);
//        System.out.println(map);
        post.setHeader("content-type","application/json");
        String param = JSON.toJSONString(map);
        StringEntity entity = new StringEntity(param,"UTF-8");
        post.setEntity(entity);
        try {
            HttpResponse response = HTTPCLIENT.execute(post);
            result = EntityUtils.toString(response.getEntity(),"UTF-8");
            // System.out.println(result);
           return result;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return "";

    }


    public static HttpResponse postResponse(String url,Map map) throws IOException {
        String result = null;
        HttpPost post = new HttpPost(url);
//        System.out.println(map);
        post.setHeader("content-type","application/json");
        String param = JSON.toJSONString(map);
        StringEntity entity = new StringEntity(param,"UTF-8");
        post.setEntity(entity);
        try {
            HttpResponse response = HTTPCLIENT.execute(post);
            System.out.println(response.getAllHeaders());
            System.out.println(response.getEntity());
            result = EntityUtils.toString(response.getEntity(),"UTF-8");
            // System.out.println(result);
            return response;

        } catch (IOException e) {
            e.printStackTrace();
        }

        return null;

    }
}
  • 调接口
HttpPost post = new HttpPost(url);
String result = HttpClientUtils.post(url, map);

相关文章

网友评论

      本文标题:java.lang.IllegalStateException:

      本文链接:https://www.haomeiwen.com/subject/msqrwrtx.html