如何在java中检查连接池是否有效?

时间:2015-09-24 07:07:07

标签: java tcp connection-pooling

我正在使用PoolingHttpClientConnectionManager,我的代码工作正常,但我想知道我的代码是否正在使用PoolingHttpClientConnectionManager资源 我已将其与CloseableHttpClient集成,如下所示

PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();    
cm.setMax(3);    
    client = HttpClients.custom()
                        .setSSLSocketFactory(sslSocketFactory)
                        .setConnectionManager(cm)
                        .build();

所以我尝试过使用wire shark并使用tcp.flags.ack==0 && tcp.flags.syn==1

进行过滤

当我使用它时,我看到ssl握手正在为每个请求做出 例如,我使用的是for循环,它向服务器发出10个请求,如下所示

for(i=0;i<10;i++)
 {

    CloseableHttpResponse response2 = client.execute(httpPost);
    response2.close();
 }

我看到为上述请求创建了10个TCP数据包(已跟踪)。我是以错误的方式使用PoolingHttpClientConnectionManager还是正确的?

更新 添加进口 我的完整代码:

import java.io.IOException;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.UnknownHostException;

import javax.net.ssl.SSLContext;

import org.apache.http.HttpClientConnection;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHost;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.protocol.HttpClientContext;
import org.apache.http.config.Registry;
import org.apache.http.config.RegistryBuilder;
import org.apache.http.conn.HttpClientConnectionManager;
import org.apache.http.conn.routing.HttpRoute;
import org.apache.http.conn.socket.ConnectionSocketFactory;
import org.apache.http.conn.socket.PlainConnectionSocketFactory;
import org.apache.http.conn.ssl.NoopHostnameVerifier;
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.protocol.HttpContext;
import org.apache.http.util.EntityUtils;
import org.glassfish.jersey.SslConfigurator;
import org.json.JSONException;
import org.json.JSONObject;
import com.google.gson.JsonObject;  
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

public class Test{
static PoolingHttpClientConnectionManager cm;
static CloseableHttpClient client;

static String path="www.example.com";    
static
    {


    SslConfigurator sslConfig = SslConfigurator.newInstance()
            .securityProtocol("TLS")
            .keyStoreFile("/path")
            .keyStorePassword("passw")
            .keyStoreType("JKS")
            .trustStoreFile("/path");

    SSLContext sslCtx = sslConfig.createSSLContext();
    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslCtx,NoopHostnameVerifier.INSTANCE);
    HttpClientContext clientContext = HttpClientContext.create();


    final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
            .register("http", PlainConnectionSocketFactory.getSocketFactory())
            .register("https", sslSocketFactory)
            .build();


    cm = new PoolingHttpClientConnectionManager(registry);

    client = HttpClients.custom()
            .setSSLSocketFactory(sslSocketFactory)
            .setConnectionManager(cm)
            .build();

}
public static void main(String a[]) throws ClientProtocolException, IOException, JSONException
{

    JSONObject jsonResponse;
    JsonObject jsonRequest = null;
    jsonRequest.addProperty("id","number");



     StringEntity se = new StringEntity(jsonRequest.toString());

    HttpPost httpPost = new HttpPost(path);
    httpPost.setEntity(se);
    httpPost.setHeader("Accept", "application/json");
    httpPost.setHeader("Content-type", "application/json");
    httpPost.setHeader("Connection", "keep-alive");
    CloseableHttpResponse response2; 
    DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SSS");



    int i;
    for(i=0;i<10;i++)
   {
        response2 = client.execute(httpPost);

    System.out.println(response2.getStatusLine());
    HttpEntity entity2 = response2.getEntity();

    String result = EntityUtils.toString(entity2);
    System.out.println(result);

    Date date = new Date();
    System.out.println(dateFormat.format(date));
   response2.close();
   }

}
}

1 个答案:

答案 0 :(得分:0)

执行从打开到关闭的事务,然后在关闭netstat

后查看Connection.以查看与数据库的TCP连接是否仍然是ESTABLISHED