use of core.framework.impl.http.HTTPClientImpl in project core-ng-project by neowu.
the class HTTPClientBuilder method build.
public HTTPClient build() {
StopWatch watch = new StopWatch();
try {
HttpClientBuilder builder = HttpClients.custom();
builder.setUserAgent(userAgent);
builder.setKeepAliveStrategy((response, context) -> keepAliveTimeout.toMillis());
builder.setSSLHostnameVerifier(NoopHostnameVerifier.INSTANCE).setSSLContext(new SSLContextBuilder().loadTrustMaterial(TrustSelfSignedStrategy.INSTANCE).build());
// builder use PoolingHttpClientConnectionManager by default, and connTimeToLive will be set by keepAlive value
builder.setDefaultSocketConfig(SocketConfig.custom().setSoKeepAlive(true).build());
builder.setDefaultRequestConfig(RequestConfig.custom().setSocketTimeout((int) timeout.toMillis()).setConnectionRequestTimeout((int) timeout.toMillis()).setConnectTimeout((int) timeout.toMillis()).build());
builder.setMaxConnPerRoute(maxConnections).setMaxConnTotal(maxConnections);
builder.disableAuthCaching();
builder.disableConnectionState();
// retry should be handled in framework level with better trace log
builder.disableAutomaticRetries();
if (!enableRedirect)
builder.disableRedirectHandling();
if (!enableCookie)
builder.disableCookieManagement();
CloseableHttpClient httpClient = builder.build();
return new HTTPClientImpl(httpClient, userAgent, slowOperationThreshold);
} catch (NoSuchAlgorithmException | KeyManagementException | KeyStoreException e) {
throw new Error(e);
} finally {
logger.info("create http client, elapsedTime={}", watch.elapsedTime());
}
}
Aggregations