Example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionReuseStrategy

List of usage examples for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionReuseStrategy

Introduction

In this page you can find the example usage for org.apache.http.impl.nio.client HttpAsyncClientBuilder setConnectionReuseStrategy.

Prototype

public final HttpAsyncClientBuilder setConnectionReuseStrategy(final ConnectionReuseStrategy reuseStrategy) 

Source Link

Document

Assigns ConnectionReuseStrategy instance.

Usage

From source file:HCNIOEngine.java

private CloseableHttpAsyncClient createCloseableHttpAsyncClient() throws Exception {
    HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create();
    builder.useSystemProperties();/* w ww.  jav a2 s .  c  o m*/
    builder.setSSLContext(SSLContext.getDefault());
    builder.setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE);
    builder.setMaxConnPerRoute(2);
    builder.setMaxConnTotal(2);
    builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(1000)
            .setConnectTimeout(2000).setSocketTimeout(2000).build());
    //        builder.setHttpProcessor()
    CloseableHttpAsyncClient hc = builder.build();
    hc.start();
    return hc;
}

From source file:co.paralleluniverse.fibers.dropwizard.FiberHttpClientBuilder.java

/**
 * Add strategies to client such as ConnectionReuseStrategy and KeepAliveStrategy Note that this
 * method mutates the client object by setting the strategies
 *
 * @param client The InstrumentedHttpClient that should be configured with strategies
 *///from w  w w .ja va  2 s  .c  om
protected void setStrategiesForClient(HttpAsyncClientBuilder client) {
    final long keepAlive = configuration.getKeepAlive().toMilliseconds();

    // don't keep alive the HTTP connection and thus don't reuse the TCP socket
    if (keepAlive == 0) {
        client.setConnectionReuseStrategy(new NoConnectionReuseStrategy());
    } else {
        client.setConnectionReuseStrategy(new DefaultConnectionReuseStrategy());
        // either keep alive based on response header Keep-Alive,
        // or if the server can keep a persistent connection (-1), then override based on client's configuration
        client.setKeepAliveStrategy(new DefaultConnectionKeepAliveStrategy() {
            @Override
            public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                final long duration = super.getKeepAliveDuration(response, context);
                return (duration == -1) ? keepAlive : duration;
            }
        });
    }
}