org.apache.http.conn.HttpClientConnectionManager Java Examples

The following examples show how to use org.apache.http.conn.HttpClientConnectionManager. 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: Http4FileProvider.java    From commons-vfs with Apache License 2.0 6 votes vote down vote up
private HttpClientConnectionManager createConnectionManager(final Http4FileSystemConfigBuilder builder,
        final FileSystemOptions fileSystemOptions) throws FileSystemException {
    final PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager();
    connManager.setMaxTotal(builder.getMaxTotalConnections(fileSystemOptions));
    connManager.setDefaultMaxPerRoute(builder.getMaxConnectionsPerHost(fileSystemOptions));

    final SocketConfig socketConfig =
            SocketConfig
            .custom()
            .setSoTimeout(builder.getSoTimeout(fileSystemOptions))
            .build();

    connManager.setDefaultSocketConfig(socketConfig);

    return connManager;
}
 
Example #2
Source File: ApacheConnectionManagerFactory.java    From ibm-cos-sdk-java with Apache License 2.0 6 votes vote down vote up
@Override
public HttpClientConnectionManager create(final HttpClientSettings settings) {
    ConnectionSocketFactory sslsf = getPreferredSocketFactory(settings);

    final PoolingHttpClientConnectionManager cm = new
            PoolingHttpClientConnectionManager(
            createSocketFactoryRegistry(sslsf),
            null,
            DefaultSchemePortResolver.INSTANCE,
            new DelegatingDnsResolver(settings.getDnsResolver()),
            settings.getConnectionPoolTTL(),
            TimeUnit.MILLISECONDS);

    cm.setValidateAfterInactivity(settings.getValidateAfterInactivityMillis());
    cm.setDefaultMaxPerRoute(settings.getMaxConnections());
    cm.setMaxTotal(settings.getMaxConnections());
    cm.setDefaultSocketConfig(buildSocketConfig(settings));
    cm.setDefaultConnectionConfig(buildConnectionConfig(settings));

    return cm;
}
 
Example #3
Source File: HTTPConnections.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 6 votes vote down vote up
@Override
public void freeManager(HTTPMethod method) {
  synchronized (this) {
    HttpClientConnectionManager mgr = methodmap.get(method);
    if (mgr == null)
      throw new IllegalStateException();
    mgrmap.remove(mgr, method);
    methodmap.remove(method);
    ((BasicHttpClientConnectionManager) mgr).close();
    if (method.executed) {
      // only decrement if method was executed
      this.actualconnections--;
    }
    if (TRACE)
      System.err.println("HTTPConnections: close connection: " + method.hashCode());
  }
}
 
Example #4
Source File: TestSecureRESTServer.java    From hbase with Apache License 2.0 6 votes vote down vote up
private Pair<CloseableHttpClient,HttpClientContext> getClient() {
  HttpClientConnectionManager pool = new PoolingHttpClientConnectionManager();
  HttpHost host = new HttpHost("localhost", REST_TEST.getServletPort());
  Registry<AuthSchemeProvider> authRegistry =
      RegistryBuilder.<AuthSchemeProvider>create().register(AuthSchemes.SPNEGO,
          new SPNegoSchemeFactory(true, true)).build();
  CredentialsProvider credentialsProvider = new BasicCredentialsProvider();
  credentialsProvider.setCredentials(AuthScope.ANY, EmptyCredentials.INSTANCE);
  AuthCache authCache = new BasicAuthCache();

  CloseableHttpClient client = HttpClients.custom()
      .setDefaultAuthSchemeRegistry(authRegistry)
      .setConnectionManager(pool).build();

  HttpClientContext context = HttpClientContext.create();
  context.setTargetHost(host);
  context.setCredentialsProvider(credentialsProvider);
  context.setAuthSchemeRegistry(authRegistry);
  context.setAuthCache(authCache);

  return new Pair<>(client, context);
}
 
Example #5
Source File: ElasticsearchClient.java    From elasticsearch-maven-plugin with Apache License 2.0 6 votes vote down vote up
private static HttpClient buildHttpClient(HttpClientConnectionManager connectionManager)
{
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(1500)
            .setConnectionRequestTimeout(1500)
            .build();

    CloseableHttpClient httpClient = HttpClients.custom()
            .setConnectionManager(connectionManager)
            .setDefaultRequestConfig(requestConfig)
            // use the default retry handler:
            // https://hc.apache.org/httpcomponents-client-ga/tutorial/html/fundamentals.html#d5e305
            .build();

    return httpClient;
}
 
Example #6
Source File: ClientConnectionManagerFactory.java    From aws-sdk-java-v2 with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a wrapped instance of {@link HttpClientConnectionManager}
 * to capture the necessary performance metrics.
 *
 * @param orig the target instance to be wrapped
 */
public static HttpClientConnectionManager wrap(HttpClientConnectionManager orig) {
    if (orig instanceof Wrapped) {
        throw new IllegalArgumentException();
    }
    Class<?>[] interfaces;
    if (orig instanceof ConnPoolControl) {
        interfaces = new Class<?>[]{
                HttpClientConnectionManager.class,
                ConnPoolControl.class,
                Wrapped.class};
    } else {
        interfaces = new Class<?>[]{
                HttpClientConnectionManager.class,
                Wrapped.class
        };
    }
    return (HttpClientConnectionManager) Proxy.newProxyInstance(
            // https://github.com/aws/aws-sdk-java/pull/48#issuecomment-29454423
            ClientConnectionManagerFactory.class.getClassLoader(),
            interfaces,
            new Handler(orig));
}
 
Example #7
Source File: HttpPoolClient.java    From seezoon-framework-all with Apache License 2.0 6 votes vote down vote up
private CloseableHttpClient createHttpClient(HttpClientConnectionManager connectionManager) {
	//HttpHost proxy = new HttpHost("127.0.0.1",8889);
	RequestConfig requestConfig = RequestConfig.custom()
			//.setProxy(proxy)
			.setConnectionRequestTimeout(httpClientConfig.getConnectionRequestTimeout())// 获取连接等待时间
			.setConnectTimeout(httpClientConfig.getConnectTimeout())// 连接超时
			.setSocketTimeout(httpClientConfig.getSocketTimeout())// 获取数据超时
			.build();
	httpClient = HttpClients.custom().setConnectionManager(httpClientConnectionManager)
			.setDefaultRequestConfig(requestConfig)
			.setUserAgent(httpClientConfig.getUserAgent())
			.disableContentCompression().disableAutomaticRetries()
			.setConnectionTimeToLive(httpClientConfig.getConnTimeToLive(), TimeUnit.MILLISECONDS)// 连接最大存活时间
			.setRetryHandler(new DefaultHttpRequestRetryHandler(httpClientConfig.getRetyTimes(), true))// 重试次数
			.build();
	return httpClient;
}
 
Example #8
Source File: DavMailIdleConnectionEvictor.java    From davmail with GNU General Public License v2.0 6 votes vote down vote up
private static void initEvictorThread() {
    if (thread == null) {
        thread = new Thread(() -> {
            try {
                while (!Thread.currentThread().isInterrupted()) {
                    Thread.sleep(sleepTimeMs);
                    synchronized (connectionManagers) {
                        // iterate over connection managers
                        for (HttpClientConnectionManager connectionManager : connectionManagers) {
                            connectionManager.closeExpiredConnections();
                            if (maxIdleTimeMs > 0) {
                                connectionManager.closeIdleConnections(maxIdleTimeMs, TimeUnit.MILLISECONDS);
                            }
                        }
                    }
                }
            } catch (final Exception ex) {
                LOGGER.error(ex);
            }

        }, "Connection evictor");
        thread.setDaemon(true);
    }
}
 
Example #9
Source File: MCRHttpUtils.java    From mycore with GNU General Public License v3.0 5 votes vote down vote up
public static CloseableHttpClient getHttpClient(HttpClientConnectionManager connectionManager, int maxConnections) {

        RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(30000).setSocketTimeout(30000).build();

        ConnectionConfig connectionConfig = ConnectionConfig.custom().setCharset(StandardCharsets.UTF_8).build();
        SocketConfig socketConfig = SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true)
            .setSoReuseAddress(true).build();

        //setup http client
        return HttpClients.custom().setConnectionManager(connectionManager)
            .setUserAgent(getHttpUserAgent()).setRetryHandler(new MCRRetryHandler(maxConnections))
            .setDefaultRequestConfig(requestConfig).setDefaultConnectionConfig(connectionConfig)
            .setDefaultSocketConfig(socketConfig).build();
    }
 
Example #10
Source File: LogDataProvider.java    From cuba with Apache License 2.0 5 votes vote down vote up
/**
* You should call {@link LogDataProvider#obtainUrl()} before
* */
@Override
public InputStream provide() {
    HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
    HttpClient httpClient = HttpClientBuilder.create()
            .setConnectionManager(connectionManager)
            .build();

    String uri = url + "?s=" + userSessionSource.getUserSession().getId();
    if (downloadFullLog) {
        uri += "&full=true";
    }

    HttpGet httpGet = new HttpGet(uri);

    try {
        HttpResponse httpResponse = httpClient.execute(httpGet);
        int httpStatus = httpResponse.getStatusLine().getStatusCode();
        if (httpStatus == HttpStatus.SC_OK) {
            HttpEntity httpEntity = httpResponse.getEntity();
            if (httpEntity != null) {
                inputStream = httpEntity.getContent();
            } else {
                log.debug("Unable to download log from " + url + "\nHttpEntity is null");

                throw new RuntimeException("Unable to download log from " + url + "\nHttpEntity is null");
            }
        } else {
            log.debug("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
            throw new RuntimeException("Unable to download log from " + url + "\n" + httpResponse.getStatusLine());
        }
    } catch (IOException e) {
        log.debug("Unable to download log from " + url + "\n" + e);
        throw new RuntimeException(e);
    }

    return inputStream;
}
 
Example #11
Source File: HttpClientIdleConnectionMonitor.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
public static synchronized boolean registerConnectionManager(HttpClientConnectionManager connectionManager,
		HttpClientConfig clientConfig) {
	if (instance == null) {
		instance = new HttpClientIdleConnectionMonitor(clientConfig);
		instance.start();
	}
	return connectionManagers.add(connectionManager);
}
 
Example #12
Source File: HttpPoolClient.java    From seezoon-framework-all with Apache License 2.0 5 votes vote down vote up
public  HttpClientConnectionManager createHttpClientConnectionManager() {
	SSLContext sslContext = null;
	try {
		sslContext = SSLContexts.custom().loadTrustMaterial(null, new TrustStrategy() {
			@Override
			public boolean isTrusted(X509Certificate[] chain, String authType) throws CertificateException {
				return false;
			}
		}).build();
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
	SSLConnectionSocketFactory sslSocketFactory = new SSLConnectionSocketFactory(sslContext,
			NoopHostnameVerifier.INSTANCE);
	Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
			.register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sslSocketFactory)
			.build();
	PoolingHttpClientConnectionManager poolingHttpClientConnectionManager = new PoolingHttpClientConnectionManager(
			socketFactoryRegistry);
	// 最大连接数
	poolingHttpClientConnectionManager.setMaxTotal(httpClientConfig.getMaxTotal());
	// 单个站点最大连接数
	poolingHttpClientConnectionManager.setDefaultMaxPerRoute(httpClientConfig.getMaxPerRoute());
	// 长连接
	poolingHttpClientConnectionManager.setDefaultSocketConfig(
			SocketConfig.custom().setSoTimeout(httpClientConfig.getSocketTimeout()).setSoKeepAlive(true).build());
	// 连接不活跃多久检查毫秒 并不是100 % 可信
	poolingHttpClientConnectionManager.setValidateAfterInactivity(httpClientConfig.getValidateAfterInactivity());
	// 空闲扫描线程
	HttpClientIdleConnectionMonitor.registerConnectionManager(poolingHttpClientConnectionManager, httpClientConfig);
	return poolingHttpClientConnectionManager;
}
 
Example #13
Source File: HttpClientBase.java    From lucene-solr with Apache License 2.0 5 votes vote down vote up
/**
 * @param conMgr
 *          connection manager to use for this http client. <b>NOTE:</b>The
 *          provided {@link HttpClientConnectionManager} will not be
 *          {@link HttpClientConnectionManager#shutdown()} by this class.
 * @param defaultConfig
 *          the default {@link RequestConfig} to set on the client. If
 *          {@code null} a default config is created w/ the default connection
 *          and socket timeouts.
 */
protected HttpClientBase(String host, int port, String path, HttpClientConnectionManager conMgr, RequestConfig defaultConfig) {
  url = normalizedURL(host, port, path);
  if (defaultConfig == null) {
    this.defaultConfig = RequestConfig.custom()
        .setConnectionRequestTimeout(DEFAULT_CONNECTION_TIMEOUT)
        .setSocketTimeout(DEFAULT_SO_TIMEOUT).build();
  } else {
    this.defaultConfig = defaultConfig;
  }
  httpc = HttpClientBuilder.create().setConnectionManager(conMgr).setDefaultRequestConfig(this.defaultConfig).build();
}
 
Example #14
Source File: HTTPConnections.java    From netcdf-java with BSD 3-Clause "New" or "Revised" License 5 votes vote down vote up
@Override
public HttpClientConnectionManager newManager(HTTPMethod m) {
  synchronized (this) {
    if (TRACE)
      System.err.println("HTTPConnections: open connection: " + m.hashCode());
    this.actualconnections++;
    return getPool();
  }
}
 
Example #15
Source File: RaptorHttpClientConfiguration.java    From raptor with Apache License 2.0 5 votes vote down vote up
@Bean
@ConditionalOnMissingBean(HttpClient.class)
public CloseableHttpClient createHttpClient(ApacheHttpClientFactory httpClientFactory,
                                      HttpClientConnectionManager httpClientConnectionManager) {
    this.httpClient = httpClientFactory.createHttpClient(httpClientConnectionManager, httpClientProperties);
    return this.httpClient;
}
 
Example #16
Source File: HttpclientSpringMvcBenchmark.java    From raptor with Apache License 2.0 5 votes vote down vote up
@Setup
public void setup() {
    port = SocketUtils.findAvailableTcpPort();
    System.setProperty("server.port", String.valueOf(port));
    context = SpringApplication.run(HttpclientSpringMvcApplication.class);
    ApacheHttpClientConnectionManagerFactory factory = new ApacheHttpClientConnectionManagerFactory();
    HttpClientConnectionManager manager = factory.newConnectionManager(false, 100, 100);
    ApacheHttpClientFactory apacheHttpClientFactory = new ApacheHttpClientFactory();
    httpClient = apacheHttpClientFactory.createHttpClient(manager, new RaptorHttpClientProperties());
}
 
Example #17
Source File: FeignHttpClientConfigurationTests.java    From spring-cloud-openfeign with Apache License 2.0 5 votes vote down vote up
@Test
public void disableSslTest() throws Exception {
	HttpClientConnectionManager connectionManager = this.context
			.getBean(HttpClientConnectionManager.class);
	Lookup<ConnectionSocketFactory> socketFactoryRegistry = getConnectionSocketFactoryLookup(
			connectionManager);
	assertThat(socketFactoryRegistry.lookup("https")).isNotNull();
	assertThat(this.getX509TrustManager(socketFactoryRegistry).getAcceptedIssuers())
			.isNull();
}
 
Example #18
Source File: HttpClientFactory.java    From data-highway with Apache License 2.0 5 votes vote down vote up
public static CloseableHttpClient create(HttpClientConnectionManager connectionManager, OnrampOptions options) {
  return org.apache.http.impl.client.HttpClientBuilder
      .create()
      .setUserAgent(userAgent())
      .setConnectionManager(connectionManager)
      .setDefaultRequestConfig(requestConfig())
      .setConnectionReuseStrategy(DefaultClientConnectionReuseStrategy.INSTANCE)
      .setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
      .setRetryHandler(getRetryHandler(options))
      .setServiceUnavailableRetryStrategy(new DefaultServiceUnavailableRetryStrategy(1, 500))
      .build();
}
 
Example #19
Source File: HttpClientConfiguration.java    From interface-test with Apache License 2.0 5 votes vote down vote up
@Bean(name = "defaultHttpClientConnectionManager")
@ConditionalOnMissingBean
public HttpClientConnectionManager httpClientConnectionManager() {
    PoolingHttpClientConnectionManager clientConnectionManager = new PoolingHttpClientConnectionManager();
    clientConnectionManager.setMaxTotal(connectionManagerMaxTotal); // 整个连接池的并发
    clientConnectionManager.setDefaultMaxPerRoute(connectionManagerDefaultMaxPerRoute); // 每个主机的并发
    return clientConnectionManager;
}
 
Example #20
Source File: DefaultDockerClient.java    From docker-client with Apache License 2.0 5 votes vote down vote up
private HttpClientConnectionManager getConnectionManager(Builder builder) {
  if (builder.uri.getScheme().equals(NPIPE_SCHEME)) {
    final BasicHttpClientConnectionManager bm = 
        new BasicHttpClientConnectionManager(getSchemeRegistry(builder));
    return bm;
  } else {
    final PoolingHttpClientConnectionManager cm =
        new PoolingHttpClientConnectionManager(getSchemeRegistry(builder));
    // Use all available connections instead of artificially limiting ourselves to 2 per server.
    cm.setMaxTotal(builder.connectionPoolSize);
    cm.setDefaultMaxPerRoute(cm.getMaxTotal());
    return cm;
  }
}
 
Example #21
Source File: HttpClientBuilder.java    From docker-maven-plugin with Apache License 2.0 5 votes vote down vote up
public CloseableHttpClient buildPooledClient() throws IOException {
    org.apache.http.impl.client.HttpClientBuilder builder = HttpClients.custom();
    HttpClientConnectionManager manager = getPooledConnectionFactory(certPath, maxConnections);
    builder.setConnectionManager(manager);
    // TODO: For push-redirects working for 301, the redirect strategy should be relaxed (see #351)
    // However not sure whether we should do it right now and whether this is correct, since normally
    // a 301 should only occur when the image name is invalid (e.g. containing "//" in which case a redirect
    // happens to the URL with a single "/")
    // builder.setRedirectStrategy(new LaxRedirectStrategy());

    // TODO: Tune client if needed (e.g. add pooling factoring .....
    // But I think, that's not really required.

    return builder.build();
}
 
Example #22
Source File: DriverTest.java    From esigate with Apache License 2.0 5 votes vote down vote up
private Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager, String name) {
    Driver driver =
            Driver.builder()
                    .setName(name)
                    .setProperties(properties)
                    .setRequestExecutorBuilder(
                            HttpClientRequestExecutor.builder().setConnectionManager(connectionManager)).build();
    DriverFactory.put(name, driver);
    return driver;
}
 
Example #23
Source File: TestUtils.java    From esigate with Apache License 2.0 5 votes vote down vote up
/**
 * Create a Driver instance with a custom connection Manager.
 * 
 * @param properties
 *            the {@link Properties}
 * @param connectionManager
 *            the {@link HttpClientConnectionManager}
 * @param name
 *            name of the Driver instance
 * @return new {@link Driver} object
 */
public static Driver createMockDriver(Properties properties, HttpClientConnectionManager connectionManager,
        String name) {
    Driver driver =
            Driver.builder()
                    .setName(name)
                    .setProperties(properties)
                    .setRequestExecutorBuilder(
                            HttpClientRequestExecutor.builder().setConnectionManager(connectionManager)).build();
    DriverFactory.put(name, driver);
    return driver;
}
 
Example #24
Source File: ConnectionEvictionThreadTest.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Verify that ClientConnectionManager are called.
 */
@Test
public void connectionEvictedIn5Seconds() throws Exception {
  final HttpClientConnectionManager clientConnectionManager = mock(HttpClientConnectionManager.class);

  final ConnectionEvictionThread underTest = new ConnectionEvictionThread(clientConnectionManager, 1000, 100);
  underTest.start();

  Thread.sleep(300);

  verify(clientConnectionManager, atLeastOnce()).closeExpiredConnections();
  verify(clientConnectionManager, atLeastOnce()).closeIdleConnections(1000, TimeUnit.MILLISECONDS);

  underTest.interrupt();
}
 
Example #25
Source File: ApacheHttpClient.java    From aws-sdk-java-v2 with Apache License 2.0 5 votes vote down vote up
private ConnectionManagerAwareHttpClient createClient(ApacheHttpClient.DefaultBuilder configuration,
                                                      AttributeMap standardOptions) {
    ApacheConnectionManagerFactory cmFactory = new ApacheConnectionManagerFactory();

    HttpClientBuilder builder = HttpClients.custom();
    // Note that it is important we register the original connection manager with the
    // IdleConnectionReaper as it's required for the successful deregistration of managers
    // from the reaper. See https://github.com/aws/aws-sdk-java/issues/722.
    HttpClientConnectionManager cm = cmFactory.create(configuration, standardOptions);

    builder.setRequestExecutor(new HttpRequestExecutor())
           // SDK handles decompression
           .disableContentCompression()
           .setKeepAliveStrategy(buildKeepAliveStrategy(standardOptions))
           .disableRedirectHandling()
           .disableAutomaticRetries()
           .setUserAgent("") // SDK will set the user agent header in the pipeline. Don't let Apache waste time
           .setConnectionManager(ClientConnectionManagerFactory.wrap(cm));

    addProxyConfig(builder, configuration);

    if (useIdleConnectionReaper(standardOptions)) {
        IdleConnectionReaper.getInstance().registerConnectionManager(
                cm, standardOptions.get(SdkHttpConfigurationOption.CONNECTION_MAX_IDLE_TIMEOUT).toMillis());
    }

    return new ApacheSdkHttpClient(builder.build(), cm);
}
 
Example #26
Source File: HttpClientFactory.java    From riptide with MIT License 5 votes vote down vote up
public static CloseableHttpClient createHttpClient(final Client client,
        final List<HttpRequestInterceptor> firstRequestInterceptors,
        final HttpClientConnectionManager connectionManager,
        @Nullable final HttpClientCustomizer customizer,
        @Nullable final Object cacheStorage) {

    final Caching caching = client.getCaching();
    final HttpClientBuilder builder = caching.getEnabled() ?
            configureCaching(caching, cacheStorage) :
            HttpClientBuilder.create();

    final RequestConfig.Builder config = RequestConfig.custom();

    firstRequestInterceptors.forEach(builder::addInterceptorFirst);

    final Connections connections = client.getConnections();
    config.setConnectionRequestTimeout((int) connections.getLeaseRequestTimeout().to(MILLISECONDS));
    config.setConnectTimeout((int) connections.getConnectTimeout().to(MILLISECONDS));
    config.setSocketTimeout((int) connections.getSocketTimeout().to(MILLISECONDS));

    builder.setConnectionManager(connectionManager);
    builder.setDefaultRequestConfig(config.build());
    builder.disableAutomaticRetries();

    Optional.ofNullable(customizer).ifPresent(customize(builder));

    return builder.build();
}
 
Example #27
Source File: BufferedJestClientFactory.java    From log4j2-elasticsearch with Apache License 2.0 5 votes vote down vote up
protected CloseableHttpClient createHttpClient(HttpClientConnectionManager connectionManager) {
    return HttpClients.custom()
                    .setConnectionManager(connectionManager)
                    .setDefaultRequestConfig(getRequestConfig())
                    .setProxyAuthenticationStrategy(wrappedHttpClientConfig.getHttpClientConfig().getProxyAuthenticationStrategy())
                    .setRoutePlanner(getRoutePlanner())
                    .setDefaultCredentialsProvider(wrappedHttpClientConfig.getHttpClientConfig().getCredentialsProvider())
    .build();
}
 
Example #28
Source File: DefaultRiptideRegistrar.java    From riptide with MIT License 5 votes vote down vote up
private String registerHttpClient(final String id, final Client client) {
    return registry.registerIfAbsent(id, HttpClient.class, () -> {
        log.debug("Client [{}]: Registering HttpClient", id);

        final String connectionManager = registry.registerIfAbsent(id, HttpClientConnectionManager.class, () ->
                genericBeanDefinition(HttpClientFactory.class)
                        .setFactoryMethod("createHttpClientConnectionManager")
                        .addConstructorArgValue(client));

        if (client.getMetrics().getEnabled()) {
            registry.registerIfAbsent(id, HttpConnectionPoolMetrics.class, () ->
                    genericBeanDefinition(HttpConnectionPoolMetrics.class)
                            .addConstructorArgReference(connectionManager)
                            .addConstructorArgValue("http.client.connections")
                            .addConstructorArgValue(ImmutableList.of(clientId(id))));
        }

        return genericBeanDefinition(HttpClientFactory.class)
                .setFactoryMethod("createHttpClient")
                .addConstructorArgValue(client)
                .addConstructorArgValue(configureFirstRequestInterceptors(id, client))
                .addConstructorArgReference(connectionManager)
                .addConstructorArgValue(registry.findRef(id, HttpClientCustomizer.class).orElse(null))
                .addConstructorArgValue(findCacheStorageReference(id, client).orElse(null))
                .setDestroyMethodName("close");
    });
}
 
Example #29
Source File: ConnectionEvictionThread.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
@VisibleForTesting
ConnectionEvictionThread(final HttpClientConnectionManager connectionManager,
                         final long idleTimeMillis,
                         final long evictingDelayMillis)
{
  super("nexus-httpclient-eviction-thread");
  checkArgument(idleTimeMillis > -1, "Keep alive period in milliseconds cannot be negative");
  checkArgument(evictingDelayMillis > 0, "Evicting delay period in milliseconds must be greater than 0");
  this.connectionManager = checkNotNull(connectionManager);
  this.idleTimeMillis = idleTimeMillis;
  this.evictingDelayMillis = evictingDelayMillis;
  setDaemon(true);
  setPriority(MIN_PRIORITY);
}
 
Example #30
Source File: IdleConnectionReaperTest.java    From ibm-cos-sdk-java with Apache License 2.0 5 votes vote down vote up
@Test
public void maxIdle_HonoredOnClose() throws InterruptedException {
    HttpClientConnectionManager connectionManager = mock(HttpClientConnectionManager.class);
    final long idleTime = 10 * 1000;
    IdleConnectionReaper.registerConnectionManager(connectionManager, idleTime);
    verify(connectionManager, timeout(90 * 1000)).closeIdleConnections(eq(idleTime), eq(TimeUnit.MILLISECONDS));

}