本文整理汇总了Java中org.apache.http.impl.conn.PoolingHttpClientConnectionManager.setMaxPerRoute方法的典型用法代码示例。如果您正苦于以下问题:Java PoolingHttpClientConnectionManager.setMaxPerRoute方法的具体用法?Java PoolingHttpClientConnectionManager.setMaxPerRoute怎么用?Java PoolingHttpClientConnectionManager.setMaxPerRoute使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类org.apache.http.impl.conn.PoolingHttpClientConnectionManager
的用法示例。
在下文中一共展示了PoolingHttpClientConnectionManager.setMaxPerRoute方法的12个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: test
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
public static void test(){
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
// Increase max total connection to 200
cm.setMaxTotal(200);
// Increase default max connection per route to 20
cm.setDefaultMaxPerRoute(20);
// Increase max connections for localhost:80 to 50
HttpHost localhost = new HttpHost("http://cc.0071515.com", 80);
cm.setMaxPerRoute(new HttpRoute(localhost), 2);
// CloseableHttpClient httpClient = HttpClients.custom()
// .setConnectionManager(cm)
// .build();
httpClient = HttpClients.custom()
.setConnectionManager(cm)
.build();
}
示例2: createHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private CloseableHttpClient createHttpClient(String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory> create()
.register("http", plainsf).register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
HttpHost httpHost = new HttpHost(hostname, port);
// 将目标主机的最大连接数增加
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
return HttpClients.custom().setConnectionManager(cm).setRetryHandler(httpRequestRetryHandler).build();
}
示例3: PingCheckMonitor
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* @param timeEntity how often the {@link #monitor()} check needs to be executed
* @param httpRequest http request that will be called at regular intervals
* @param pingTimeoutInMilliseconds timeout in milliseconds for http request execution (ping response)
* @param pingWindowSize rolling window frame, which needs to be maintained
* @param maxFailures maximum failures allowed in the rolling window frame
* @param host host name (could be localhost)
* @param port port
*/
public PingCheckMonitor(TimeEntity timeEntity,
HttpRequest httpRequest,
Integer pingTimeoutInMilliseconds,
Integer pingWindowSize,
Integer maxFailures,
String host,
Integer port) {
super(PingCheckMonitor.class.getSimpleName(), timeEntity);
this.httpRequest = httpRequest;
this.pingTimeoutInMilliseconds = pingTimeoutInMilliseconds;
this.host = host;
this.port = port;
this.rollingWindowHealthQueue = new RollingWindowHealthQueue(pingWindowSize, maxFailures);
this.executorService = Executors.newSingleThreadExecutor();
PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
connectionManager.setMaxPerRoute(new HttpRoute(new HttpHost(host, port)), 2);
this.httpClient = HttpClients.custom()
.setConnectionManager(connectionManager)
.build();
}
示例4: createConnectionManager
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
protected HttpClientConnectionManager createConnectionManager() {
if (httpClientConfig.isMultiThreaded()) {
log.debug("Multi-threaded http connection manager created");
final PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager();
final Integer maxTotal = httpClientConfig.getMaxTotalConnection();
if (maxTotal != null) {
cm.setMaxTotal(maxTotal);
}
final Integer defaultMaxPerRoute = httpClientConfig.getDefaultMaxTotalConnectionPerRoute();
if (defaultMaxPerRoute != null) {
cm.setDefaultMaxPerRoute(defaultMaxPerRoute);
}
final Map<HttpRoute, Integer> maxPerRoute = httpClientConfig.getMaxTotalConnectionPerRoute();
for (final HttpRoute route : maxPerRoute.keySet()) {
cm.setMaxPerRoute(route, maxPerRoute.get(route));
}
return cm;
}
log.debug("Default http connection is created without multi threaded option");
return new BasicHttpClientConnectionManager();
}
示例5: ArgusHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* Creates a new Argus HTTP client.
*
* @param endpoint The URL of the read endpoint including the port number. Must not be null.
* @param maxConn The maximum number of concurrent connections. Must be greater than 0.
* @param timeout The connection timeout in milliseconds. Must be greater than 0.
* @param reqTimeout The connection request timeout in milliseconds. Must be greater than 0.
* @param isPreview Set to true if the collector should skip actually submitting the data.
*
* @throws OrchestraException If an error occurs.
*/
public ArgusHttpClient(String endpoint, int maxConn, int timeout, int reqTimeout, boolean isPreview) {
requireArgument((endpoint != null) && (!endpoint.isEmpty()), "Illegal endpoint URL.");
requireArgument(maxConn >= 2, "At least two connections are required.");
requireArgument(timeout >= 1, "Timeout must be greater than 0.");
requireArgument(reqTimeout >= 1, "Request timeout must be greater than 0.");
try {
preview = isPreview;
if (preview) {
MAPPER.configure(SerializationFeature.INDENT_OUTPUT, true);
}
URL url = new URL(endpoint);
int port = url.getPort();
requireArgument(port != -1, "Endpoint must include explicit port.");
connMgr = new PoolingHttpClientConnectionManager();
connMgr.setMaxTotal(maxConn);
connMgr.setDefaultMaxPerRoute(maxConn);
String routePath = endpoint.substring(0, endpoint.lastIndexOf(':'));
HttpHost host = new HttpHost(routePath, port);
RequestConfig defaultRequestConfig = RequestConfig.custom().setConnectionRequestTimeout(reqTimeout).setConnectTimeout(timeout).build();
connMgr.setMaxPerRoute(new HttpRoute(host), maxConn / 2);
httpClient = HttpClients.custom().setConnectionManager(connMgr).setDefaultRequestConfig(defaultRequestConfig).build();
//cookieStore = new BasicCookieStore();
httpContext = new BasicHttpContext();
//httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
} catch (MalformedURLException ex) {
throw new OrchestraException("Error initializing the Argus HTTP Client.", ex);
}
LOGGER.info("Argus HTTP Client initialized using " + endpoint);
this.endpoint = endpoint;
}
示例6: createHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
private static CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute, String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory
.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
// 将目标主机的最大连接数增加
HttpHost httpHost = new HttpHost(hostname, port);
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = (exception, executionCount, context) -> {
if (executionCount >= 5) {// 如果已经重试了5次,就放弃
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// SSL握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler)
.build();
return httpClient;
}
示例7: createHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
public CloseableHttpClient createHttpClient(int maxTotal, int maxPerRoute, int maxRoute,
String hostname, int port) {
ConnectionSocketFactory plainsf = PlainConnectionSocketFactory
.getSocketFactory();
LayeredConnectionSocketFactory sslsf = SSLConnectionSocketFactory
.getSocketFactory();
Registry<ConnectionSocketFactory> registry = RegistryBuilder
.<ConnectionSocketFactory> create().register("http", plainsf)
.register("https", sslsf).build();
PoolingHttpClientConnectionManager cm = new PoolingHttpClientConnectionManager(
registry);
// 将最大连接数增加
cm.setMaxTotal(maxTotal);
// 将每个路由基础的连接增加
cm.setDefaultMaxPerRoute(maxPerRoute);
HttpHost httpHost = new HttpHost(hostname, port);
// 将目标主机的最大连接数增加
cm.setMaxPerRoute(new HttpRoute(httpHost), maxRoute);
// 请求重试处理
HttpRequestRetryHandler httpRequestRetryHandler = new HttpRequestRetryHandler() {
public boolean retryRequest(IOException exception,
int executionCount, HttpContext context) {
if (executionCount >= _maxRetryTimes) {
return false;
}
if (exception instanceof NoHttpResponseException) {// 如果服务器丢掉了连接,那么就重试
return true;
}
if (exception instanceof SSLHandshakeException) {// 不要重试SSL握手异常
return false;
}
if (exception instanceof InterruptedIOException) {// 超时
return false;
}
if (exception instanceof UnknownHostException) {// 目标服务器不可达
return false;
}
if (exception instanceof ConnectTimeoutException) {// 连接被拒绝
return false;
}
if (exception instanceof SSLException) {// SSL握手异常
return false;
}
HttpClientContext clientContext = HttpClientContext
.adapt(context);
HttpRequest request = clientContext.getRequest();
// 如果请求是幂等的,就再次尝试
if (!(request instanceof HttpEntityEnclosingRequest)) {
return true;
}
return false;
}
};
CloseableHttpClient httpClient = HttpClients.custom()
.setConnectionManager(cm)
.setRetryHandler(httpRequestRetryHandler).build();
return httpClient;
}
示例8: createConnectionMgr
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* create custom Http Client connection pool to be used by Http Client
*/
private void createConnectionMgr() {
connectionMgr = new PoolingHttpClientConnectionManager();
connectionMgr.setMaxTotal(50);
connectionMgr.setDefaultMaxPerRoute(20);
HttpHost localhost = new HttpHost("localhost", 80);
connectionMgr.setMaxPerRoute(new HttpRoute(localhost), 50);
}
示例9: buildHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* Build a HTTP client based on the current properties.
*
* @return the built HTTP client
*/
private CloseableHttpClient buildHttpClient() {
try {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(this.readTimeout)
.setConnectTimeout(this.connectionTimeout)
.setConnectionRequestTimeout(this.connectionTimeout)
.setCircularRedirectsAllowed(this.circularRedirectsAllowed)
.setRedirectsEnabled(this.redirectsEnabled)
.setAuthenticationEnabled(this.authenticationEnabled)
.build();
final HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connMgmr)
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(this.hostnameVerifier)
.setRedirectStrategy(this.redirectionStrategy)
.setDefaultCredentialsProvider(this.credentialsProvider)
.setDefaultCookieStore(this.cookieStore)
.setConnectionReuseStrategy(this.connectionReuseStrategy)
.setConnectionBackoffStrategy(this.connectionBackoffStrategy)
.setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy)
.setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy)
.setDefaultHeaders(this.defaultHeaders)
.useSystemProperties();
return builder.build();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
示例10: buildHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* Build a HTTP client based on the current properties.
*
* @return the built HTTP client
*/
private CloseableHttpClient buildHttpClient() {
try {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(this.readTimeout)
.setConnectTimeout(this.connectionTimeout)
.setConnectionRequestTimeout(this.connectionTimeout)
.setStaleConnectionCheckEnabled(true)
.setCircularRedirectsAllowed(this.circularRedirectsAllowed)
.setRedirectsEnabled(this.redirectsEnabled)
.setAuthenticationEnabled(this.authenticationEnabled)
.build();
final HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connMgmr)
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(this.hostnameVerifier)
.setRedirectStrategy(this.redirectionStrategy)
.setDefaultCredentialsProvider(this.credentialsProvider)
.setDefaultCookieStore(this.cookieStore)
.setConnectionReuseStrategy(this.connectionReuseStrategy)
.setConnectionBackoffStrategy(this.connectionBackoffStrategy)
.setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy)
.setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy)
.setDefaultHeaders(this.defaultHeaders)
.useSystemProperties();
return builder.build();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw new RuntimeException(e);
}
}
示例11: buildHttpClient
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager; //导入方法依赖的package包/类
/**
* Build a HTTP client based on the current properties.
*
* @return the built HTTP client
*/
private CloseableHttpClient buildHttpClient() {
try {
final ConnectionSocketFactory plainsf = PlainConnectionSocketFactory.getSocketFactory();
final LayeredConnectionSocketFactory sslsf = this.sslSocketFactory;
final Registry<ConnectionSocketFactory> registry = RegistryBuilder.<ConnectionSocketFactory>create()
.register("http", plainsf)
.register("https", sslsf)
.build();
final PoolingHttpClientConnectionManager connMgmr = new PoolingHttpClientConnectionManager(registry);
connMgmr.setMaxTotal(this.maxPooledConnections);
connMgmr.setDefaultMaxPerRoute(this.maxConnectionsPerRoute);
connMgmr.setValidateAfterInactivity(DEFAULT_TIMEOUT);
final HttpHost httpHost = new HttpHost(InetAddress.getLocalHost());
final HttpRoute httpRoute = new HttpRoute(httpHost);
connMgmr.setMaxPerRoute(httpRoute, MAX_CONNECTIONS_PER_ROUTE);
final RequestConfig requestConfig = RequestConfig.custom()
.setSocketTimeout(this.readTimeout)
.setConnectTimeout(Long.valueOf(this.connectionTimeout).intValue())
.setConnectionRequestTimeout(Long.valueOf(this.connectionTimeout).intValue())
.setCircularRedirectsAllowed(this.circularRedirectsAllowed)
.setRedirectsEnabled(this.redirectsEnabled)
.setAuthenticationEnabled(this.authenticationEnabled)
.build();
final HttpClientBuilder builder = HttpClients.custom()
.setConnectionManager(connMgmr)
.setDefaultRequestConfig(requestConfig)
.setSSLSocketFactory(sslsf)
.setSSLHostnameVerifier(this.hostnameVerifier)
.setRedirectStrategy(this.redirectionStrategy)
.setDefaultCredentialsProvider(this.credentialsProvider)
.setDefaultCookieStore(this.cookieStore)
.setConnectionReuseStrategy(this.connectionReuseStrategy)
.setConnectionBackoffStrategy(this.connectionBackoffStrategy)
.setServiceUnavailableRetryStrategy(this.serviceUnavailableRetryStrategy)
.setProxyAuthenticationStrategy(this.proxyAuthenticationStrategy)
.setDefaultHeaders(this.defaultHeaders)
.useSystemProperties();
return builder.build();
} catch (final Exception e) {
LOGGER.error(e.getMessage(), e);
throw Throwables.propagate(e);
}
}
示例12: 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);
}
}