use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project ovirt-engine by oVirt.
the class HttpClientBuilder method build.
public CloseableHttpClient build() throws IOException, GeneralSecurityException {
// Prepare the default configuration for all requests:
RequestConfig requestConfig = RequestConfig.custom().setConnectTimeout(connectTimeout != null ? connectTimeout : 0).setSocketTimeout(readTimeout != null ? readTimeout : 0).build();
// Configure the trust manager:
TrustManager[] trustManager = null;
if (verifyChain) {
if (trustStore != null) {
try (InputStream is = new FileInputStream(trustStore)) {
KeyStore ks = KeyStore.getInstance(trustStoreType);
ks.load(is, StringUtils.isEmpty(trustStorePassword) ? null : trustStorePassword.toCharArray());
TrustManagerFactory tmf = TrustManagerFactory.getInstance(trustManagerAlgorithm);
tmf.init(ks);
trustManager = tmf.getTrustManagers();
}
}
} else {
trustManager = new TrustManager[] { new X509TrustManager() {
public X509Certificate[] getAcceptedIssuers() {
return new X509Certificate[] {};
}
public void checkClientTrusted(X509Certificate[] certs, String authType) {
}
public void checkServerTrusted(X509Certificate[] certs, String authType) {
}
} };
}
// Create the SSL context:
SSLContext sslContext = SSLContext.getInstance(tlsProtocol);
sslContext.init(null, trustManager, null);
// Create the SSL host name verifier:
HostnameVerifier sslHostnameVerifier = null;
if (!verifyHost) {
sslHostnameVerifier = (hostname, session) -> true;
}
// Create the socket factory for HTTP:
ConnectionSocketFactory httpSocketFactory = new PlainConnectionSocketFactory();
// Create the socket factory for HTTPS:
ConnectionSocketFactory httpsSocketFactory = new SSLConnectionSocketFactory(sslContext, sslHostnameVerifier);
// Create the socket factory registry:
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", httpSocketFactory).register("https", httpsSocketFactory).build();
// Create the connection manager:
HttpClientConnectionManager connectionManager;
if (poolSize != null) {
PoolingHttpClientConnectionManager poolManager = new PoolingHttpClientConnectionManager(socketFactoryRegistry);
poolManager.setDefaultMaxPerRoute(poolSize);
poolManager.setMaxTotal(poolSize);
poolManager.setValidateAfterInactivity(validateAfterInactivity == null ? 100 : validateAfterInactivity);
connectionManager = poolManager;
} else {
connectionManager = new BasicHttpClientConnectionManager(socketFactoryRegistry);
}
// Create the client:
return org.apache.http.impl.client.HttpClientBuilder.create().setDefaultRequestConfig(requestConfig).setSSLHostnameVerifier(sslHostnameVerifier).setConnectionManager(connectionManager).setRetryHandler(new StandardHttpRequestRetryHandler(retryCount == null ? 1 : retryCount, true)).build();
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project gateway-dubbox by zhuzhong.
the class OpenApiHttpClientServiceImpl method initHttpClient.
private void initHttpClient() {
// 注册访问协议相关的socket工厂
Registry<ConnectionSocketFactory> socketFactoryRegistry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.INSTANCE).register("https", SSLConnectionSocketFactory.getSystemSocketFactory()).build();
// httpclient 工厂
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(DefaultHttpRequestWriterFactory.INSTANCE, DefaultHttpResponseParserFactory.INSTANCE);
// dns解析器
DnsResolver dnsResolver = SystemDefaultDnsResolver.INSTANCE;
// 创建池化连接管理器
manager = new PoolingHttpClientConnectionManager(socketFactoryRegistry, connFactory, dnsResolver);
// 默认socket配置
SocketConfig defaultSocketConfig = SocketConfig.custom().setTcpNoDelay(true).build();
manager.setDefaultSocketConfig(defaultSocketConfig);
// 设置整个连接池的最大连接数
manager.setMaxTotal(this.maxTotal);
// 每个路由最大连接数
manager.setDefaultMaxPerRoute(this.defaultMaxPerRoute);
manager.setValidateAfterInactivity(this.validateAfterInactivity);
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectTimeout(this.connectionTimeout).setSocketTimeout(this.socketTimeout).setConnectionRequestTimeout(this.connectionRequestTimeout).build();
httpClient = HttpClients.custom().setConnectionManager(manager).setConnectionManagerShared(false).evictIdleConnections(60, TimeUnit.SECONDS).evictExpiredConnections().setConnectionTimeToLive(60, TimeUnit.SECONDS).setDefaultRequestConfig(defaultRequestConfig).setConnectionReuseStrategy(DefaultConnectionReuseStrategy.INSTANCE).setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE).setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)).build();
Runtime.getRuntime().addShutdownHook(new Thread() {
@Override
public void run() {
try {
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project validator by validator.
the class PrudentHttpEntityResolver method setParams.
/**
* Sets the timeouts of the HTTP client.
*
* @param connectionTimeout
* timeout until connection established in milliseconds. Zero
* means no timeout.
* @param socketTimeout
* timeout for waiting for data in milliseconds. Zero means no
* timeout.
* @param maxRequests
* maximum number of connections to a particular host
*/
public static void setParams(int connectionTimeout, int socketTimeout, int maxRequests) {
PrudentHttpEntityResolver.maxRequests = maxRequests;
PoolingHttpClientConnectionManager phcConnMgr;
//
Registry<ConnectionSocketFactory> registry = //
RegistryBuilder.<ConnectionSocketFactory>create().register("http", //
PlainConnectionSocketFactory.getSocketFactory()).register("https", //
SSLConnectionSocketFactory.getSocketFactory()).build();
HttpClientBuilder builder = HttpClients.custom().useSystemProperties();
builder.setRedirectStrategy(new LaxRedirectStrategy());
builder.setMaxConnPerRoute(maxRequests);
builder.setMaxConnTotal(Integer.parseInt(System.getProperty("nu.validator.servlet.max-total-connections", "200")));
if ("true".equals(System.getProperty("nu.validator.xml.promiscuous-ssl", "true"))) {
//
try {
SSLContext promiscuousSSLContext = //
new SSLContextBuilder().loadTrustMaterial(null, new TrustStrategy() {
@Override
public boolean isTrusted(X509Certificate[] arg0, String arg1) throws CertificateException {
return true;
}
}).build();
builder.setSslcontext(promiscuousSSLContext);
//
HostnameVerifier verifier = SSLConnectionSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER;
//
SSLConnectionSocketFactory promiscuousSSLConnSocketFactory = new SSLConnectionSocketFactory(promiscuousSSLContext, verifier);
registry = //
RegistryBuilder.<ConnectionSocketFactory>create().register("https", //
promiscuousSSLConnSocketFactory).register("http", //
PlainConnectionSocketFactory.getSocketFactory()).build();
} catch (KeyManagementException | KeyStoreException | NoSuchAlgorithmException | NumberFormatException e) {
e.printStackTrace();
}
}
phcConnMgr = new PoolingHttpClientConnectionManager(registry);
phcConnMgr.setDefaultMaxPerRoute(maxRequests);
phcConnMgr.setMaxTotal(200);
builder.setConnectionManager(phcConnMgr);
RequestConfig.Builder config = RequestConfig.custom();
config.setCircularRedirectsAllowed(true);
config.setMaxRedirects(Integer.parseInt(System.getProperty("nu.validator.servlet.max-redirects", "20")));
config.setConnectTimeout(connectionTimeout);
config.setCookieSpec(CookieSpecs.BEST_MATCH);
config.setSocketTimeout(socketTimeout);
config.setCookieSpec(CookieSpecs.IGNORE_COOKIES);
client = builder.setDefaultRequestConfig(config.build()).build();
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project opacclient by opacapp.
the class HttpClientFactory method getNewApacheHttpClient.
/**
* Create a new HttpClient.
*
* @param tls_only If this is true, only TLS v1 and newer will be used, SSLv3 will be disabled.
* We highly recommend to set this to true, if possible. This is currently a
* no-op on the default implementation and only used in the Android
* implementation!
*/
public HttpClient getNewApacheHttpClient(boolean customssl, boolean tls_only, boolean allCipherSuites, boolean disguise_app) {
HttpClientBuilder builder = HttpClientBuilder.create();
builder.setRedirectStrategy(new CustomRedirectStrategy());
if (disguise_app) {
builder.setUserAgent("Mozilla/5.0 (Windows NT 6.1; WOW64) AppleWebKit/537.36 (KHTML, " + "like Gecko) Chrome/43.0.2357.130 Safari/537.36\t");
} else {
builder.setUserAgent(user_agent);
}
if (customssl && ssl_store_path != null) {
try {
if (trust_store == null) {
trust_store = getKeyStore();
}
SSLConnectionSocketFactory sf = AdditionalKeyStoresSSLSocketFactory.create(getSocketFactoryClass(tls_only, allCipherSuites), new AdditionalKeyStoresSSLSocketFactory.AdditionalKeyStoresTrustManager(trust_store));
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create().register("http", PlainConnectionSocketFactory.getSocketFactory()).register("https", sf).build();
HttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(registry);
builder.setConnectionManager(ccm);
return builder.build();
} catch (Exception e) {
e.printStackTrace();
return builder.build();
}
} else {
return builder.build();
}
}
use of org.apache.http.impl.conn.PoolingHttpClientConnectionManager in project camunda-bpm-platform by camunda.
the class RestJaxRs2IT method shouldPerform500ConcurrentRequests.
@Test
public void shouldPerform500ConcurrentRequests() throws InterruptedException, ExecutionException {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
final CloseableHttpClient httpClient = HttpClients.custom().setConnectionManager(cm).build();
Callable<String> performRequest = new Callable<String>() {
@Override
public String call() throws IOException {
HttpPost request = new HttpPost(APP_BASE_PATH + FETCH_AND_LOCK_PATH);
request.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
StringEntity stringEntity = new StringEntity("{ \"workerId\": \"aWorkerId\", \"asyncResponseTimeout\": 1000 }");
request.setEntity(stringEntity);
CloseableHttpResponse response = httpClient.execute(request, HttpClientContext.create());
String responseBody = null;
try {
HttpEntity entity = response.getEntity();
responseBody = EntityUtils.toString(entity);
request.releaseConnection();
} finally {
response.close();
}
return responseBody;
}
};
int requestsCount = 500;
ExecutorService service = Executors.newFixedThreadPool(requestsCount);
List<Callable<String>> requests = new ArrayList<Callable<String>>();
for (int i = 0; i < requestsCount; i++) {
requests.add(performRequest);
}
List<Future<String>> futures = service.invokeAll(requests);
service.shutdown();
service.awaitTermination(1, TimeUnit.HOURS);
for (Future<String> future : futures) {
assertEquals(future.get(), "[]");
}
}
Aggregations