Java Code Examples for org.apache.http.impl.nio.client.HttpAsyncClients#custom()

The following examples show how to use org.apache.http.impl.nio.client.HttpAsyncClients#custom() . 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: SaltService.java    From uyuni with GNU General Public License v2.0 6 votes vote down vote up
/**
 * Default constructor
 */
public SaltService() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(0)
            .setSocketTimeout(0)
            .setConnectionRequestTimeout(0)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    asyncHttpClient = httpClientBuilder
            .setMaxConnPerRoute(20)
            .setMaxConnTotal(20)
            .build();
    asyncHttpClient.start();

    SALT_CLIENT = new SaltClient(SALT_MASTER_URI, new HttpAsyncClientImpl(asyncHttpClient));
    saltSSHService = new SaltSSHService(SALT_CLIENT, SaltActionChainGeneratorService.INSTANCE);
    defaultBatch = Batch.custom().withBatchAsAmount(ConfigDefaults.get().getSaltBatchSize())
                    .withDelay(ConfigDefaults.get().getSaltBatchDelay())
                    .withPresencePingTimeout(ConfigDefaults.get().getSaltPresencePingTimeout())
                    .withPresencePingGatherJobTimeout(ConfigDefaults.get().getSaltPresencePingGatherJobTimeout())
                    .build();
}
 
Example 2
Source File: HttpProxyClient.java    From zeppelin with Apache License 2.0 6 votes vote down vote up
private CloseableHttpAsyncClient getAsyncProxyHttpClient(URI proxyUri) {
  LOG.info("Creating async proxy http client");
  PoolingNHttpClientConnectionManager cm = getAsyncConnectionManager();
  HttpHost proxy = new HttpHost(proxyUri.getHost(), proxyUri.getPort());
  
  HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();
  if (cm != null) {
    clientBuilder = clientBuilder.setConnectionManager(cm);
  }

  if (proxy != null) {
    clientBuilder = clientBuilder.setProxy(proxy);
  }
  clientBuilder = setRedirects(clientBuilder);
  return clientBuilder.build();
}
 
Example 3
Source File: SiteToSiteRestApiClient.java    From localization_nifi with Apache License 2.0 5 votes vote down vote up
private void setupAsyncClient() {
    final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();

    if (sslContext != null) {
        clientBuilder.setSSLContext(sslContext);
        clientBuilder.addInterceptorFirst(new HttpsResponseInterceptor());
    }

    httpAsyncClient = clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider()).build();
    httpAsyncClient.start();
}
 
Example 4
Source File: AsyncHttpClientGenerator.java    From cetty with Apache License 2.0 5 votes vote down vote up
@Override
protected CloseableHttpAsyncClient build(Payload payload) {
    HttpAsyncClientBuilder asyncClientBuilder = HttpAsyncClients.custom();

    if (StringUtils.isNotBlank(payload.getUserAgent())) {
        asyncClientBuilder.setUserAgent(payload.getUserAgent());
    } else {
        asyncClientBuilder.setUserAgent("");
    }

    asyncClientBuilder.setRedirectStrategy(new CustomRedirectStrategy());

    asyncClientBuilder.setConnectionManagerShared(true);

    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(payload.getConnectTimeout())
            .setSocketTimeout(payload.getSocketTimeout()).build();

    ConnectionConfig connectionConfig = ConnectionConfig.custom()
            .setMalformedInputAction(CodingErrorAction.IGNORE)
            .setUnmappableInputAction(CodingErrorAction.IGNORE)
            .setCharset(Consts.UTF_8).build();

    poolingNHttpClientConnectionManager.setDefaultConnectionConfig(connectionConfig);
    asyncClientBuilder.setConnectionManager(poolingNHttpClientConnectionManager);
    asyncClientBuilder.setDefaultRequestConfig(requestConfig);
    if (payload.getProxy() != null) {
        Proxy proxy = payload.getProxy();
        HttpHost httpHost = new HttpHost(proxy.getHost(), proxy.getPort(), proxy.getScheme());
        asyncClientBuilder.setProxy(httpHost);
    }
    reduceCookie(asyncClientBuilder,payload);
    return asyncClientBuilder.build();
}
 
Example 5
Source File: AsyncInternalClient.java    From fc-java-sdk with MIT License 5 votes vote down vote up
private CloseableHttpAsyncClient createHttpAsyncClient(Config config, PoolingNHttpClientConnectionManager cm){
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setConnectionManager(cm);
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(config.getConnectTimeoutMillis())
            .setConnectionRequestTimeout(config.getConnectTimeoutMillis())
            .setSocketTimeout(config.getReadTimeoutMillis())
            .build();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    httpClientBuilder.setUserAgent(config.getUserAgent());
    httpClientBuilder.disableCookieManagement();

    return httpClientBuilder.build();
}
 
Example 6
Source File: HttpFactory.java    From aliyun-tablestore-java-sdk with Apache License 2.0 5 votes vote down vote up
public static CloseableHttpAsyncClient createHttpAsyncClient(
        ClientConfiguration config, PoolingNHttpClientConnectionManager cm) {
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setConnectionManager(cm);
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(config.getConnectionTimeoutInMillisecond())
            .setSocketTimeout(config.getSocketTimeoutInMillisecond()).build();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);
    httpClientBuilder.setUserAgent(Constants.USER_AGENT);
    httpClientBuilder.disableCookieManagement();

    String proxyHost = config.getProxyHost();
    int proxyPort = config.getProxyPort();
    if (proxyHost != null) {
        if (proxyPort <= 0) {
            throw new ClientException("The proxy port is invalid. Please check your configuration.");
        }
        HttpHost proxy = new HttpHost(proxyHost, proxyPort);
        httpClientBuilder.setProxy(proxy);
        String proxyUsername = config.getProxyUsername();
        String proxyPassword = config.getProxyPassword();
        if (proxyUsername != null && proxyPassword != null) {
            String proxyDomain = config.getProxyDomain();
            String proxyWorkstation = config.getProxyWorkstation();
            CredentialsProvider credsProvider = new BasicCredentialsProvider();
            credsProvider.setCredentials(
                new AuthScope(proxyHost, proxyPort),
                new NTCredentials(
                    proxyUsername, proxyPassword, proxyWorkstation, proxyDomain));
            httpClientBuilder.setDefaultCredentialsProvider(credsProvider);
        }
    }

    return httpClientBuilder.build();
}
 
Example 7
Source File: HttpClientUtils.java    From salt-netapi-client with MIT License 5 votes vote down vote up
/**
 * Creates a simple default http client
 * @return HttpAsyncClient
 */
public static CloseableHttpAsyncClient defaultClient() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(0)
            .setConnectTimeout(10000)
            .setSocketTimeout(20000)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    CloseableHttpAsyncClient asyncHttpClient = httpClientBuilder.build();
    asyncHttpClient.start();
    return asyncHttpClient;
}
 
Example 8
Source File: TestUtils.java    From salt-netapi-client with MIT License 5 votes vote down vote up
public static CloseableHttpAsyncClient defaultClient() {
    RequestConfig requestConfig = RequestConfig.custom()
            .setConnectionRequestTimeout(0)
            .setConnectTimeout(0)
            .setSocketTimeout(0)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    CloseableHttpAsyncClient asyncHttpClient = httpClientBuilder.build();
    asyncHttpClient.start();
    return asyncHttpClient;
}
 
Example 9
Source File: SaltClientTest.java    From salt-netapi-client with MIT License 5 votes vote down vote up
@Test
public void testRunRequestWithSocketTimeout() {
    exception.expect(CompletionException.class);
    exception.expectCause(instanceOf(SocketTimeoutException.class));

    RequestConfig requestConfig = RequestConfig.custom()
            .setSocketTimeout(1000)
            .setCookieSpec(CookieSpecs.STANDARD)
            .build();
    HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom();
    httpClientBuilder.setDefaultRequestConfig(requestConfig);

    CloseableHttpAsyncClient asyncHttpClient = httpClientBuilder.build();
    asyncHttpClient.start();

    // create a local SaltClient with a fast timeout configuration
    // to do not lock tests more than 2s
    URI uri = URI.create("http://localhost:" + Integer.toString(MOCK_HTTP_PORT));
    SaltClient clientWithFastTimeout = new SaltClient(uri, new HttpAsyncClientImpl(asyncHttpClient));


    stubFor(any(urlMatching(".*"))
            .willReturn(aResponse()
            .withFixedDelay(2000)));

    clientWithFastTimeout.login("user", "pass", AUTO).toCompletableFuture().join();
}
 
Example 10
Source File: SiteToSiteRestApiClient.java    From nifi with Apache License 2.0 5 votes vote down vote up
private void setupAsyncClient() {
    final HttpAsyncClientBuilder clientBuilder = HttpAsyncClients.custom();

    if (sslContext != null) {
        clientBuilder.setSSLContext(sslContext);
        clientBuilder.addInterceptorFirst(new HttpsResponseInterceptor());
    }

    httpAsyncClient = clientBuilder.setDefaultCredentialsProvider(getCredentialsProvider()).build();
    httpAsyncClient.start();
}
 
Example 11
Source File: HttpClientFactory.java    From aliyun-tsdb-java-sdk with Apache License 2.0 4 votes vote down vote up
private static CloseableHttpAsyncClient createPoolingHttpClient(
        Config config, PoolingNHttpClientConnectionManager cm) throws HttpClientInitException {
    int httpConnectionPool = config.getHttpConnectionPool();
    int httpConnectionLiveTime = config.getHttpConnectionLiveTime();
    int httpKeepaliveTime = config.getHttpKeepaliveTime();

    RequestConfig requestConfig = initRequestConfig(config);

    if (httpConnectionPool > 0) {
        cm.setMaxTotal(httpConnectionPool);
        cm.setDefaultMaxPerRoute(httpConnectionPool);
        cm.closeExpiredConnections();
    }

    HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom();

    // 设置连接管理器
    httpAsyncClientBuilder.setConnectionManager(cm);

    // 设置RequestConfig
    if (requestConfig != null) {
        httpAsyncClientBuilder.setDefaultRequestConfig(requestConfig);
    }

    // 设置Keepalive
    if (httpKeepaliveTime > 0) {
        HiTSDBConnectionKeepAliveStrategy hiTSDBConnectionKeepAliveStrategy = new HiTSDBConnectionKeepAliveStrategy(httpConnectionLiveTime);
        httpAsyncClientBuilder.setKeepAliveStrategy(hiTSDBConnectionKeepAliveStrategy);
    } else if (httpKeepaliveTime == 0) {
        HiTSDBConnectionReuseStrategy hiTSDBConnectionReuseStrategy = new HiTSDBConnectionReuseStrategy();
        httpAsyncClientBuilder.setConnectionReuseStrategy(hiTSDBConnectionReuseStrategy);
    }

    // 设置连接自动关闭
    if (httpConnectionLiveTime > 0) {
        TSDBHttpAsyncCallbackExecutor httpAsyncCallbackExecutor = new TSDBHttpAsyncCallbackExecutor(httpConnectionLiveTime);
        httpAsyncClientBuilder.setEventHandler(httpAsyncCallbackExecutor);
    }

    CloseableHttpAsyncClient client = httpAsyncClientBuilder.build();
    return client;
}