Example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager requestConnection

List of usage examples for org.apache.http.impl.conn PoolingHttpClientConnectionManager requestConnection

Introduction

In this page you can find the example usage for org.apache.http.impl.conn PoolingHttpClientConnectionManager requestConnection.

Prototype

public ConnectionRequest requestConnection(final HttpRoute route, final Object state) 

Source Link

Usage

From source file:org.apache.solr.client.solrj.impl.ConnectionReuseTest.java

public ConnectionRequest getClientConnectionRequest(HttpClient httpClient, HttpRoute route,
        PoolingHttpClientConnectionManager cm) {
    ConnectionRequest mConn = cm.requestConnection(route, HttpSolrClient.cacheKey);
    return mConn;
}

From source file:org.apache.solr.client.solrj.ConnectionReuseTest.java

public ConnectionRequest getClientConnectionRequest(HttpClient httpClient, HttpRoute route,
        PoolingHttpClientConnectionManager cm) {
    ConnectionRequest mConn = cm.requestConnection(route, null);
    return mConn;
}

From source file:org.apache.solr.client.solrj.impl.ConnectionReuseTest.java

@Test
public void testConnectionReuse() throws Exception {

    URL url = cluster.getJettySolrRunners().get(0).getBaseUrl();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    CloseableHttpClient httpClient = HttpClientUtil.createClient(null, cm);
    try (SolrClient client = buildClient(httpClient, url)) {

        HttpHost target = new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http");
        HttpRoute route = new HttpRoute(target);

        ConnectionRequest mConn = getClientConnectionRequest(httpClient, route, cm);

        HttpClientConnection conn1 = getConn(mConn);
        headerRequest(target, route, conn1, cm);

        cm.releaseConnection(conn1, null, -1, TimeUnit.MILLISECONDS);

        int queueBreaks = 0;
        int cnt1 = atLeast(3);
        int cnt2 = atLeast(30);
        for (int j = 0; j < cnt1; j++) {
            boolean done = false;
            for (int i = 0; i < cnt2; i++) {
                AddUpdateCommand c = new AddUpdateCommand(null);
                c.solrDoc = sdoc("id", id.incrementAndGet());
                try {
                    client.add(c.solrDoc);
                } catch (Exception e) {
                    e.printStackTrace();
                }/*from w  ww .j a  va2 s .  com*/
                if (!done && i > 0 && i < cnt2 - 1 && client instanceof ConcurrentUpdateSolrClient
                        && random().nextInt(10) > 8) {
                    queueBreaks++;
                    done = true;
                    Thread.sleep(350); // wait past streaming client poll time of 250ms
                }
            }
            if (client instanceof ConcurrentUpdateSolrClient) {
                ((ConcurrentUpdateSolrClient) client).blockUntilFinished();
            }
        }

        route = new HttpRoute(new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http"));

        mConn = cm.requestConnection(route, HttpSolrClient.cacheKey);

        HttpClientConnection conn2 = getConn(mConn);

        HttpConnectionMetrics metrics = conn2.getMetrics();
        headerRequest(target, route, conn2, cm);

        cm.releaseConnection(conn2, null, -1, TimeUnit.MILLISECONDS);

        assertNotNull(
                "No connection metrics found - is the connection getting aborted? server closing the connection? "
                        + client.getClass().getSimpleName(),
                metrics);

        // we try and make sure the connection we get has handled all of the requests in this test
        if (client instanceof ConcurrentUpdateSolrClient) {
            // we can't fully control queue polling breaking up requests - allow a bit of leeway
            int exp = cnt1 + queueBreaks + 2;
            assertTrue(
                    "We expected all communication via streaming client to use one connection! expected=" + exp
                            + " got=" + metrics.getRequestCount(),
                    Math.max(exp, metrics.getRequestCount()) - Math.min(exp, metrics.getRequestCount()) < 3);
        } else {
            assertTrue("We expected all communication to use one connection! "
                    + client.getClass().getSimpleName() + " " + metrics.getRequestCount(),
                    cnt1 * cnt2 + 2 <= metrics.getRequestCount());
        }

    } finally {
        HttpClientUtil.close(httpClient);
    }
}

From source file:org.apache.solr.client.solrj.ConnectionReuseTest.java

@Test
public void testConnectionReuse() throws Exception {

    URL url = cluster.getJettySolrRunners().get(0).getBaseUrl();
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    CloseableHttpClient httpClient = HttpClientUtil.createClient(null, cm);
    try (SolrClient client = buildClient(httpClient, url)) {

        HttpHost target = new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http");
        HttpRoute route = new HttpRoute(target);

        ConnectionRequest mConn = getClientConnectionRequest(httpClient, route, cm);

        HttpClientConnection conn1 = getConn(mConn);
        headerRequest(target, route, conn1, cm);

        cm.releaseConnection(conn1, null, -1, TimeUnit.MILLISECONDS);

        int queueBreaks = 0;
        int cnt1 = atLeast(3);
        int cnt2 = atLeast(30);
        for (int j = 0; j < cnt1; j++) {
            boolean done = false;
            for (int i = 0; i < cnt2; i++) {
                AddUpdateCommand c = new AddUpdateCommand(null);
                c.solrDoc = sdoc("id", id.incrementAndGet());
                try {
                    client.add(c.solrDoc);
                } catch (Exception e) {
                    e.printStackTrace();
                }/*from   w ww  .j  av  a  2  s. com*/
                if (!done && i > 0 && i < cnt2 - 1 && client instanceof ConcurrentUpdateSolrClient
                        && random().nextInt(10) > 8) {
                    queueBreaks++;
                    done = true;
                    Thread.sleep(350); // wait past streaming client poll time of 250ms
                }
            }
            if (client instanceof ConcurrentUpdateSolrClient) {
                ((ConcurrentUpdateSolrClient) client).blockUntilFinished();
            }
        }

        route = new HttpRoute(new HttpHost(url.getHost(), url.getPort(), isSSLMode() ? "https" : "http"));

        mConn = cm.requestConnection(route, null);

        HttpClientConnection conn2 = getConn(mConn);

        HttpConnectionMetrics metrics = conn2.getMetrics();
        headerRequest(target, route, conn2, cm);

        cm.releaseConnection(conn2, null, -1, TimeUnit.MILLISECONDS);

        assertNotNull(
                "No connection metrics found - is the connection getting aborted? server closing the connection? "
                        + client.getClass().getSimpleName(),
                metrics);

        // we try and make sure the connection we get has handled all of the requests in this test
        if (client instanceof ConcurrentUpdateSolrClient) {
            // we can't fully control queue polling breaking up requests - allow a bit of leeway
            int exp = cnt1 + queueBreaks + 2;
            assertTrue(
                    "We expected all communication via streaming client to use one connection! expected=" + exp
                            + " got=" + metrics.getRequestCount(),
                    Math.max(exp, metrics.getRequestCount()) - Math.min(exp, metrics.getRequestCount()) < 3);
        } else {
            assertTrue("We expected all communication to use one connection! "
                    + client.getClass().getSimpleName() + " " + metrics.getRequestCount(),
                    cnt1 * cnt2 + 2 <= metrics.getRequestCount());
        }

    } finally {
        HttpClientUtil.close(httpClient);
    }
}