Search in sources :

Example 1 with ConnectionKeepAliveStrategy

use of org.apache.http.conn.ConnectionKeepAliveStrategy in project robolectric by robolectric.

the class FakeHttpTest method makeRequest.

private void makeRequest(String uri) throws HttpException, IOException {
    FakeHttp.addPendingHttpResponse(200, "a happy response body");
    ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
        }
    };
    DefaultRequestDirector requestDirector = new DefaultRequestDirector(null, null, null, connectionKeepAliveStrategy, null, null, null, null, null, null, null, null);
    requestDirector.execute(null, new HttpGet(uri), null);
}
Also used : ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpGet(org.apache.http.client.methods.HttpGet) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultRequestDirector(org.apache.http.impl.client.DefaultRequestDirector)

Example 2 with ConnectionKeepAliveStrategy

use of org.apache.http.conn.ConnectionKeepAliveStrategy in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method setUp_EnsureStaticStateIsReset.

@Before
public void setUp_EnsureStaticStateIsReset() {
    FakeHttpLayer fakeHttpLayer = FakeHttp.getFakeHttpLayer();
    assertFalse(fakeHttpLayer.hasPendingResponses());
    assertFalse(fakeHttpLayer.hasRequestInfos());
    assertFalse(fakeHttpLayer.hasResponseRules());
    connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse httpResponse, HttpContext httpContext) {
            return 0;
        }
    };
    requestDirector = new DefaultRequestDirector(null, null, null, connectionKeepAliveStrategy, null, null, null, null, null, null, null, null);
}
Also used : ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) DefaultRequestDirector(org.apache.http.impl.client.DefaultRequestDirector) Before(org.junit.Before)

Example 3 with ConnectionKeepAliveStrategy

use of org.apache.http.conn.ConnectionKeepAliveStrategy in project robolectric by robolectric.

the class ShadowDefaultRequestDirectorTest method shouldRecordExtendedRequestData.

@Test
public void shouldRecordExtendedRequestData() throws Exception {
    FakeHttp.addPendingHttpResponse(200, "a happy response body");
    HttpGet httpGet = new HttpGet("http://example.com");
    requestDirector.execute(null, httpGet, null);
    assertSame(FakeHttp.getSentHttpRequestInfo(0).getHttpRequest(), httpGet);
    ConnectionKeepAliveStrategy strategy = shadowOf((DefaultRequestDirector) FakeHttp.getSentHttpRequestInfo(0).getRequestDirector()).getConnectionKeepAliveStrategy();
    assertSame(strategy, connectionKeepAliveStrategy);
}
Also used : ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpGet(org.apache.http.client.methods.HttpGet) DefaultRequestDirector(org.apache.http.impl.client.DefaultRequestDirector) Test(org.junit.Test)

Example 4 with ConnectionKeepAliveStrategy

use of org.apache.http.conn.ConnectionKeepAliveStrategy in project lucene-solr by apache.

the class HttpClientUtil method createClient.

public static CloseableHttpClient createClient(final SolrParams params, PoolingHttpClientConnectionManager cm, boolean sharedConnectionManager, HttpRequestExecutor httpRequestExecutor) {
    final ModifiableSolrParams config = new ModifiableSolrParams(params);
    if (logger.isDebugEnabled()) {
        logger.debug("Creating new http client, config:" + config);
    }
    cm.setMaxTotal(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS, 10000));
    cm.setDefaultMaxPerRoute(params.getInt(HttpClientUtil.PROP_MAX_CONNECTIONS_PER_HOST, 10000));
    cm.setValidateAfterInactivity(Integer.getInteger(VALIDATE_AFTER_INACTIVITY, VALIDATE_AFTER_INACTIVITY_DEFAULT));
    HttpClientBuilder newHttpClientBuilder = HttpClientBuilder.create();
    if (sharedConnectionManager) {
        newHttpClientBuilder.setConnectionManagerShared(true);
    } else {
        newHttpClientBuilder.setConnectionManagerShared(false);
    }
    ConnectionKeepAliveStrategy keepAliveStrat = new ConnectionKeepAliveStrategy() {

        @Override
        public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
            // we only close connections based on idle time, not ttl expiration
            return -1;
        }
    };
    if (httpClientBuilder.getAuthSchemeRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultAuthSchemeRegistry(httpClientBuilder.getAuthSchemeRegistryProvider().getAuthSchemeRegistry());
    }
    if (httpClientBuilder.getCookieSpecRegistryProvider() != null) {
        newHttpClientBuilder.setDefaultCookieSpecRegistry(httpClientBuilder.getCookieSpecRegistryProvider().getCookieSpecRegistry());
    }
    if (httpClientBuilder.getCredentialsProviderProvider() != null) {
        newHttpClientBuilder.setDefaultCredentialsProvider(httpClientBuilder.getCredentialsProviderProvider().getCredentialsProvider());
    }
    newHttpClientBuilder.addInterceptorLast(new DynamicInterceptor());
    newHttpClientBuilder = newHttpClientBuilder.setKeepAliveStrategy(keepAliveStrat).evictIdleConnections((long) Integer.getInteger(EVICT_IDLE_CONNECTIONS, EVICT_IDLE_CONNECTIONS_DEFAULT), TimeUnit.MILLISECONDS);
    if (httpRequestExecutor != null) {
        newHttpClientBuilder.setRequestExecutor(httpRequestExecutor);
    }
    HttpClientBuilder builder = setupBuilder(newHttpClientBuilder, params);
    HttpClient httpClient = builder.setConnectionManager(cm).build();
    assert ObjectReleaseTracker.track(httpClient);
    return (CloseableHttpClient) httpClient;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) HttpClient(org.apache.http.client.HttpClient) CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) HttpClientBuilder(org.apache.http.impl.client.HttpClientBuilder) ModifiableSolrParams(org.apache.solr.common.params.ModifiableSolrParams)

Example 5 with ConnectionKeepAliveStrategy

use of org.apache.http.conn.ConnectionKeepAliveStrategy in project Notes by lguipeng.

the class TEvernoteHttpClient method getHTTPClient.

@Deprecated
private DefaultHttpClient getHTTPClient() {
    try {
        if (mConnectionManager != null) {
            mConnectionManager.closeExpiredConnections();
            mConnectionManager.closeIdleConnections(1, TimeUnit.SECONDS);
        } else {
            BasicHttpParams params = new BasicHttpParams();
            HttpConnectionParams.setConnectionTimeout(params, 10000);
            HttpConnectionParams.setSoTimeout(params, 20000);
            ConnManagerParams.setMaxTotalConnections(params, ConnManagerParams.DEFAULT_MAX_TOTAL_CONNECTIONS);
            ConnManagerParams.setTimeout(params, 10000);
            // Giving 18 connections to Evernote
            ConnPerRouteBean connPerRoute = new ConnPerRouteBean(18);
            ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute);
            SchemeRegistry schemeRegistry = new SchemeRegistry();
            schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
            schemeRegistry.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443));
            mConnectionManager = new ThreadSafeClientConnManager(params, schemeRegistry);
            DefaultHttpClient httpClient = new DefaultHttpClient(mConnectionManager, params);
            httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {

                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    // 2 minutes in millis
                    return 2 * 60 * 1000;
                }
            });
            httpClient.setReuseStrategy(new ConnectionReuseStrategy() {

                @Override
                public boolean keepAlive(HttpResponse response, HttpContext context) {
                    return true;
                }
            });
            mHttpClient = httpClient;
        }
    } catch (Exception ex) {
        return null;
    }
    return mHttpClient;
}
Also used : Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) ConnectionKeepAliveStrategy(org.apache.http.conn.ConnectionKeepAliveStrategy) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) HttpContext(org.apache.http.protocol.HttpContext) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) ConnPerRouteBean(org.apache.http.conn.params.ConnPerRouteBean) ConnectionReuseStrategy(org.apache.http.ConnectionReuseStrategy) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) TTransportException(com.evernote.thrift.transport.TTransportException)

Aggregations

ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)5 HttpResponse (org.apache.http.HttpResponse)4 HttpContext (org.apache.http.protocol.HttpContext)4 DefaultRequestDirector (org.apache.http.impl.client.DefaultRequestDirector)3 HttpGet (org.apache.http.client.methods.HttpGet)2 TTransportException (com.evernote.thrift.transport.TTransportException)1 IOException (java.io.IOException)1 ConnectionReuseStrategy (org.apache.http.ConnectionReuseStrategy)1 HttpClient (org.apache.http.client.HttpClient)1 ConnPerRouteBean (org.apache.http.conn.params.ConnPerRouteBean)1 Scheme (org.apache.http.conn.scheme.Scheme)1 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)1 CloseableHttpClient (org.apache.http.impl.client.CloseableHttpClient)1 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)1 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)1 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)1 BasicHttpParams (org.apache.http.params.BasicHttpParams)1 ModifiableSolrParams (org.apache.solr.common.params.ModifiableSolrParams)1 Before (org.junit.Before)1 Test (org.junit.Test)1