Example usage for org.apache.http.impl.nio.conn PoolingNHttpClientConnectionManager PoolingNHttpClientConnectionManager

List of usage examples for org.apache.http.impl.nio.conn PoolingNHttpClientConnectionManager PoolingNHttpClientConnectionManager

Introduction

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

Prototype

public PoolingNHttpClientConnectionManager(final ConnectingIOReactor ioreactor,
            final NHttpConnectionFactory<ManagedNHttpClientConnection> connFactory,
            final Registry<SchemeIOSessionStrategy> iosessionFactoryRegistry,
            final SchemePortResolver schemePortResolver, final DnsResolver dnsResolver, final long timeToLive,
            final TimeUnit tunit) 

Source Link

Usage

From source file:org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory.java

public synchronized void setupNIOClient(HTTPClientPolicy clientPolicy) throws IOReactorException {
    if (client != null) {
        return;//  w w  w  .j a v  a2  s.co  m
    }

    IOReactorConfig config = IOReactorConfig.custom().setIoThreadCount(ioThreadCount)
            .setSelectInterval(selectInterval).setInterestOpQueued(interestOpQueued).setSoLinger(soLinger)
            .setSoTimeout(soTimeout).setSoKeepAlive(soKeepalive).setTcpNoDelay(tcpNoDelay).build();

    Registry<SchemeIOSessionStrategy> ioSessionFactoryRegistry = RegistryBuilder
            .<SchemeIOSessionStrategy>create().register("http", NoopIOSessionStrategy.INSTANCE)
            .register("https", SSLIOSessionStrategy.getSystemDefaultStrategy()).build();

    ManagedNHttpClientConnectionFactory connectionFactory = new ManagedNHttpClientConnectionFactory() {

        @Override
        public ManagedNHttpClientConnection create(final IOSession iosession, final ConnectionConfig config) {
            ManagedNHttpClientConnection conn = super.create(iosession, config);
            return conn;
        }
    };

    DefaultConnectingIOReactor ioreactor = new DefaultConnectingIOReactor(config);
    connectionManager = new PoolingNHttpClientConnectionManager(ioreactor, connectionFactory,
            ioSessionFactoryRegistry, DefaultSchemePortResolver.INSTANCE, SystemDefaultDnsResolver.INSTANCE,
            connectionTTL, TimeUnit.MILLISECONDS);

    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    connectionManager.setMaxTotal(maxConnections);

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setBufferSize(clientPolicy.getChunkLength() > 0 ? clientPolicy.getChunkLength() : 16332).build();

    connectionManager.setDefaultConnectionConfig(connectionConfig);

    RedirectStrategy redirectStrategy = new RedirectStrategy() {

        public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            return false;
        }

        public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context)
                throws ProtocolException {
            return null;
        }
    };

    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom()
            .setConnectionManager(connectionManager).setRedirectStrategy(redirectStrategy)
            .setDefaultCookieStore(new BasicCookieStore() {
                private static final long serialVersionUID = 1L;

                public void addCookie(Cookie cookie) {
                }
            });

    adaptClientBuilder(httpAsyncClientBuilder);

    client = httpAsyncClientBuilder.build();
    // Start the client thread
    client.start();
    if (this.connectionTTL == 0) {
        //if the connection does not have an expiry deadline
        //use the ConnectionMaxIdle to close the idle connection
        new CloseIdleConnectionThread(connectionManager, client).start();
    }
}

From source file:com.liferay.petra.json.web.service.client.BaseJSONWebServiceClientImpl.java

protected PoolingNHttpClientConnectionManager getPoolingNHttpClientConnectionManager()
        throws IOReactorException {

    PoolingNHttpClientConnectionManager poolingNHttpClientConnectionManager = null;

    ConnectingIOReactor connectingIOReactor = new DefaultConnectingIOReactor();

    if (_keyStore != null) {
        poolingNHttpClientConnectionManager = new PoolingNHttpClientConnectionManager(connectingIOReactor, null,
                getSchemeIOSessionStrategyRegistry(), null, null, 60000, TimeUnit.MILLISECONDS);
    } else {//from  ww w .j  av  a  2s . c o m
        poolingNHttpClientConnectionManager = new PoolingNHttpClientConnectionManager(connectingIOReactor);
    }

    poolingNHttpClientConnectionManager.setMaxTotal(20);

    return poolingNHttpClientConnectionManager;
}