@Test public void testConnectionCount() { testConnectionCount = 0; httpClientUtils = new HttpClientUtils(); // httpClientUtils.setMaxConnPerRoute(0); // default:-1, means 2 // httpClientUtils.setMaxConnTotal(0); // default:-1, means 20 httpClientUtils.setMaxConnPerRoute(3); httpClientUtils.setMaxConnTotal(100); // httpClientUtils.setMaxConnPerRoute(100); // httpClientUtils.setMaxConnTotal(3); httpClientUtils.initHttpClient(); Logger.info(this, "-------------- HttpClient initialized -------------"); final ExceptionHandler exceptionHandler = new ExceptionHandler() { public boolean handle(Throwable e) { return e == null; } }; new Thread("t-1") { @Override public void run() { runTest("http://localhost:65534/test/testConnectionCount", 7, exceptionHandler, 2); } }.start(); new Thread("t-2") { @Override public void run() { runTest("http://localhost:65533/test/testConnectionCount", 7, exceptionHandler, 1); } }.start(); new Thread("t-3") { @Override public void run() { runTest("http://www.baidu.com", 1, exceptionHandler, 10); } }.start(); runTest("https://www.lufax.com", 1, exceptionHandler, 3); /* 测试结论: 1、 ”Connection“ == 【URL】;“Route” == 【IP:port】。 2、连接数越多,并发性能越高;反之,将频繁进行Connection released/leased。 3、当连接数满足不了并发的要求时,并发请求Connection的线程将wait。 【route allocated: 1 of 3; total allocated: 9 of 100】 [DEBUG]-[2014-03-07 09:56:46.828]-[http://localhost:65533/test/testConnectionCount_runTest_1] - Connection released: [id: 4][route: {}->http://localhost:65533][total kept alive: 6; route allocated: 1 of 3; total allocated: 9 of 100] [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] org.apache.http.impl.conn.PoolingHttpClientConnectionManager.releaseConnection(PoolingHttpClientConnectionManager.java:282) */ }
@Test public void testSocketTimeout() { testConnectionTimeoutCount = 0; httpClientUtils = new HttpClientUtils(); httpClientUtils.setMaxConnPerRoute(1); httpClientUtils.setMaxConnTotal(1); httpClientUtils.setSocketTimeout(2500); httpClientUtils.initHttpClient(); Logger.info(this, "-------------- HttpClient initialized -------------"); ExceptionHandler exceptionHandler = new ExceptionHandler() { public boolean handle(Throwable e) { return (SocketTimeoutException.class == e.getClass()) && "Read timed out".equals(e.getMessage()); } }; runTest("http://localhost:65534/test/testConnectionTimeout", 1, exceptionHandler, 1); }
@Test public void testConnectionRequestTimeout() { testConnectionResetCount = 0; httpClientUtils = new HttpClientUtils(); httpClientUtils.setMaxConnPerRoute(1); httpClientUtils.setMaxConnTotal(1); httpClientUtils.setConnectRequestTimeout(100); httpClientUtils.setConnectTimeout(10); httpClientUtils.initHttpClient(); ExceptionHandler exceptionHandler = new ExceptionHandler() { public boolean handle(Throwable e) { if (e.getMessage().contains("Timeout waiting for connection from pool")) { Assert.assertTrue(false); // throw new RuntimeException("No available connect to user", e); } return e == null; } }; runTest("http://localhost:65534/test/testConnectionRequestTimeout", 1, exceptionHandler, 2); }
@Test public void testConnectionTimeout() { testConnectionTimeoutCount = 0; httpClientUtils = new HttpClientUtils(); httpClientUtils.setMaxConnPerRoute(1); httpClientUtils.setMaxConnTotal(1); /* PING www.a.shifen.com (115.239.210.26) 56(84) bytes of data. 64 bytes from 115.239.210.26: icmp_seq=1 ttl=50 time=6.97 ms PING www-wide.l.google.com (74.125.128.199) 56(84) bytes of data. 64 bytes from hg-in-f199.1e100.net (74.125.128.199): icmp_seq=1 ttl=42 time=33.2 ms */ httpClientUtils.setConnectTimeout(10); httpClientUtils.initHttpClient(); Logger.info(this, "-------------- HttpClient initialized -------------"); ExceptionHandler exceptionHandler = new ExceptionHandler() { public boolean handle(Throwable e) { return e == null; } }; ExceptionHandler exceptionHandler2 = new ExceptionHandler() { public boolean handle(Throwable e) { return (ConnectException.class == e.getClass()) && "Network is unreachable".equals(e.getMessage()); } }; runTest("http://www.baidu.com", 1, exceptionHandler, 1); runTest("https://www.lufax.com/", 1, exceptionHandler2, 1); /* 测试结论: 1、...timed out. Connection will be retried using another IP address 9、runTest("https://www.google.com.hk", 1, exceptionHandler, 1); 测试日志阶段截段: [BUG]-[2014-03-05 19:40:06.211]-[testConnectionCount_12] - Connecting to www.google.com.hk/74.125.128.199:443 [org.apache.http.conn.HttpClientConnectionManager] org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114) [DEBUG]-[2014-03-05 19:40:06.213]-[testConnectionCount_12] - Connect to www.google.com.hk/74.125.128.199:443 timed out. Connection will be retried using another IP address [org.apache.http.conn.HttpClientConnectionManager] org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:143) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connecting to www.google.com.hk/2404:6800:4005:c00:0:0:0:c7:443 [org.apache.http.conn.HttpClientConnectionManager] org.apache.http.impl.conn.HttpClientConnectionOperator.connect(HttpClientConnectionOperator.java:114) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Shutdown connection [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] org.apache.http.impl.conn.LoggingManagedHttpClientConnection.shutdown(LoggingManagedHttpClientConnection.java:87) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection discarded [org.apache.http.impl.execchain.MainClientExec] org.apache.http.impl.execchain.ConnectionHolder.abortConnection(ConnectionHolder.java:126) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - http-outgoing-101: Close connection [org.apache.http.impl.conn.DefaultManagedHttpClientConnection] org.apache.http.impl.conn.LoggingManagedHttpClientConnection.close(LoggingManagedHttpClientConnection.java:79) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_12] - Connection released: [id: 101][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 0 of 1; total allocated: 0 of 1] [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] org.apache.http.impl.conn.PoolingHttpClientConnectionManager.releaseConnection(PoolingHttpClientConnectionManager.java:282) [DEBUG]-[2014-03-05 19:40:06.214]-[testConnectionCount_13] - Connection leased: [id: 102][route: {s}->https://www.google.com.hk:443][total kept alive: 0; route allocated: 1 of 1; total allocated: 1 of 1] [org.apache.http.impl.conn.PoolingHttpClientConnectionManager] org.apache.http.impl.conn.PoolingHttpClientConnectionManager.leaseConnection(PoolingHttpClientConnectionManager.java:246) */ }