use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project fabric-sdk-java by hyperledger.
the class HFCAClient method httpGet.
JsonObject httpGet(String url, User registrar) throws Exception {
String authHTTPCert = getHTTPAuthCertificate(registrar.getEnrollment(), "");
url = getURL(url);
HttpGet httpGet = new HttpGet(url);
logger.debug(format("httpGet %s, authHTTPCert: %s", url, authHTTPCert));
final HttpClientBuilder httpClientBuilder = HttpClientBuilder.create();
if (registry != null) {
httpClientBuilder.setConnectionManager(new PoolingHttpClientConnectionManager(registry));
}
HttpClient client = httpClientBuilder.build();
final HttpClientContext context = HttpClientContext.create();
httpGet.addHeader("Authorization", authHTTPCert);
HttpResponse response = client.execute(httpGet, context);
return getResult(response, "", "GET");
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project cloudbreak by hortonworks.
the class ConsulUtils method createHttpClient.
private static HttpClient createHttpClient(String clientCert, String clientKey, String serverCert) throws Exception {
SSLContext sslContext = org.apache.http.ssl.SSLContexts.custom().loadTrustMaterial(KeyStoreUtil.createTrustStore(serverCert), null).loadKeyMaterial(KeyStoreUtil.createKeyStore(clientCert, clientKey), "consul".toCharArray()).build();
try (PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(setupSchemeRegistry(sslContext))) {
connectionManager.setMaxTotal(MAX_CONNECTION);
connectionManager.setDefaultMaxPerRoute(MAX_ROUTE);
Builder requestBuilder = RequestConfig.custom().setConnectTimeout(DEFAULT_TIMEOUT_MS).setConnectionRequestTimeout(DEFAULT_TIMEOUT_MS).setSocketTimeout(DEFAULT_TIMEOUT_MS);
return HttpClientBuilder.create().setConnectionManager(connectionManager).setDefaultRequestConfig(requestBuilder.build()).build();
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project scout.rt by eclipse.
the class HttpServiceTunnelTransportManager method interceptNewHttpTransport.
@Override
public void interceptNewHttpTransport(IHttpTransportBuilder builder0) {
super.interceptNewHttpTransport(builder0);
if (builder0 instanceof ApacheHttpTransportBuilder) {
ApacheHttpTransportBuilder builder = (ApacheHttpTransportBuilder) builder0;
if (builder.getConnectionManager() != null && builder.getConnectionManager() instanceof PoolingHttpClientConnectionManager) {
@SuppressWarnings("resource") PoolingHttpClientConnectionManager cm = (PoolingHttpClientConnectionManager) builder.getConnectionManager();
cm.setDefaultMaxPerRoute(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsPerRouteProperty.class));
cm.setMaxTotal(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsTotalProperty.class));
} else {
builder.getBuilder().setMaxConnPerRoute(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsPerRouteProperty.class));
builder.getBuilder().setMaxConnTotal(CONFIG.getPropertyValue(HttpServiceTunnelTransportMaxConnectionsTotalProperty.class));
}
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project LogHub by fbacchella.
the class AbstractHttpSender method configure.
@Override
public boolean configure(Properties properties) {
endPoints = Helpers.stringsToUrl(destinations, port, protocol, logger);
if (endPoints.length == 0) {
return false;
}
// Create the senders threads and the common queue
batch = new Batch();
threads = new Thread[publisherThreads];
for (int i = 1; i <= publisherThreads; i++) {
String tname = getPublishName() + "Publisher" + i;
threads[i - 1] = new Thread(publisher) {
{
setDaemon(false);
setName(tname);
start();
}
};
}
// The HTTP connection management
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setUserAgent(VersionInfo.getUserAgent("LogHub-HttpClient", "org.apache.http.client", HttpClientBuilder.class));
// Set the Configuration manager
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(properties.ssl)).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setDefaultMaxPerRoute(2);
cm.setMaxTotal(2 * publisherThreads);
cm.setValidateAfterInactivity(timeout * 1000);
builder.setConnectionManager(cm);
if (properties.ssl != null) {
builder.setSSLContext(properties.ssl);
}
builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000).setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build());
builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoTimeout(timeout * 1000).build());
builder.setDefaultConnectionConfig(ConnectionConfig.custom().build());
builder.disableCookieManagement();
builder.setRetryHandler(new HttpRequestRetryHandler() {
@Override
public boolean retryRequest(IOException exception, int executionCount, HttpContext context) {
return false;
}
});
client = builder.build();
if (user != null && password != null) {
credsProvider = new BasicCredentialsProvider();
for (URL i : endPoints) {
credsProvider.setCredentials(new AuthScope(i.getHost(), i.getPort()), new UsernamePasswordCredentials(user, password));
}
}
// Schedule a task to flush every 5 seconds
Runnable flush = () -> {
synchronized (publisher) {
long now = new Date().getTime();
if ((now - lastFlush) > 5000) {
batches.add(batch);
batch = new Batch();
publisher.notify();
}
}
};
properties.registerScheduledTask(getPublishName() + "Flusher", flush, 5000);
return true;
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project LogHub by fbacchella.
the class TestHttpSsl method TestHcComplex.
@Test
public void TestHcComplex() throws NoSuchAlgorithmException, KeyStoreException, CertificateException, IOException, KeyManagementException {
CloseableHttpClient client = null;
;
try {
int publisherThreads = 1;
int timeout = 2;
// The HTTP connection management
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setUserAgent(VersionInfo.getUserAgent("LogHub-HttpClient", "org.apache.http.client", HttpClientBuilder.class));
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", new SSLConnectionSocketFactory(getContext.get(), localhostVerifier)).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
cm.setDefaultMaxPerRoute(2);
cm.setMaxTotal(2 * publisherThreads);
cm.setValidateAfterInactivity(timeout * 1000);
builder.setConnectionManager(cm);
builder.setDefaultRequestConfig(RequestConfig.custom().setConnectionRequestTimeout(timeout * 1000).setConnectTimeout(timeout * 1000).setSocketTimeout(timeout * 1000).build());
builder.setDefaultSocketConfig(SocketConfig.custom().setTcpNoDelay(true).setSoKeepAlive(true).setSoTimeout(timeout * 1000).build());
builder.setDefaultConnectionConfig(ConnectionConfig.custom().setCharset(Charset.forName("UTF-8")).build());
builder.disableCookieManagement();
client = builder.build();
HttpClientContext context = HttpClientContext.create();
HttpHost host;
RequestLine requestLine = new BasicRequestLine("GET", theurl.getPath(), HttpVersion.HTTP_1_1);
BasicHttpEntityEnclosingRequest request = new BasicHttpEntityEnclosingRequest(requestLine);
host = new HttpHost(theurl.getHost(), theurl.getPort(), "https");
CloseableHttpResponse response = client.execute(host, request, context);
Assert.assertEquals(200, response.getStatusLine().getStatusCode());
try {
HttpEntity entity = response.getEntity();
EntityUtils.consume(entity);
} finally {
response.close();
}
} finally {
if (client != null) {
client.close();
}
}
}
Aggregations