Search in sources :

Example 31 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenHttpClientChecksStaleConns_thenNoExceptions.

// 8
@Test
public final // 8.1
void whenHttpClientChecksStaleConns_thenNoExceptions() {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setDefaultRequestConfig(RequestConfig.custom().setStaleConnectionCheckEnabled(true).build()).setConnectionManager(poolingConnManager).build();
}
Also used : PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.Test)

Example 32 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenClosingConnectionsandManager_thenCloseWithNoExceptions1.

// 9
@Test(expected = IllegalStateException.class)
public final // 9.1
void whenClosingConnectionsandManager_thenCloseWithNoExceptions1() throws InterruptedException, ExecutionException, IOException, HttpException {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
    final HttpGet get = new HttpGet("http://google.com");
    response = client.execute(get);
    EntityUtils.consume(response.getEntity());
    response.close();
    client.close();
    poolingConnManager.close();
    poolingConnManager.shutdown();
    client.execute(get);
    assertTrue(response.getEntity() == null);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.Test)

Example 33 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenCustomizedIdleConnMonitor_thenEliminateIdleConns.

@Test
@Ignore("Very Long Running")
public final /*tester*/
void whenCustomizedIdleConnMonitor_thenEliminateIdleConns() throws InterruptedException, IOException {
    poolingConnManager = new PoolingHttpClientConnectionManager();
    client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
    final IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(poolingConnManager);
    final HttpGet get = new HttpGet("http://google.com");
    // test this with new HttpGet("http://iotechperu.com")----First test will fail b/c there is redirect connection at that site
    final TesterVersion_MultiHttpClientConnThread thread1 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
    final TesterVersion_MultiHttpClientConnThread thread2 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
    final TesterVersion_MultiHttpClientConnThread thread3 = new TesterVersion_MultiHttpClientConnThread(client, get, poolingConnManager);
    staleMonitor.start();
    thread1.start();
    thread1.join();
    thread2.start();
    thread2.join();
    thread3.start();
    assertTrue(poolingConnManager.getTotalStats().getAvailable() == 1);
    thread3.join(32000);
    assertTrue(poolingConnManager.getTotalStats().getAvailable() == 0);
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Ignore(org.junit.Ignore) Test(org.junit.Test)

Example 34 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenConfiguringTimeOut_thenNoExceptions.

// 7
@Test
public final // 7.1
void whenConfiguringTimeOut_thenNoExceptions() {
    route = new HttpRoute(new HttpHost("localhost", 80));
    poolingConnManager = new PoolingHttpClientConnectionManager();
    poolingConnManager.setSocketConfig(route.getTargetHost(), SocketConfig.custom().setSoTimeout(5000).build());
    assertTrue(poolingConnManager.getSocketConfig(route.getTargetHost()).getSoTimeout() == 5000);
}
Also used : HttpRoute(org.apache.http.conn.routing.HttpRoute) HttpHost(org.apache.http.HttpHost) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.Test)

Example 35 with PoolingHttpClientConnectionManager

use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project tutorials by eugenp.

the class HttpClientConnectionManagementLiveTest method whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse.

@Test
public final // @Ignore
void whenConnectionsNeededGreaterThanMaxTotal_thenLeaseMasTotalandReuse() throws InterruptedException {
    final HttpGet get = new HttpGet("http://echo.200please.com");
    poolingConnManager = new PoolingHttpClientConnectionManager();
    poolingConnManager.setDefaultMaxPerRoute(5);
    poolingConnManager.setMaxTotal(5);
    client = HttpClients.custom().setConnectionManager(poolingConnManager).build();
    final MultiHttpClientConnThread[] threads = new MultiHttpClientConnThread[10];
    for (int i = 0; i < threads.length; i++) {
        threads[i] = new MultiHttpClientConnThread(client, get, poolingConnManager);
    }
    for (final MultiHttpClientConnThread thread : threads) {
        thread.start();
    }
    for (final MultiHttpClientConnThread thread : threads) {
        thread.join(10000);
    }
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) PoolingHttpClientConnectionManager(org.apache.http.impl.conn.PoolingHttpClientConnectionManager) Test(org.junit.Test)

Aggregations

PoolingHttpClientConnectionManager (org.apache.http.impl.conn.PoolingHttpClientConnectionManager)180 SSLConnectionSocketFactory (org.apache.http.conn.ssl.SSLConnectionSocketFactory)63 ConnectionSocketFactory (org.apache.http.conn.socket.ConnectionSocketFactory)62 PlainConnectionSocketFactory (org.apache.http.conn.socket.PlainConnectionSocketFactory)54 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)52 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)42 SSLContext (javax.net.ssl.SSLContext)36 RequestConfig (org.apache.http.client.config.RequestConfig)31 IOException (java.io.IOException)29 Test (org.junit.Test)27 HttpHost (org.apache.http.HttpHost)21 HttpGet (org.apache.http.client.methods.HttpGet)18 BasicCredentialsProvider (org.apache.http.impl.client.BasicCredentialsProvider)17 AuthScope (org.apache.http.auth.AuthScope)16 SSLContextBuilder (org.apache.http.ssl.SSLContextBuilder)16 HttpResponse (org.apache.http.HttpResponse)15 UsernamePasswordCredentials (org.apache.http.auth.UsernamePasswordCredentials)14 CredentialsProvider (org.apache.http.client.CredentialsProvider)14 HostnameVerifier (javax.net.ssl.HostnameVerifier)13 HttpClient (org.apache.http.client.HttpClient)12