Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setMaxTotal.

Prototype

public void setMaxTotal(final int max) 

Source Link

Document

since 4.1

Usage

From source file:org.ancoron.osgi.test.glassfish.GlassfishDerbyTest.java

protected DefaultHttpClient getHTTPClient() throws NoSuchAlgorithmException, KeyManagementException {
    SSLContext sslContext = SSLContext.getInstance("SSL");

    // set up a TrustManager that trusts everything
    sslContext.init(null, new TrustManager[] { new X509TrustManager() {
        @Override// w  w  w  . j a  v  a2s  .co m
        public X509Certificate[] getAcceptedIssuers() {
            System.out.println("getAcceptedIssuers =============");
            return null;
        }

        @Override
        public void checkClientTrusted(X509Certificate[] certs, String authType) {
            System.out.println("checkClientTrusted =============");
        }

        @Override
        public void checkServerTrusted(X509Certificate[] certs, String authType) {
            System.out.println("checkServerTrusted =============");
        }
    } }, new SecureRandom());

    SSLSocketFactory sf = new SSLSocketFactory(sslContext, SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
    Scheme httpsScheme = new Scheme("https", 8181, sf);

    PlainSocketFactory plain = new PlainSocketFactory();
    Scheme httpScheme = new Scheme("http", 8080, plain);
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(httpsScheme);
    schemeRegistry.register(httpScheme);

    HttpParams params = new BasicHttpParams();

    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
    // Increase max total connection to 200
    cm.setMaxTotal(200);
    // Increase default max connection per route to 20
    cm.setDefaultMaxPerRoute(20);

    DefaultHttpClient httpClient = new DefaultHttpClient(cm, params);
    httpClient.getParams().setParameter(CoreProtocolPNames.PROTOCOL_VERSION, HttpVersion.HTTP_1_1);
    httpClient.getParams().setParameter(CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8");
    return httpClient;
}

From source file:com.alu.e3.gateway.loadbalancer.E3HttpClientConfigurer.java

/**
 * Configures the ClientConnectionManager
 * @param clientConnectionManager to be set
 *///from w  w  w.ja  va  2 s  .  c o m
private void setThreadSafeConnectionManager(ClientConnectionManager clientConnectionManager) {

    ThreadSafeClientConnManager threadSafeclientConnectionManager = (ThreadSafeClientConnManager) clientConnectionManager;

    // set HttpClient connection pool : global and default values
    threadSafeclientConnectionManager.setMaxTotal(getMaxConnectionsPool());
    threadSafeclientConnectionManager.setDefaultMaxPerRoute(getConnectionsPerRoute());

    // set HttpClient connection pool : max connection per route
    if (connectionsPerRoutes != null) {
        Set<TargetHost> targetHosts = connectionsPerRoutes.keySet();

        for (TargetHost targetHost : targetHosts) {
            try {
                HttpRoute route = getHttpRoute(targetHost);
                threadSafeclientConnectionManager.setMaxForRoute(route, connectionsPerRoutes.get(targetHost));
            } catch (MalformedURLException e) {
                LOGGER.warn("Unable to set a max connection for route [" + targetHost.getUrl() + "]", e);
            }
        }
    }
}

From source file:org.apache.wink.client.internal.handlers.httpclient.ApacheHttpClientConnectionHandler.java

private synchronized HttpClient openConnection(ClientRequest request)
        throws NoSuchAlgorithmException, KeyManagementException {
    if (this.httpclient != null) {
        return this.httpclient;
    }//from   w  w  w.  j ava 2  s . c  o m

    // cast is safe because we're on the client
    ApacheHttpClientConfig config = (ApacheHttpClientConfig) request.getAttribute(WinkConfiguration.class);
    BasicHttpParams params = new BasicHttpParams();
    params.setParameter(CoreConnectionPNames.CONNECTION_TIMEOUT, Integer.valueOf(config.getConnectTimeout()));
    params.setParameter(CoreConnectionPNames.SO_TIMEOUT, Integer.valueOf(config.getReadTimeout()));
    params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.valueOf(config.isFollowRedirects()));
    if (config.isFollowRedirects()) {
        params.setParameter(ClientPNames.ALLOW_CIRCULAR_REDIRECTS, Boolean.TRUE);
    }
    // setup proxy
    if (config.getProxyHost() != null) {
        params.setParameter(ConnRoutePNames.DEFAULT_PROXY,
                new HttpHost(config.getProxyHost(), config.getProxyPort()));
    }

    if (config.getMaxPooledConnections() > 0) {
        SchemeRegistry schemeRegistry = SchemeRegistryFactory.createDefault();
        ThreadSafeClientConnManager httpConnectionManager = new ThreadSafeClientConnManager(schemeRegistry);

        httpConnectionManager.setMaxTotal(config.getMaxPooledConnections());
        httpConnectionManager.setDefaultMaxPerRoute(config.getMaxPooledConnections());

        this.httpclient = new DefaultHttpClient(httpConnectionManager, params);
    } else {
        this.httpclient = new DefaultHttpClient(params);
    }

    if (config.getBypassHostnameVerification()) {
        SSLContext sslcontext = SSLContext.getInstance("TLS");
        sslcontext.init(null, null, null);

        SSLSocketFactory sf = new SSLSocketFactory(sslcontext, new X509HostnameVerifier() {

            public boolean verify(String hostname, SSLSession session) {
                return true;
            }

            public void verify(String host, String[] cns, String[] subjectAlts) throws SSLException {
            }

            public void verify(String host, X509Certificate cert) throws SSLException {
            }

            public void verify(String host, SSLSocket ssl) throws IOException {
            }
        });
        httpclient.getConnectionManager().getSchemeRegistry().register(new Scheme("https", 443, sf));
    }

    return this.httpclient;
}

From source file:org.commonjava.web.json.test.WebFixture.java

@Override
protected void before() throws Exception {
    final ThreadSafeClientConnManager ccm = new ThreadSafeClientConnManager();
    ccm.setMaxTotal(20);

    http = new DefaultHttpClient(ccm);
    http.setCredentialsProvider(new CredentialsProvider() {

        @Override/*from   w w w . j av a2  s  . com*/
        public void setCredentials(final AuthScope authscope, final Credentials credentials) {
        }

        @Override
        public Credentials getCredentials(final AuthScope authscope) {
            if (user != null) {
                return new UsernamePasswordCredentials(user, pass);
            }

            return null;
        }

        @Override
        public void clear() {
        }
    });
}

From source file:org.apache.abdera2.common.protocol.BasicClient.java

/**
 * Return the maximum number of connections allowed for the client
 *//*w w  w  . j  a v  a 2  s .c  o m*/
public <T extends Client> T setMaxConnectionsTotal(int max) {
    ClientConnectionManager ccm = client.getConnectionManager();
    if (ccm instanceof ThreadSafeClientConnManager) {
        ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) client.getConnectionManager();
        cm.setMaxTotal(max);
    } else {
        throw new IllegalStateException();
    }
    return (T) this;
}

From source file:org.keycloak.adapters.cloned.HttpClientBuilder.java

public HttpClient build() {
    X509HostnameVerifier verifier = null;
    if (this.verifier != null)
        verifier = new VerifierWrapper(this.verifier);
    else {/* w  w w  .j  a  va  2s  .co m*/
        switch (policy) {
        case ANY:
            verifier = new AllowAllHostnameVerifier();
            break;
        case WILDCARD:
            verifier = new BrowserCompatHostnameVerifier();
            break;
        case STRICT:
            verifier = new StrictHostnameVerifier();
            break;
        }
    }
    try {
        SSLSocketFactory sslsf = null;
        SSLContext theContext = sslContext;
        if (disableTrustManager) {
            theContext = SSLContext.getInstance("SSL");
            theContext.init(null, new TrustManager[] { new PassthroughTrustManager() }, new SecureRandom());
            verifier = new AllowAllHostnameVerifier();
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (theContext != null) {
            sslsf = new SniSSLSocketFactory(theContext, verifier);
        } else if (clientKeyStore != null || truststore != null) {
            sslsf = new SniSSLSocketFactory(SSLSocketFactory.TLS, clientKeyStore, clientPrivateKeyPassword,
                    truststore, null, verifier);
        } else {
            final SSLContext tlsContext = SSLContext.getInstance(SSLSocketFactory.TLS);
            tlsContext.init(null, null, null);
            sslsf = new SniSSLSocketFactory(tlsContext, verifier);
        }
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
        Scheme httpsScheme = new Scheme("https", 443, sslsf);
        registry.register(httpsScheme);
        ClientConnectionManager cm = null;
        if (connectionPoolSize > 0) {
            ThreadSafeClientConnManager tcm = new ThreadSafeClientConnManager(registry, connectionTTL,
                    connectionTTLUnit);
            tcm.setMaxTotal(connectionPoolSize);
            if (maxPooledPerRoute == 0)
                maxPooledPerRoute = connectionPoolSize;
            tcm.setDefaultMaxPerRoute(maxPooledPerRoute);
            cm = tcm;

        } else {
            cm = new SingleClientConnManager(registry);
        }
        BasicHttpParams params = new BasicHttpParams();
        params.setParameter(ClientPNames.COOKIE_POLICY, CookiePolicy.BROWSER_COMPATIBILITY);

        if (proxyHost != null) {
            params.setParameter(ConnRoutePNames.DEFAULT_PROXY, proxyHost);
        }

        if (socketTimeout > -1) {
            HttpConnectionParams.setSoTimeout(params, (int) socketTimeoutUnits.toMillis(socketTimeout));

        }
        if (establishConnectionTimeout > -1) {
            HttpConnectionParams.setConnectionTimeout(params,
                    (int) establishConnectionTimeoutUnits.toMillis(establishConnectionTimeout));
        }
        DefaultHttpClient client = new DefaultHttpClient(cm, params);

        if (disableCookieCache) {
            client.setCookieStore(new CookieStore() {
                @Override
                public void addCookie(Cookie cookie) {
                    //To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public List<Cookie> getCookies() {
                    return Collections.emptyList();
                }

                @Override
                public boolean clearExpired(Date date) {
                    return false; //To change body of implemented methods use File | Settings | File Templates.
                }

                @Override
                public void clear() {
                    //To change body of implemented methods use File | Settings | File Templates.
                }
            });

        }
        return client;
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:org.apache.abdera2.common.protocol.BasicClient.java

/**
 * Default initialization of the Client Connection Manager,
 * subclasses may overload this to customize the connection
 * manager configuration//  w ww .  j  a  va  2 s .  c o  m
 */
protected ClientConnectionManager initConnectionManager(SchemeRegistry sr) {
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(sr);
    cm.setDefaultMaxPerRoute(initDefaultMaxConnectionsPerRoute());
    cm.setMaxTotal(initDefaultMaxTotalConnections());
    return cm;
}

From source file:org.nuxeo.wizard.download.PackageDownloader.java

protected PackageDownloader() {
    SchemeRegistry registry = new SchemeRegistry();
    registry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    registry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));
    HttpParams httpParams = new BasicHttpParams();
    HttpProtocolParams.setUseExpectContinue(httpParams, false);
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(registry);
    cm.setMaxTotal(NB_DOWNLOAD_THREADS);
    cm.setDefaultMaxPerRoute(NB_DOWNLOAD_THREADS);
    httpClient = new DefaultHttpClient(cm, httpParams);
}

From source file:org.opensaml.util.http.HttpClientBuilder.java

/**
 * Builds the connection manager used by the HTTP client. If {@link #connectionPooling} is false then the
 * {@link SingleClientConnManager} is used. Otherwise the {@link ThreadSafeClientConnManager} is used with
 * {@link ThreadSafeClientConnManager#setDefaultMaxPerRoute(int)} set to {@link #connectionsMaxPerRoute} and
 * {@link ThreadSafeClientConnManager#setMaxTotalConnections(int)} set to {@link #connectionsMaxTotal}.
 * /* w w w .  ja  v  a2 s.  c  o m*/
 * @return the connection manager used by the HTTP client
 */
private ClientConnectionManager buildConnectionManager() {
    final SchemeRegistry registry = buildSchemeRegistry();

    if (!connectionPooling) {
        return new SingleClientConnManager(registry);
    } else {
        final ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(registry);
        manager.setDefaultMaxPerRoute(connectionsMaxPerRoute);
        manager.setMaxTotal(connectionsMaxTotal);
        return manager;
    }
}

From source file:org.wrml.core.www.WebClient.java

public WebClient(Context context) {
    super(context);

    final ThreadSafeClientConnManager connectionManager = new ThreadSafeClientConnManager();

    // TODO: Make this configurable
    connectionManager.setMaxTotal(100);

    _HttpClient = new DefaultHttpClient(connectionManager);

    // TODO: Make this configurable
    _DefaultFormatter = new DefaultFormatter();
}