connection management work

This commit is contained in:
eugenp 2014-07-06 19:13:05 +03:00
parent fc6e8af9ea
commit 40dc518df0
3 changed files with 89 additions and 73 deletions

View File

@ -33,6 +33,7 @@ import org.apache.http.protocol.HttpRequestExecutor;
import org.apache.http.util.EntityUtils; import org.apache.http.util.EntityUtils;
import org.junit.After; import org.junit.After;
import org.junit.Before; import org.junit.Before;
import org.junit.Ignore;
import org.junit.Test; import org.junit.Test;
public class HttpClientConnectionManagementTest { public class HttpClientConnectionManagementTest {
@ -58,31 +59,37 @@ public class HttpClientConnectionManagementTest {
public final void before() { public final void before() {
get1 = new HttpGet(SERVER1); get1 = new HttpGet(SERVER1);
get2 = new HttpGet(SERVER7); get2 = new HttpGet(SERVER7);
route = new HttpRoute(new HttpHost("localhost", 80)); route = new HttpRoute(new HttpHost("www.baeldung.com", 80));
} }
@After @After
public final void after() throws IllegalStateException, IOException { public final void after() throws IllegalStateException, IOException {
if (conn != null) if (conn != null) {
conn.close(); conn.close();
if (conn1 != null) }
if (conn1 != null) {
conn1.close(); conn1.close();
if (conn2 != null) }
if (conn2 != null) {
conn2.close(); conn2.close();
if (poolingConnManager != null) }
if (poolingConnManager != null) {
poolingConnManager.shutdown(); poolingConnManager.shutdown();
if (basicConnManager != null) }
if (basicConnManager != null) {
basicConnManager.shutdown(); basicConnManager.shutdown();
if (client != null) }
if (client != null) {
client.close(); client.close();
if (response != null) }
if (response != null) {
response.close(); response.close();
} }
}
// 2 // 2
@Test @Test
// @Ignore
// 2.1 IN ARTCLE // 2.1 IN ARTCLE
public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException { public final void whenLowLevelConnectionIsEstablished_thenNoExceptions() throws IOException, HttpException, InterruptedException, ExecutionException {
basicConnManager = new BasicHttpClientConnectionManager(); basicConnManager = new BasicHttpClientConnectionManager();
@ -98,8 +105,9 @@ public class HttpClientConnectionManagementTest {
context = HttpClientContext.create(); context = HttpClientContext.create();
final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null); final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
conn = connRequest.get(1000, TimeUnit.SECONDS); conn = connRequest.get(1000, TimeUnit.SECONDS);
if (!conn.isOpen()) if (!conn.isOpen()) {
basicConnManager.connect(conn, route, 1000, context); basicConnManager.connect(conn, route, 1000, context);
}
conn.setSocketTimeout(30000); conn.setSocketTimeout(30000);
assertTrue(conn.getSocketTimeout() == 30000); assertTrue(conn.getSocketTimeout() == 30000);
@ -109,6 +117,7 @@ public class HttpClientConnectionManagementTest {
// 3 // 3
@Test @Test
// Example 3.1.
public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws InterruptedException, ClientProtocolException, IOException { public final void whenPollingConnectionManagerIsConfiguredOnHttpClient_thenNoExceptions() throws InterruptedException, ClientProtocolException, IOException {
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
client = HttpClients.custom().setConnectionManager(poolingConnManager).build(); client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@ -127,8 +136,8 @@ public class HttpClientConnectionManagementTest {
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager); final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client1, get1, poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager); final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client2, get2, poolingConnManager);
thread1.start(); thread1.start();
thread1.join();
thread2.start(); thread2.start();
thread1.join();
thread2.join(1000); thread2.join(1000);
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2); assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
} }
@ -148,42 +157,40 @@ public class HttpClientConnectionManagementTest {
thread2.join(); thread2.join();
} }
// 4
@Test @Test
// @Ignore // Example 4.1
// 3.4
public final void whenIncreasingConnectionPool_thenNoEceptions() { public final void whenIncreasingConnectionPool_thenNoEceptions() {
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
poolingConnManager.setMaxTotal(5); poolingConnManager.setMaxTotal(5);
poolingConnManager.setDefaultMaxPerRoute(4); poolingConnManager.setDefaultMaxPerRoute(4);
final HttpHost localhost = new HttpHost("locahost", 80); final HttpHost localhost = new HttpHost("locahost", 80);
poolingConnManager.setMaxPerRoute(new HttpRoute(localhost), 5); poolingConnManager.setMaxPerRoute(new HttpRoute(localhost), 5);
} }
@Test @Test
// @Ignore // @Ignore
// 3.5 Tester Version // 4.2 Tester Version
/*tester*/public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException, IOException { /*tester*/public final void whenExecutingSameRequestsInDifferentThreads_thenUseDefaultConnLimit() throws InterruptedException, IOException {
final HttpGet get = new HttpGet("http://google.com");
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
client = HttpClients.custom().setConnectionManager(poolingConnManager).build(); client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager); final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, new HttpGet("http://www.google.com"), poolingConnManager);
thread1.start(); thread1.start();
thread1.join(1000);
assertTrue(poolingConnManager.getTotalStats().getLeased() == 1);
thread2.start(); thread2.start();
thread2.join(1000);
assertTrue(poolingConnManager.getTotalStats().getLeased() == 2);
thread3.start(); thread3.start();
thread3.join(1000); thread1.join(10000);
thread2.join(10000);
thread3.join(10000);
} }
@Test @Test
// @Ignore // 4.2 Article version
// 3.5 Article version
public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException { public final void whenExecutingSameRequestsInDifferentThreads_thenExecuteReuqest() throws InterruptedException {
final HttpGet get = new HttpGet("http://localhost"); final HttpGet get = new HttpGet("http://www.google.com");
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
client = HttpClients.custom().setConnectionManager(poolingConnManager).build(); client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
final MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get); final MultiHttpClientConnThread thread1 = new MultiHttpClientConnThread(client, get);
@ -197,11 +204,11 @@ public class HttpClientConnectionManagementTest {
thread3.join(); thread3.join();
} }
// 4 // 5
@Test @Test
// @Ignore // @Ignore
// 4.1 // 5.1
public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException { public final void whenCustomizingKeepAliveStrategy_thenNoExceptions() throws ClientProtocolException, IOException {
final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() { final ConnectionKeepAliveStrategy myStrategy = new ConnectionKeepAliveStrategy() {
@Override @Override
@ -229,37 +236,36 @@ public class HttpClientConnectionManagementTest {
client.execute(get2); client.execute(get2);
} }
// 5 // 6
@Test @Test
// @Ignore // @Ignore
// 5.1 // 6.1
public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException { public final void givenBasicHttpClientConnManager_whenConnectionReuse_thenNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
basicConnManager = new BasicHttpClientConnectionManager(); basicConnManager = new BasicHttpClientConnectionManager();
context = HttpClientContext.create(); context = HttpClientContext.create();
final HttpGet get = new HttpGet("http://localhost");
HttpResponse thisResponse = null;
final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null); final ConnectionRequest connRequest = basicConnManager.requestConnection(route, null);
client = HttpClients.custom().setConnectionManager(basicConnManager).build();
boolean respAvail = false;
conn = connRequest.get(10, TimeUnit.SECONDS); conn = connRequest.get(10, TimeUnit.SECONDS);
if (!conn.isOpen()) {
basicConnManager.connect(conn, route, 1000, context); basicConnManager.connect(conn, route, 1000, context);
basicConnManager.routeComplete(conn, route, context); basicConnManager.routeComplete(conn, route, context);
final HttpRequestExecutor exeRequest = new HttpRequestExecutor(); final HttpRequestExecutor exeRequest = new HttpRequestExecutor();
context.setTargetHost((new HttpHost("localhost", 80))); context.setTargetHost((new HttpHost("www.baeldung.com", 80)));
thisResponse = exeRequest.execute(get, conn, context);
respAvail = conn.isResponseAvailable(1000); final HttpGet get = new HttpGet("http://www.baeldung.com");
} exeRequest.execute(get, conn, context);
conn.isResponseAvailable(1000);
basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS); basicConnManager.releaseConnection(conn, null, 1, TimeUnit.SECONDS);
if (respAvail) {
//
client = HttpClients.custom().setConnectionManager(basicConnManager).build();
client.execute(get); client.execute(get);
} }
}
@Test @Test
// @Ignore // @Ignore
// 5.2 TESTER VERSION // 6.2 TESTER VERSION
/*tester*/public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException { /*tester*/public final void whenConnectionsNeededGreaterThanMaxTotal_thenReuseConnections() throws InterruptedException {
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
poolingConnManager.setDefaultMaxPerRoute(5); poolingConnManager.setDefaultMaxPerRoute(5);
@ -282,7 +288,7 @@ public class HttpClientConnectionManagementTest {
} }
@Test @Test
// 5.2 ARTICLE VERSION // 7.2 ARTICLE VERSION
// @Ignore // @Ignore
public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException { public final void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
final HttpGet get = new HttpGet("http://echo.200please.com"); final HttpGet get = new HttpGet("http://echo.200please.com");
@ -304,7 +310,7 @@ public class HttpClientConnectionManagementTest {
@Test @Test
// @Ignore // @Ignore
// 6.2.1 // 7.2.1
public final void whenConfiguringTimeOut_thenNoExceptions() { public final void whenConfiguringTimeOut_thenNoExceptions() {
route = new HttpRoute(new HttpHost("localhost", 80)); route = new HttpRoute(new HttpHost("localhost", 80));
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
@ -312,17 +318,19 @@ public class HttpClientConnectionManagementTest {
assertTrue(poolingConnManager.getSocketConfig(route.getTargetHost()).getSoTimeout() == 5000); assertTrue(poolingConnManager.getSocketConfig(route.getTargetHost()).getSoTimeout() == 5000);
} }
// 8
@Test @Test
// @Ignore // @Ignore
// 7.1 // 8.1
public final void whenHttpClientChecksStaleConns_thenNoExceptions() { public final void whenHttpClientChecksStaleConns_thenNoExceptions() {
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build(); client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build();
} }
@Test @Test
// @Ignore @Ignore("Very Long Running")
// 7.2 TESTER VERSION // 8.2 TESTER VERSION
/*tester*/public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException, IOException { /*tester*/public final void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException, IOException {
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
client = HttpClients.custom().setConnectionManager(poolingConnManager).build(); client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
@ -345,7 +353,7 @@ public class HttpClientConnectionManagementTest {
@Test @Test
// @Ignore // @Ignore
// 7.2 ARTICLE VERSION // 8.2 ARTICLE VERSION
public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException, IOException { public final void whenCustomizedIdleConnMonitor_thenNoExceptions() throws InterruptedException, IOException {
final HttpGet get = new HttpGet("http://google.com"); final HttpGet get = new HttpGet("http://google.com");
poolingConnManager = new PoolingHttpClientConnectionManager(); poolingConnManager = new PoolingHttpClientConnectionManager();
@ -355,9 +363,11 @@ public class HttpClientConnectionManagementTest {
staleMonitor.join(1000); staleMonitor.join(1000);
} }
// 9
@Test(expected = IllegalStateException.class) @Test(expected = IllegalStateException.class)
// @Ignore // @Ignore
// 8.1 // 9.1
public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException { public final void whenClosingConnectionsandManager_thenCloseWithNoExceptions() throws InterruptedException, ExecutionException, IOException, HttpException {
route = new HttpRoute(new HttpHost("google.com", 80)); route = new HttpRoute(new HttpHost("google.com", 80));
final HttpGet get = new HttpGet("http://google.com"); final HttpGet get = new HttpGet("http://google.com");

View File

@ -18,14 +18,13 @@ public class MultiHttpClientConnThread extends Thread {
private final HttpGet get; private final HttpGet get;
private PoolingHttpClientConnectionManager connManager; private PoolingHttpClientConnectionManager connManager;
private static HttpResponse response;
public int leasedConn; public int leasedConn;
public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client; this.client = client;
this.get = get; this.get = get;
this.connManager = connManager; this.connManager = connManager;
this.leasedConn = 0; leasedConn = 0;
} }
public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) { public MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get) {
@ -44,14 +43,23 @@ public class MultiHttpClientConnThread extends Thread {
@Override @Override
public final void run() { public final void run() {
try { try {
logger.info("Thread Running: " + getName()); logger.debug("Thread Running: " + getName());
logger.debug("Thread Running: " + getName());
response = client.execute(get);
if (connManager != null) { if (connManager != null) {
logger.info("Leased Connections " + connManager.getTotalStats().getLeased()); logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
leasedConn = connManager.getTotalStats().getLeased(); logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
logger.info("Available Connections " + connManager.getTotalStats().getAvailable());
} }
final HttpResponse response = client.execute(get);
if (connManager != null) {
leasedConn = connManager.getTotalStats().getLeased();
logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
}
EntityUtils.consume(response.getEntity()); EntityUtils.consume(response.getEntity());
} catch (final ClientProtocolException ex) { } catch (final ClientProtocolException ex) {
logger.error("", ex); logger.error("", ex);

View File

@ -9,37 +9,35 @@ import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import com.google.common.base.Preconditions;
public class TesterVersion_MultiHttpClientConnThread extends Thread { public class TesterVersion_MultiHttpClientConnThread extends Thread {
private final Logger logger = LoggerFactory.getLogger(getClass()); private final Logger logger = LoggerFactory.getLogger(getClass());
private final CloseableHttpClient client; private final CloseableHttpClient client;
private final HttpGet get; private final HttpGet get;
private PoolingHttpClientConnectionManager connManager; private PoolingHttpClientConnectionManager connManager;
public int leasedConn;
public TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) { public TesterVersion_MultiHttpClientConnThread(final CloseableHttpClient client, final HttpGet get, final PoolingHttpClientConnectionManager connManager) {
this.client = client; this.client = client;
this.get = get; this.get = get;
this.connManager = connManager; this.connManager = Preconditions.checkNotNull(connManager);
leasedConn = 0;
} }
// //
public final int getLeasedConn() {
return leasedConn;
}
@Override @Override
public final void run() { public final void run() {
try { try {
logger.info("Thread Running: " + getName()); logger.debug("Thread Running: " + getName());
logger.info("Before - Leased Connections = " + connManager.getTotalStats().getLeased());
logger.info("Before - Available Connections = " + connManager.getTotalStats().getAvailable());
client.execute(get); client.execute(get);
if (connManager != null) {
logger.info("Leased Connections " + connManager.getTotalStats().getLeased()); logger.info("After - Leased Connections = " + connManager.getTotalStats().getLeased());
leasedConn = connManager.getTotalStats().getLeased(); logger.info("After - Available Connections = " + connManager.getTotalStats().getAvailable());
logger.info("Available Connections " + connManager.getTotalStats().getAvailable());
}
} catch (final ClientProtocolException ex) { } catch (final ClientProtocolException ex) {
logger.error("", ex); logger.error("", ex);
} catch (final IOException ex) { } catch (final IOException ex) {