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

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

Introduction

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

Prototype

public final HttpAsyncClientBuilder setKeepAliveStrategy(final ConnectionKeepAliveStrategy keepAliveStrategy) 

Source Link

Document

Assigns ConnectionKeepAliveStrategy instance.

Usage

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  ww.  ja  v a2s  .c  o  m
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;
            }
        });
    }
}