本文整理汇总了Java中org.apache.http.impl.conn.PoolingHttpClientConnectionManager.setDefaultConnectionConfig方法的典型用法代码示例。如果您正苦于以下问题:Java PoolingHttpClientConnectionManager.setDefaultConnectionConfig方法的具体用法?Java PoolingHttpClientConnectionManager.setDefaultConnectionConfig怎么用?Java PoolingHttpClientConnectionManager.setDefaultConnectionConfig使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.conn.PoolingHttpClientConnectionManager
的用法示例。
在下文中一共展示了PoolingHttpClientConnectionManager.setDefaultConnectionConfig方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: create
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
@Override
public HttpClientConnectionManager create(final HttpClientSettings settings) {
ConnectionSocketFactory sslsf = getPreferredSocketFactory(settings);
final PoolingHttpClientConnectionManager cm = new
PoolingHttpClientConnectionManager(
createSocketFactoryRegistry(sslsf),
null,
DefaultSchemePortResolver.INSTANCE,
new DelegatingDnsResolver(settings.getDnsResolver()),
settings.getConnectionPoolTTL(),
TimeUnit.MILLISECONDS);
cm.setValidateAfterInactivity(settings.getValidateAfterInactivityMillis());
cm.setDefaultMaxPerRoute(settings.getMaxConnections());
cm.setMaxTotal(settings.getMaxConnections());
cm.setDefaultSocketConfig(buildSocketConfig(settings));
cm.setDefaultConnectionConfig(buildConnectionConfig(settings));
return cm;
}
示例2: client
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private static CloseableHttpClient client() {
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
cm.setDefaultMaxPerRoute(100);
cm.setMaxTotal(400);
MessageConstraints messageConstraints = MessageConstraints.custom()
.setMaxHeaderCount(200)
.setMaxLineLength(2000)
.build();
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.setCharset(Consts.UTF_8)
.setMessageConstraints(messageConstraints)
.build();
RequestConfig defaultRequestConfig = RequestConfig.custom()
.setSocketTimeout(5000)
.setConnectTimeout(5000)
.setConnectionRequestTimeout(5000)
.build();
cm.setDefaultConnectionConfig(connectionConfig);
return HttpClients.custom()
.setConnectionManager(cm)
.setDefaultRequestConfig(defaultRequestConfig)
.build();
}
示例3: createHttpClientConnPool
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* 创建 HttpClient 连接池.
*
* @param connectionConfig
* @return
*/
private PoolingHttpClientConnectionManager createHttpClientConnPool(ConnectionConfig connectionConfig) {
PoolingHttpClientConnectionManager httpClientConnectionManager = new PoolingHttpClientConnectionManager(
RegistryBuilder.<ConnectionSocketFactory>create().
register("http", PlainConnectionSocketFactory.getSocketFactory()).
register("https", buildSSLConn()).build());
httpClientConnectionManager.setMaxTotal(MAX_TOTAL); // 设置连接池线程最大数量
httpClientConnectionManager.setDefaultMaxPerRoute(MAX_ROUTE_TOTAL); // 设置单个路由最大的连接线程数量
httpClientConnectionManager.setDefaultConnectionConfig(connectionConfig);
return httpClientConnectionManager;
}
示例4: createHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private CloseableHttpClient createHttpClient() throws NSPException {
try {
initSocketFactory();
DnsResolver dnsResolver = new SystemDefaultDnsResolver() {
public InetAddress[] resolve(String host)
throws UnknownHostException {
if (host.equalsIgnoreCase("localhost")) {
return new InetAddress[] { InetAddress
.getByAddress(new byte[] { 127, 0, 0, 1 }) };
}
return super.resolve(host);
}
};
PoolingHttpClientConnectionManager connManager = new PoolingHttpClientConnectionManager(
this.socketFactoryRegistry, dnsResolver);
SocketConfig socketConfig = SocketConfig.custom()
.setTcpNoDelay(true).setSoKeepAlive(true).setSoLinger(0)
.setSoReuseAddress(true).build();
connManager.setDefaultSocketConfig(socketConfig);
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setCharset(Consts.UTF_8).build();
connManager.setDefaultConnectionConfig(connectionConfig);
connManager.setConnectionConfig(new HttpHost("localhost", 80),
ConnectionConfig.DEFAULT);
connManager.setMaxTotal(this.mMaxConnections);
if ((this.mMaxConnections <= this.mConnectionsPerRoute)
&& (this.mMaxConnections > 0)) {
this.mConnectionsPerRoute = this.mMaxConnections;
}
connManager.setDefaultMaxPerRoute(this.mConnectionsPerRoute);
connManager.setMaxPerRoute(new HttpRoute(new HttpHost("localhost",
80)), 100);
CloseableHttpClient httpclient = HttpClients
.custom()
.setConnectionManager(connManager)
.setDefaultRequestConfig(
RequestConfig.copy(defaultRequestConfig).build())
.build();
return ((CloseableHttpClient) new WeakReference(httpclient).get());
} catch (Exception e) {
throw new NSPException(2, "Service unavailable.", e);
}
}
示例5: buildClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
@Override
protected ExecREST buildClient(RESTPool pool) throws IOException {
SSLContext sslContext;
try {
sslContext = SSLContext.getDefault();
} catch (NoSuchAlgorithmException e) {
throw new IOException(e);
}
Registry<ConnectionSocketFactory> socketRegistry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", PlainConnectionSocketFactory.INSTANCE)
.register("https", new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE))
.build();
//TODO buffers size
SocketConfig socketConfig = SocketConfig.custom()
.setSoTimeout(new Long(pool.getSocketTimeout()).intValue())
.setTcpNoDelay(true)
.setSoKeepAlive(true)
.setSoReuseAddress(true)
.build();
ConnectionConfig connectionConfig = ConnectionConfig.custom()
.setCharset(StandardCharsets.UTF_8)
.setMalformedInputAction(CodingErrorAction.IGNORE)
.setUnmappableInputAction(CodingErrorAction.IGNORE)
.build();
HttpConnectionFactory<HttpRoute, ManagedHttpClientConnection> connFactory = new ManagedHttpClientConnectionFactory(
new DefaultHttpRequestWriterFactory(),
new DefaultHttpResponseParserFactory()
);
RequestConfig requestConfig = RequestConfig.custom()
.setConnectionRequestTimeout(new Long(pool.getMaxPoolWait()).intValue())
.setConnectTimeout(new Long(pool.getConnectionTimeout()).intValue())
.setExpectContinueEnabled(pool.expectContinue())
.build();
PoolingHttpClientConnectionManager ccm = new PoolingHttpClientConnectionManager(socketRegistry, connFactory);
ccm.setMaxTotal(pool.getMaxTotal());
ccm.setDefaultMaxPerRoute(pool.getMaxPerRoute());
ccm.setDefaultSocketConfig(socketConfig);
ccm.setDefaultConnectionConfig(connectionConfig);
ccm.setValidateAfterInactivity(pool.getValidationOnInactivity());
HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(ccm)
.setDefaultRequestConfig(requestConfig)
.disableAutomaticRetries()
.setKeepAliveStrategy(DefaultConnectionKeepAliveStrategy.INSTANCE)
.disableCookieManagement()
.disableContentCompression();
addProxy(pool, builder);
handleRedirects(pool, builder);
CloseableHttpClient servClient = builder.build();
IdleConnectionEvictor evictor = new IdleConnectionEvictor(ccm, pool.getEvictorSleep(), TimeUnit.MILLISECONDS, pool.getMaxIdleTime(), TimeUnit.MILLISECONDS);
HTTPCClientMonitor monitor = pool.hasConnectionMetrics() ? new HTTPCSyncClientMonitor(pool.getName(), ccm) : null;
return new HTTPCClient(servClient, evictor, monitor);
}