Java Code Examples for org.apache.http.impl.conn.PoolingHttpClientConnectionManager#closeIdleConnections()

The following examples show how to use org.apache.http.impl.conn.PoolingHttpClientConnectionManager#closeIdleConnections() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: RestProtocol.java    From dubbo-2.6.5 with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    try {
        while (!shutdown) {
            synchronized (this) {
                wait(1000);
                for (PoolingHttpClientConnectionManager connectionManager : connectionManagers) {
                    connectionManager.closeExpiredConnections();
                    // TODO constant
                    connectionManager.closeIdleConnections(30, TimeUnit.SECONDS);
                }
            }
        }
    } catch (InterruptedException ex) {
        shutdown();
    }
}
 
Example 2
Source File: ApacheHttpClientConfig.java    From sfg-blog-posts with GNU General Public License v3.0 6 votes vote down vote up
@Bean
public Runnable idleConnectionMonitor(PoolingHttpClientConnectionManager pool) {
  return new Runnable() {
    @Override
    @Scheduled(fixedDelay = 20000)
    public void run() {
      // only if connection pool is initialised
      if (pool != null) {
        pool.closeExpiredConnections();
        pool.closeIdleConnections(IDLE_CONNECTION_WAIT_TIME, TimeUnit.MILLISECONDS);

        LOG.info("Idle connection monitor: Closing expired and idle connections");
      }
    }
  };
}
 
Example 3
Source File: HttpClientConfig.java    From wecube-platform with Apache License 2.0 6 votes vote down vote up
@Bean
public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
    return new Runnable() {
        @Override
        @Scheduled(fixedDelay = 10000)
        public void run() {
            try {
                if (connectionManager != null) {
                    log.trace("run IdleConnectionMonitor - Closing expired and idle connections...");
                    connectionManager.closeExpiredConnections();
                    connectionManager.closeIdleConnections(
                            httpClientProperties.getCloseIdleConnectionWaitTimeSecs(), TimeUnit.SECONDS);
                } else {
                    log.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
                }
            } catch (Exception e) {
                log.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
            }
        }
    };
}
 
Example 4
Source File: HttpClientConfig.java    From SpringBootBucket with MIT License 6 votes vote down vote up
@Bean
public Runnable idleConnectionMonitor(final PoolingHttpClientConnectionManager connectionManager) {
    return new Runnable() {
        @Override
        @Scheduled(fixedDelay = 10000)
        public void run() {
            try {
                if (connectionManager != null) {
                    LOGGER.trace("run IdleConnectionMonitor - Closing expired and idle connections...");
                    connectionManager.closeExpiredConnections();
                    connectionManager.closeIdleConnections(p.getCloseIdleConnectionWaitTimeSecs(), TimeUnit.SECONDS);
                } else {
                    LOGGER.trace("run IdleConnectionMonitor - Http Client Connection manager is not initialised");
                }
            } catch (Exception e) {
                LOGGER.error("run IdleConnectionMonitor - Exception occurred. msg={}, e={}", e.getMessage(), e);
            }
        }
    };
}
 
Example 5
Source File: UniverseClient.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
public UniverseClient(String user, String password, String apiUrl, String authType,
                      int queryTimeout) {
  RequestConfig requestConfig = RequestConfig.custom()
      .setConnectTimeout(queryTimeout)
      .setSocketTimeout(queryTimeout)
      .build();
  PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
  cm.setMaxTotal(100);
  cm.setDefaultMaxPerRoute(100);
  cm.closeIdleConnections(10, TimeUnit.MINUTES);
  httpClient = HttpClientBuilder.create()
      .setConnectionManager(cm)
      .setDefaultRequestConfig(requestConfig)
      .build();

  this.user = user;
  this.password = password;
  this.authType = authType;
  if (StringUtils.isNotBlank(apiUrl)) {
    this.apiUrl = apiUrl.replaceAll("/$", "");
  }
}
 
Example 6
Source File: HttpClientInstance.java    From sofa-tracer with Apache License 2.0 5 votes vote down vote up
private CloseableHttpClient getHttpClient() {
    if (this.httpClient != null) {
        return this.httpClient;
    }
    PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setDefaultMaxPerRoute(6);
    connManager.setMaxTotal(20);
    connManager.closeIdleConnections(120, TimeUnit.SECONDS);
    HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
    //SOFATracer
    SofaTracerHttpClientBuilder.clientBuilder(httpClientBuilder);
    httpClient = httpClientBuilder.setConnectionManager(connManager).disableAutomaticRetries()
        .setUserAgent("CLIENT_VERSION").build();
    return httpClient;
}
 
Example 7
Source File: DefaultHttpRequestProcessor.java    From sofa-lookout with Apache License 2.0 5 votes vote down vote up
/**
 * lazy init singleton.
 *
 * @return CloseableHttpClient http client
 */
static CloseableHttpClient getHttpClent() {
    if (httpClientCache != null) {
        return httpClientCache;
    }
    if (httpClientInitialized.compareAndSet(false, true)) {
        final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
        connManager.setDefaultMaxPerRoute(2);
        connManager.setMaxTotal(4);
        httpClientCache = HttpClientBuilder.create().setConnectionManager(connManager)
            .setRetryHandler(new DefaultHttpRequestRetryHandler(1, false))
            .setUserAgent(CLIENT_VERSION).build();
        clearIdleConnectionsTask = new Runnable() {
            @Override
            public void run() {
                try {
                    connManager.closeIdleConnections(30, TimeUnit.SECONDS);
                    connManager.closeExpiredConnections();
                } catch (Throwable e) {
                    logger.warn("fail to close idle connections.{}", e.getMessage());
                }
            }
        };
        return httpClientCache;
    }
    return null; //发生并发初始化情况;
}
 
Example 8
Source File: AbstractResourceIT.java    From fcrepo-import-export with Apache License 2.0 5 votes vote down vote up
AbstractResourceIT() {
    clientBuilder = FcrepoClient.client().credentials(USERNAME, PASSWORD).authScope("localhost");
    setDefaultHttpClient(new FcrepoHttpClientBuilder(USERNAME, PASSWORD, "localhost").build());

    final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
    connectionManager.setMaxTotal(Integer.MAX_VALUE);
    connectionManager.setDefaultMaxPerRoute(20);
    connectionManager.closeIdleConnections(3, TimeUnit.SECONDS);
}
 
Example 9
Source File: ApiClient.java    From amex-api-java-client-core with Apache License 2.0 4 votes vote down vote up
private CloseableHttpClient createClient() {

        try {

            final SSLContext sslcontext = SSLContexts.custom().useProtocol("TLSv1.2").build();
            SSLConnectionSocketFactory sslConnectionFactory = new SSLConnectionSocketFactory(sslcontext) {

                @Override
                public Socket createLayeredSocket(
                        final Socket socket,
                        final String target,
                        final int port,
                        final HttpContext context) throws IOException {
                    context.setAttribute("__enable_sni__", true);
                    return super.createLayeredSocket(socket, target, port, context);
                }
            };

            Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
                    .register("https", sslConnectionFactory)
                    .register("http", PlainConnectionSocketFactory.getSocketFactory())
                    .build();
            PoolingHttpClientConnectionManager poolingHttpClientConnectionManager =
                    new PoolingHttpClientConnectionManager(registry);
            poolingHttpClientConnectionManager.closeExpiredConnections();

            poolingHttpClientConnectionManager
                    .closeIdleConnections((timeout != null) ? timeout : DEFAULT_TIMEOUT, TimeUnit.MILLISECONDS);
            RequestConfig requestConfig = RequestConfig
                    .custom().setConnectTimeout((timeout != null) ? timeout : DEFAULT_TIMEOUT)
                    .setConnectionRequestTimeout((timeout != null) ? timeout : DEFAULT_TIMEOUT)
                    .setSocketTimeout((timeout != null) ? timeout : DEFAULT_TIMEOUT)
                    .build();
            httpClient = HttpClients.custom().setConnectionManager(poolingHttpClientConnectionManager)
                    .setDefaultRequestConfig(requestConfig).build();
        } catch (Exception ex) {
            httpClient = HttpClients.createDefault();
        }

        return httpClient;
    }