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

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

Introduction

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

Prototype

public void setMaxTotal(final int max) 

Source Link

Usage

From source file:org.brutusin.rpc.client.http.HttpEndpoint.java

public HttpEndpoint(URI endpoint, Config cfg, HttpClientContextFactory clientContextFactory) {
    if (endpoint == null) {
        throw new IllegalArgumentException("Endpoint is required");
    }//from   w  w w. j  av  a  2 s . c om
    if (cfg == null) {
        cfg = new ConfigurationBuilder().build();
    }
    CacheConfig cacheConfig = CacheConfig.custom().setMaxCacheEntries(cfg.getMaxCacheEntries())
            .setMaxObjectSize(cfg.getMaxCacheObjectSize()).build();
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(1000 * cfg.getConnectTimeOutSeconds())
            .setSocketTimeout(1000 * cfg.getSocketTimeOutSeconds()).build();

    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
    cm.setMaxTotal(cfg.getMaxConections());

    this.endpoint = endpoint;
    this.httpClient = CachingHttpClients.custom().setCacheConfig(cacheConfig)
            .setDefaultRequestConfig(requestConfig).setRetryHandler(new StandardHttpRequestRetryHandler())
            .setConnectionManager(cm).build();
    this.clientContextFactory = clientContextFactory;
    initPingThread(cfg.getPingSeconds());
}

From source file:net.yacy.cora.protocol.http.HTTPClient.java

/**
 * Initialize the maximum connections for the given pool
 * /* w  ww .  ja v a 2  s  .co m*/
 * @param pool
 *            a pooling connection manager. Must not be null.
 * @param maxConnections.
 *            The new maximum connections values. Must be greater than 0.
 * @throws IllegalArgumentException
 *             when pool is null or when maxConnections is lower than 1
 */
public static void initPoolMaxConnections(final PoolingHttpClientConnectionManager pool, int maxConnections) {
    if (pool == null) {
        throw new IllegalArgumentException("pool parameter must not be null");
    }
    if (maxConnections <= 0) {
        throw new IllegalArgumentException("maxConnections parameter must be greater than zero");
    }
    pool.setMaxTotal(maxConnections);
    // for statistics same value should also be set here
    ConnectionInfo.setMaxcount(maxConnections);

    // connections per host (2 default)
    pool.setDefaultMaxPerRoute((int) (2 * Memory.cores()));

    // Increase max connections for localhost
    final HttpHost localhost = new HttpHost(Domains.LOCALHOST);
    pool.setMaxPerRoute(new HttpRoute(localhost), maxConnections);
}

From source file:org.glassfish.jersey.examples.sseitemstore.jaxrs.JaxrsItemStoreResourceTest.java

@Override
protected void configureClient(ClientConfig config) {
    // using AHC as a test client connector to avoid issues with HttpUrlConnection socket management.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    // adjusting max. connections just to be safe - the testEventSourceReconnect is quite greedy...
    cm.setMaxTotal(MAX_LISTENERS * MAX_ITEMS);
    cm.setDefaultMaxPerRoute(MAX_LISTENERS * MAX_ITEMS);

    config.property(ApacheClientProperties.CONNECTION_MANAGER, cm).property(ClientProperties.READ_TIMEOUT, 2000)
            .connectorProvider(new ApacheConnectorProvider());
}

From source file:org.apache.dubbo.rpc.protocol.rest.RestProtocol.java

@Override
protected <T> T doRefer(Class<T> serviceType, URL url) throws RpcException {
    if (connectionMonitor == null) {
        connectionMonitor = new ConnectionMonitor();
    }/*from  www .j a  v  a 2 s .  co  m*/

    // TODO more configs to add
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    // 20 is the default maxTotal of current PoolingClientConnectionManager
    connectionManager.setMaxTotal(url.getParameter(Constants.CONNECTIONS_KEY, 20));
    connectionManager.setDefaultMaxPerRoute(url.getParameter(Constants.CONNECTIONS_KEY, 20));

    connectionMonitor.addConnectionManager(connectionManager);
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT))
            .setSocketTimeout(url.getParameter(Constants.TIMEOUT_KEY, Constants.DEFAULT_TIMEOUT)).build();

    SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build();

    CloseableHttpClient httpClient = HttpClientBuilder.create()
            .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {
                @Override
                public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
                    HeaderElementIterator it = new BasicHeaderElementIterator(
                            response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                    while (it.hasNext()) {
                        HeaderElement he = it.nextElement();
                        String param = he.getName();
                        String value = he.getValue();
                        if (value != null && param.equalsIgnoreCase("timeout")) {
                            return Long.parseLong(value) * 1000;
                        }
                    }
                    // TODO constant
                    return 30 * 1000;
                }
            }).setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig).build();

    ApacheHttpClient4Engine engine = new ApacheHttpClient4Engine(httpClient/*, localContext*/);

    ResteasyClient client = new ResteasyClientBuilder().httpEngine(engine).build();
    clients.add(client);

    client.register(RpcContextFilter.class);
    for (String clazz : Constants.COMMA_SPLIT_PATTERN.split(url.getParameter(Constants.EXTENSION_KEY, ""))) {
        if (!StringUtils.isEmpty(clazz)) {
            try {
                client.register(Thread.currentThread().getContextClassLoader().loadClass(clazz.trim()));
            } catch (ClassNotFoundException e) {
                throw new RpcException("Error loading JAX-RS extension class: " + clazz.trim(), e);
            }
        }
    }

    // TODO protocol
    ResteasyWebTarget target = client
            .target("http://" + url.getHost() + ":" + url.getPort() + "/" + getContextPath(url));
    return target.proxy(serviceType);
}

From source file:org.talend.dataprep.configuration.HttpClient.java

/**
 * @return the http connection manager./*from   w w w  .ja va 2  s  .com*/
 */
@Bean(destroyMethod = "shutdown")
public PoolingHttpClientConnectionManager getConnectionManager() {

    // fallback to default implementation
    if (sslSocketFactory == null) {
        sslSocketFactory = SSLConnectionSocketFactory.getSocketFactory();
    }

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .register("https", sslSocketFactory).build());

    connectionManager.setMaxTotal(maxPoolSize);
    connectionManager.setDefaultMaxPerRoute(maxPerRoute);
    return connectionManager;
}

From source file:org.glassfish.jersey.examples.sseitemstore.ItemStoreResourceTest.java

@Override
protected void configureClient(ClientConfig config) {
    // using AHC as a test client connector to avoid issues with HttpUrlConnection socket management.
    PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();

    // adjusting max. connections just to be safe - the testEventSourceReconnect is quite greedy...
    cm.setMaxTotal(MAX_LISTENERS * MAX_ITEMS);
    cm.setDefaultMaxPerRoute(MAX_LISTENERS * MAX_ITEMS);

    config.register(SseFeature.class).property(ApacheClientProperties.CONNECTION_MANAGER, cm)
            .property(ClientProperties.READ_TIMEOUT, 2000).connectorProvider(new ApacheConnectorProvider());
}

From source file:com.aliyun.oss.common.comm.DefaultServiceClient.java

protected HttpClientConnectionManager createHttpClientConnectionManager() {
    SSLContext sslContext = null;
    try {//w  w  w  . java2  s .  c o  m
        sslContext = new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {

            @Override
            public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
                return true;
            }

        }).build();

    } catch (Exception e) {
        throw new ClientException(e.getMessage());
    }

    SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
            NoopHostnameVerifier.INSTANCE);
    Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
            .register(Protocol.HTTP.toString(), PlainConnectionSocketFactory.getSocketFactory())
            .register(Protocol.HTTPS.toString(), sslSocketFactory).build();

    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(
            socketFactoryRegistry);
    connectionManager.setDefaultMaxPerRoute(config.getMaxConnections());
    connectionManager.setMaxTotal(config.getMaxConnections());
    connectionManager.setValidateAfterInactivity(config.getValidateAfterInactivity());
    connectionManager.setDefaultSocketConfig(
            SocketConfig.custom().setSoTimeout(config.getSocketTimeout()).setTcpNoDelay(true).build());
    if (config.isUseReaper()) {
        IdleConnectionReaper.setIdleConnectionTime(config.getIdleConnectionTime());
        IdleConnectionReaper.registerConnectionManager(connectionManager);
    }
    return connectionManager;
}

From source file:de.rwth.dbis.acis.activitytracker.service.ActivityTrackerService.java

@GET
@Path("/")
@Produces(MediaType.APPLICATION_JSON)/*from  ww  w.  j ava2s .  c o m*/
@ApiOperation(value = "This method returns a list of activities", notes = "Default the latest ten activities will be returned")
@ApiResponses(value = {
        @ApiResponse(code = HttpURLConnection.HTTP_OK, message = "Returns a list of activities"),
        @ApiResponse(code = HttpURLConnection.HTTP_NOT_FOUND, message = "Not found"),
        @ApiResponse(code = HttpURLConnection.HTTP_INTERNAL_ERROR, message = "Internal server problems") })
//TODO add filter
public HttpResponse getActivities(
        @ApiParam(value = "Before cursor pagination", required = false) @DefaultValue("-1") @QueryParam("before") int before,
        @ApiParam(value = "After cursor pagination", required = false) @DefaultValue("-1") @QueryParam("after") int after,
        @ApiParam(value = "Limit of elements of components", required = false) @DefaultValue("10") @QueryParam("limit") int limit,
        @ApiParam(value = "User authorization token", required = false) @DefaultValue("") @HeaderParam("authorization") String authorizationToken) {

    DALFacade dalFacade = null;
    try {
        if (before != -1 && after != -1) {
            ExceptionHandler.getInstance().throwException(ExceptionLocation.ACTIVITIESERVICE,
                    ErrorCode.WRONG_PARAMETER, "both: before and after parameter not possible");
        }
        int cursor = before != -1 ? before : after;
        Pageable.SortDirection sortDirection = after != -1 ? Pageable.SortDirection.ASC
                : Pageable.SortDirection.DESC;

        PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
        cm.setMaxTotal(20);
        CloseableHttpClient httpclient = HttpClients.custom().setConnectionManager(cm).build();

        dalFacade = getDBConnection();
        Gson gson = new Gson();
        ExecutorService executor = Executors.newCachedThreadPool();

        int getObjectCount = 0;
        PaginationResult<Activity> activities;
        List<ActivityEx> activitiesEx = new ArrayList<>();
        Pageable pageInfo = new PageInfo(cursor, limit, "", sortDirection);
        while (activitiesEx.size() < limit && getObjectCount < 5) {
            pageInfo = new PageInfo(cursor, limit, "", sortDirection);
            activities = dalFacade.findActivities(pageInfo);
            getObjectCount++;
            cursor = sortDirection == Pageable.SortDirection.ASC ? cursor + limit : cursor - limit;
            if (cursor < 0) {
                cursor = 0;
            }
            activitiesEx.addAll(
                    getObjectBodies(httpclient, executor, authorizationToken, activities.getElements()));
        }

        executor.shutdown();
        if (activitiesEx.size() > limit) {
            activitiesEx = activitiesEx.subList(0, limit);
        }
        PaginationResult<ActivityEx> activitiesExResult = new PaginationResult<>(pageInfo, activitiesEx);

        HttpResponse response = new HttpResponse(gson.toJson(activitiesExResult.getElements()),
                HttpURLConnection.HTTP_OK);
        Map<String, String> parameter = new HashMap<>();
        parameter.put("limit", String.valueOf(limit));
        response = this.addPaginationToHtppResponse(activitiesExResult, "", parameter, response);

        return response;

    } catch (ActivityTrackerException atException) {
        return new HttpResponse(ExceptionHandler.getInstance().toJSON(atException),
                HttpURLConnection.HTTP_INTERNAL_ERROR);
    } catch (Exception ex) {
        ActivityTrackerException atException = ExceptionHandler.getInstance().convert(ex,
                ExceptionLocation.ACTIVITIESERVICE, ErrorCode.UNKNOWN, ex.getMessage());
        return new HttpResponse(ExceptionHandler.getInstance().toJSON(atException),
                HttpURLConnection.HTTP_INTERNAL_ERROR);
    } finally {
        closeDBConnection(dalFacade);
    }
}

From source file:com.helger.pd.client.jdk6.PDClient.java

@Nonnull
protected HttpClientBuilder createClientBuilder() {
    SSLConnectionSocketFactory aSSLSocketFactory = null;
    try {//from  w w  w. j a va2 s . com
        // Set SSL context
        final KeyStore aKeyStore = KeyStoreHelper.loadKeyStore(PDClientConfiguration.getKeyStorePath(),
                PDClientConfiguration.getKeyStorePassword());
        final SSLContext aSSLContext = SSLContexts.custom().loadKeyMaterial(aKeyStore,
                PDClientConfiguration.getKeyStoreKeyPassword(), new PrivateKeyStrategy() {
                    public String chooseAlias(final Map<String, PrivateKeyDetails> aAliases,
                            final Socket aSocket) {
                        final String sAlias = PDClientConfiguration.getKeyStoreKeyAlias();
                        return aAliases.containsKey(sAlias) ? sAlias : null;
                    }
                }).build();
        // Allow TLSv1 protocol only
        aSSLSocketFactory = new SSLConnectionSocketFactory(aSSLContext, new String[] { "TLSv1" }, null,
                SSLConnectionSocketFactory.getDefaultHostnameVerifier());
    } catch (final Throwable t) {
        s_aLogger.error("Failed to initialize keystore for service connection! Can only use http now!", t);
    }

    try {
        final RegistryBuilder<ConnectionSocketFactory> aRB = RegistryBuilder.<ConnectionSocketFactory>create()
                .register("http", PlainConnectionSocketFactory.getSocketFactory());
        if (aSSLSocketFactory != null)
            aRB.register("https", aSSLSocketFactory);
        final Registry<ConnectionSocketFactory> sfr = aRB.build();

        final PoolingHttpClientConnectionManager aConnMgr = new PoolingHttpClientConnectionManager(sfr);
        aConnMgr.setDefaultMaxPerRoute(100);
        aConnMgr.setMaxTotal(200);
        aConnMgr.setValidateAfterInactivity(1000);
        final ConnectionConfig aConnectionConfig = ConnectionConfig.custom()
                .setMalformedInputAction(CodingErrorAction.IGNORE)
                .setUnmappableInputAction(CodingErrorAction.IGNORE).setCharset(Consts.UTF_8).build();
        aConnMgr.setDefaultConnectionConfig(aConnectionConfig);

        return HttpClientBuilder.create().setConnectionManager(aConnMgr);
    } catch (final Exception ex) {
        throw new InitializationException("Failed to init HTTP client", ex);
    }
}

From source file:AIR.Common.Web.HttpWebHelper.java

public HttpWebHelper() {
    PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(100000000,
            TimeUnit.SECONDS);//from w  w  w.  ja v  a2s.com
    connectionManager.setMaxTotal(50);
    _client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
}