Java CloseableHttpClient以及如何从连接池使用它?

Java CloseableHttpClient以及如何从连接池使用它?,java,httpclient,connection-pooling,closeablehttpresponse,Java,Httpclient,Connection Pooling,Closeablehttpresponse,我为池创建了一个connectionManager,如下-->> 我正在使用-->> 但在这里,每次我使用PoolightTPClientConnectionManager创建CloseableHttpClient的实例时。我不知道这是不是正确的方法 在我进行HTTP调用的类中,我是这样做的--->> 请建议最好的方法或替代方案?我也是一个新手,这样做是为了学习,所以如果我犯了任何错误,请原谅并纠正我 public static PoolingHttpClientConnectionMa

我为池创建了一个connectionManager,如下-->>

我正在使用-->>

但在这里,每次我使用PoolightTPClientConnectionManager创建CloseableHttpClient的实例时。我不知道这是不是正确的方法

在我进行HTTP调用的类中,我是这样做的--->>

请建议最好的方法或替代方案?我也是一个新手,这样做是为了学习,所以如果我犯了任何错误,请原谅并纠正我

    public static PoolingHttpClientConnectionManager getCm() {
        return cm;
    }

    public static void setCm(PoolingHttpClientConnectionManager cm) {
        ClassName.cm = cm;
    }

public static PoolingHttpClientConnectionManager createPool()
        {
            System.out.println("Generating new connection pool");
            setCm( new PoolingHttpClientConnectionManager());   
            getCm().setMaxTotal(10);
            getCm().setDefaultMaxPerRoute(5);
            return getCm();
        }
    public static CloseableHttpClient createConnection(){
            if(getCm()!=null)
                return  HttpClients.custom().setConnectionManager(getCm()).setConnectionManagerShared(true).setDefaultRequestConfig(getConfig()).build();
            else
                return HttpClients.custom().setConnectionManager(createPool()).setConnectionManagerShared(true).setDefaultRequestConfig(getConfig()).build();
        }
private static CloseableHttpClient client;   //class level variable. static or final? Which one to prefer? I want this to handle more than 50 concurrent request

Inside My method--->>

client = createConnection();

String endPoint = //someendpoint 
HttpPost httpPost = new HttpPost(endPoint);
httpPost.setHeader("Content-type", "application/json");
httpPost.setHeader("Accept-API-Version", "resource=2.0, protocol=1.0");


CloseableHttpResponse response1 = null;
        try{
             response1 =  client.execute(httpPost);
                String responseString;
                int statusCode;
                responseString = EntityUtils.toString(response1.getEntity());
         //doing something useful
         }catch(){
          
          } finally(){           //Another big question comes here. How to close and what should I close?
                                 /Just to be on the safe side, I am closing everything
           httpPost.releaseConnection();
           if(response1!= null)
                        response1.close();
           if (client != null)
                        client.close();
        

         }