Java Code Examples for org.apache.http.impl.nio.client.HttpAsyncClientBuilder#build()
The following examples show how to use
org.apache.http.impl.nio.client.HttpAsyncClientBuilder#build() .
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: HttpProxyClient.java From zeppelin with Apache License 2.0 | 6 votes |
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 2
Source File: PiwikTracker.java From matomo-java-tracker with BSD 3-Clause "New" or "Revised" License | 6 votes |
/** * Get an async HTTP client. With proxy if a proxy is provided in the constructor. * @return an async HTTP client */ protected CloseableHttpAsyncClient getHttpAsyncClient(){ HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create(); if(proxyHost != null && proxyPort != 0) { HttpHost proxy = new HttpHost(proxyHost, proxyPort); DefaultProxyRoutePlanner routePlanner = new DefaultProxyRoutePlanner(proxy); builder.setRoutePlanner(routePlanner); } RequestConfig.Builder config = RequestConfig.custom() .setConnectTimeout(timeout) .setConnectionRequestTimeout(timeout) .setSocketTimeout(timeout); builder.setDefaultRequestConfig(config.build()); return builder.build(); }
Example 3
Source File: ApacheHttpAsyncClient.java From incubator-gobblin with Apache License 2.0 | 6 votes |
public ApacheHttpAsyncClient(HttpAsyncClientBuilder builder, Config config, SharedResourcesBroker<GobblinScopeTypes> broker) { super (broker, HttpUtils.createApacheHttpClientLimiterKey(config)); config = config.withFallback(FALLBACK); RequestConfig requestConfig = RequestConfig.copy(RequestConfig.DEFAULT) .setSocketTimeout(config.getInt(REQUEST_TIME_OUT_MS_KEY)) .setConnectTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .setConnectionRequestTimeout(config.getInt(CONNECTION_TIME_OUT_MS_KEY)) .build(); try { builder.disableCookieManagement().useSystemProperties().setDefaultRequestConfig(requestConfig); builder.setConnectionManager(getNHttpConnManager(config)); client = builder.build(); client.start(); } catch (IOException e) { throw new RuntimeException("ApacheHttpAsyncClient cannot be initialized"); } }
Example 4
Source File: AsyncHttpClientGenerator.java From cetty with Apache License 2.0 | 5 votes |
@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: HttpClientFactory.java From super-cloudops with Apache License 2.0 | 5 votes |
/*** * 创建client * * @param config * 查询对象 * @param cm * 连接池管理 * @param openTSDBConfig * @return */ private static CloseableHttpAsyncClient createPoolingHttpClient(RequestConfig config, PoolingNHttpClientConnectionManager cm, OpenTSDBConfig openTSDBConfig) { cm.setMaxTotal(100); cm.setDefaultMaxPerRoute(100); HttpAsyncClientBuilder httpAsyncClientBuilder = HttpAsyncClients.custom().setConnectionManager(cm) .setDefaultRequestConfig(config); // 如果不是只读,则设置为长连接 if (!openTSDBConfig.isReadonly()) { httpAsyncClientBuilder.setKeepAliveStrategy(myStrategy()); } CloseableHttpAsyncClient client = httpAsyncClientBuilder.build(); return client; }
Example 6
Source File: AsyncInternalClient.java From fc-java-sdk with MIT License | 5 votes |
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 7
Source File: PoolingHttpAsyncClientFactory.java From caravan with Apache License 2.0 | 5 votes |
public static CloseableHttpAsyncClient create(RequestConfig config, NHttpClientConnectionManager connectionManager) { HttpAsyncClientBuilder builder = HttpAsyncClientBuilder.create(); builder.useSystemProperties().setRedirectStrategy(AlwaysRedirectStrategy.DEFAULT).addInterceptorLast(new RequestContent(true)); if (config != null) builder.setDefaultRequestConfig(config); if (connectionManager != null) builder.setConnectionManager(connectionManager); return builder.build(); }
Example 8
Source File: HttpFactory.java From aliyun-tablestore-java-sdk with Apache License 2.0 | 5 votes |
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 9
Source File: ApacheHttpRequester.java From algoliasearch-client-java-2 with MIT License | 5 votes |
public ApacheHttpRequester(@Nonnull ConfigBase config, @Nonnull HttpAsyncClientBuilder builder) { this.config = config; this.requestConfig = RequestConfig.custom() .setConnectTimeout(config.getConnectTimeOut()) .setContentCompressionEnabled(true) .build(); this.asyncHttpClient = builder.build(); this.asyncHttpClient.start(); }
Example 10
Source File: HttpClientUtils.java From salt-netapi-client with MIT License | 5 votes |
/** * 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 11
Source File: TestUtils.java From salt-netapi-client with MIT License | 5 votes |
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 12
Source File: SaltClientTest.java From salt-netapi-client with MIT License | 5 votes |
@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 13
Source File: BceHttpClient.java From bce-sdk-java with Apache License 2.0 | 5 votes |
/** * Create asynchronous http client based on connection manager. * * @param connectionManager Asynchronous http client connection manager. * @return Asynchronous http client based on connection manager. */ protected CloseableHttpAsyncClient createHttpAsyncClient(NHttpClientConnectionManager connectionManager) { HttpAsyncClientBuilder builder = HttpAsyncClients.custom().setConnectionManager(connectionManager); int socketBufferSizeInBytes = this.config.getSocketBufferSizeInBytes(); if (socketBufferSizeInBytes > 0) { builder.setDefaultConnectionConfig( ConnectionConfig.custom().setBufferSize(socketBufferSizeInBytes).build()); } return builder.build(); }
Example 14
Source File: HttpFactory.java From mq-http-java-sdk with MIT License | 4 votes |
public static CloseableHttpAsyncClient createHttpAsyncClient( PoolingNHttpClientConnectionManager connManager, ClientConfiguration config) { HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom() .setConnectionManager(connManager); // Set proxy if set. String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { 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 credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxy), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain) ); httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider); } } RequestConfig defaultRequestConfig = RequestConfig .custom() .setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true) .setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes( Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .setConnectTimeout(config.getConnectionTimeout()) .setSocketTimeout(config.getSocketTimeout()) .setExpectContinueEnabled(config.isExceptContinue()).build(); httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig); httpClientBuilder .setMaxConnPerRoute(config.getMaxConnectionsPerRoute()); httpClientBuilder.setMaxConnTotal(config.getMaxConnections()); httpClientBuilder.setUserAgent(VersionInfoUtils.getDefaultUserAgent()); CloseableHttpAsyncClient httpclient = httpClientBuilder.build(); return httpclient; }
Example 15
Source File: HttpFactory.java From mq-http-java-sdk with MIT License | 4 votes |
public static CloseableHttpAsyncClient createHttpAsyncClient( PoolingNHttpClientConnectionManager connManager, ClientConfiguration config) { HttpAsyncClientBuilder httpClientBuilder = HttpAsyncClients.custom() .setConnectionManager(connManager); // Set proxy if set. String proxyHost = config.getProxyHost(); int proxyPort = config.getProxyPort(); if (proxyHost != null && proxyPort > 0) { 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 credentialsProvider = new BasicCredentialsProvider(); credentialsProvider.setCredentials(new AuthScope(proxy), new NTCredentials(proxyUsername, proxyPassword, proxyWorkstation, proxyDomain) ); httpClientBuilder .setDefaultCredentialsProvider(credentialsProvider); } } RequestConfig defaultRequestConfig = RequestConfig .custom() .setCookieSpec(CookieSpecs.BEST_MATCH) .setExpectContinueEnabled(true) .setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes( Arrays.asList(AuthSchemes.NTLM, AuthSchemes.DIGEST)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .setConnectTimeout(config.getConnectionTimeout()) .setSocketTimeout(config.getSocketTimeout()) .setExpectContinueEnabled(config.isExceptContinue()).build(); httpClientBuilder.setDefaultRequestConfig(defaultRequestConfig); httpClientBuilder .setMaxConnPerRoute(config.getMaxConnectionsPerRoute()); httpClientBuilder.setMaxConnTotal(config.getMaxConnections()); httpClientBuilder.setUserAgent(VersionInfoUtils.getDefaultUserAgent()); CloseableHttpAsyncClient httpclient = httpClientBuilder.build(); return httpclient; }
Example 16
Source File: HttpClientFactory.java From aliyun-tsdb-java-sdk with Apache License 2.0 | 4 votes |
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; }