2015-09-24 52 views
2

我使用PoolingHttpClientConnectionManager和我的代碼工作正常,但我想我的代碼是否使用PoolingHttpClientConnectionManager資源瞭解 我有CloseableHttpClient集成像下面如何在java中檢查連接池是否正常工作?

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

所以我一直在使用電線鯊魚嘗試和過濾使用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(); 
    } 

} 
} 
+0

你能提供[mcve](http://stackoverflow.com/help/mcve)嗎?您已經差不多完成了,但尚未完成。 –

+0

我已經提供了我的代碼更新 – Labeo

+0

@ Ortomala Lokni是否有任何解決方案 – Labeo

回答

0

從公開執行交易關閉,然後看netstat,看是否與數據庫的TCP連接後,你仍然ESTABLISHED關閉了Connection.

+0

但我的代碼中沒有jdbc的連接池 – Labeo

+0

因此執行* HTTP *事務。同樣的區別。 – EJP

+0

我正在檢查線鯊魚,我上面說過結果是錯的? 我會嘗試在netstat netstat – Labeo

相關問題