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

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

Introduction

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

Prototype

public void setDefaultSocketConfig(final SocketConfig defaultSocketConfig) 

Source Link

Usage

From source file:org.kuali.rice.ksb.messaging.serviceconnectors.DefaultHttpClientConfigurer.java

/**
 * Builds the HttpClientConnectionManager.
 *
 * <p>Note that this calls {@link #buildSslConnectionSocketFactory()} and registers the resulting {@link SSLConnectionSocketFactory}
 * (if non-null) with its socket factory registry.</p>
 *
 * @return the HttpClientConnectionManager
 *//*from   w w  w. jav  a2  s .  co  m*/
protected HttpClientConnectionManager buildConnectionManager() {
    PoolingHttpClientConnectionManager poolingConnectionManager = null;

    SSLConnectionSocketFactory sslConnectionSocketFactory = buildSslConnectionSocketFactory();
    if (sslConnectionSocketFactory != null) {
        Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder
                .<ConnectionSocketFactory>create().register("https", sslConnectionSocketFactory)
                .register("http", new PlainConnectionSocketFactory()).build();
        poolingConnectionManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
    } else {
        poolingConnectionManager = new PoolingHttpClientConnectionManager();
    }

    // Configure the connection manager
    poolingConnectionManager
            .setMaxTotal(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS));

    // By default we'll set the max connections per route (essentially that means per host for us) to the max total
    poolingConnectionManager
            .setDefaultMaxPerRoute(MAX_TOTAL_CONNECTIONS.getValueOrDefault(DEFAULT_MAX_TOTAL_CONNECTIONS));

    SocketConfig.Builder socketConfigBuilder = SocketConfig.custom();
    socketConfigBuilder.setSoTimeout(SO_TIMEOUT.getValueOrDefault(DEFAULT_SOCKET_TIMEOUT));

    Integer soLinger = SO_LINGER.getValue();
    if (soLinger != null) {
        socketConfigBuilder.setSoLinger(soLinger);
    }

    Boolean isTcpNoDelay = TCP_NODELAY.getValue();
    if (isTcpNoDelay != null) {
        socketConfigBuilder.setTcpNoDelay(isTcpNoDelay);
    }

    poolingConnectionManager.setDefaultSocketConfig(socketConfigBuilder.build());

    ConnectionConfig.Builder connectionConfigBuilder = ConnectionConfig.custom();

    Integer sendBuffer = SO_SNDBUF.getValue();
    Integer receiveBuffer = SO_RCVBUF.getValue();

    // if either send or recieve buffer size is set, we'll set the buffer size to whichever is greater
    if (sendBuffer != null || receiveBuffer != null) {
        Integer bufferSize = -1;
        if (sendBuffer != null) {
            bufferSize = sendBuffer;
        }

        if (receiveBuffer != null && receiveBuffer > bufferSize) {
            bufferSize = receiveBuffer;
        }

        connectionConfigBuilder.setBufferSize(bufferSize);
    }

    String contentCharset = HTTP_CONTENT_CHARSET.getValue();
    if (contentCharset != null) {
        connectionConfigBuilder.setCharset(Charset.forName(contentCharset));
    }

    poolingConnectionManager.setDefaultConnectionConfig(connectionConfigBuilder.build());

    return poolingConnectionManager;
}