本文整理匯總了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);
}
}