List of usage examples for org.apache.http.conn ConnectionKeepAliveStrategy ConnectionKeepAliveStrategy
ConnectionKeepAliveStrategy
From source file:org.aevans.goat.net.ConnectionUtils.java
public static ConnectionKeepAliveStrategy getConnectionKeepAliveStrategy(final int keepAlive) { return new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // Honor 'keep-alive' header HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { }/*from w w w . j av a2 s . c om*/ } } return keepAlive * 1000; } }; }
From source file:com.ninja.examples.utility.net.APIRequest.java
public static DefaultHttpClient getClient() { if (httpclient == null) { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setUseExpectContinue(params, false); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "utf-8"); params.setBooleanParameter("http.protocol.expect-continue", false); HttpConnectionParams.setConnectionTimeout(params, WS_TIMEOUT); HttpConnectionParams.setSoTimeout(params, WS_TIMEOUT); SchemeRegistry registry = new SchemeRegistry(); registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager(params, registry); httpclient = new DefaultHttpClient(manager, params); httpclient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { return 50; }/*from w w w. j a va2s . c om*/ }); } return httpclient; }
From source file:it.av.youeat.web.pubsubhubbub.Publisher.java
/** * Constructor// www . j av a 2 s .c o m */ public Publisher() { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, 200); ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20); connPerRoute.setDefaultMaxPerRoute(50); ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); ClientConnectionManager cm = new ThreadSafeClientConnManager(params, schemeRegistry); httpClient = new DefaultHttpClient(cm, params); httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } // default keepalive is 60 seconds. This is higher than usual // since the number of hubs it should be talking to should be // small return 30 * 1000; } }); }
From source file:org.frontcache.agent.FrontCacheAgent.java
public FrontCacheAgent(String frontcacheURL) { final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(10000).setConnectTimeout(3000) .setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override/*from ww w.j av a 2 s. c o m*/ public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return 10 * 1000; } }; client = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }).build(); this.frontCacheURL = frontcacheURL; if (frontcacheURL.endsWith("/")) this.frontCacheURI = frontcacheURL + IO_URI; else this.frontCacheURI = frontcacheURL + "/" + IO_URI; }
From source file:org.alfresco.cacheserver.http.CacheHttpClient.java
private CloseableHttpClient getHttpClient(HttpHost target, HttpClientContext localContext, String username, String password) {/*from w w w . j ava 2 s. c o m*/ ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // Honor 'keep-alive' header HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } HttpHost target = (HttpHost) context.getAttribute(HttpClientContext.HTTP_TARGET_HOST); if ("www.naughty-server.com".equalsIgnoreCase(target.getHostName())) { // Keep alive for 5 seconds only return 5 * 1000; } else { // otherwise keep alive for 30 seconds return 30 * 1000; } } }; HttpRequestRetryHandler retryHandler = new HttpRequestRetryHandler() { public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if (executionCount >= 5) { // Do not retry if over max retry count return false; } if (exception instanceof InterruptedIOException) { // Timeout return false; } if (exception instanceof UnknownHostException) { // Unknown host return false; } if (exception instanceof ConnectTimeoutException) { // Connection refused return false; } if (exception instanceof SSLException) { // SSL handshake exception return false; } HttpClientContext clientContext = HttpClientContext.adapt(context); HttpRequest request = clientContext.getRequest(); boolean idempotent = !(request instanceof HttpEntityEnclosingRequest); if (idempotent) { // Retry if the request is considered idempotent return true; } return false; } }; CredentialsProvider credsProvider = new BasicCredentialsProvider(); credsProvider.setCredentials(new AuthScope(target.getHostName(), target.getPort()), new UsernamePasswordCredentials(username, password)); RequestConfig defaultRequestConfig = RequestConfig.custom().setCookieSpec(CookieSpecs.DEFAULT) .setExpectContinueEnabled(true) // .setStaleConnectionCheckEnabled(true) .setTargetPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)) .setProxyPreferredAuthSchemes(Arrays.asList(AuthSchemes.BASIC)).build(); CloseableHttpClient httpclient = HttpClients.custom().setDefaultRequestConfig(defaultRequestConfig) .setDefaultCredentialsProvider(credsProvider).setKeepAliveStrategy(keepAliveStrategy) .setRetryHandler(retryHandler).build(); return httpclient; }
From source file:com.feedeo.web.client.AbstractWebClient.java
protected HttpClient createHttpClient() { final SocketConfig socketConfig = SocketConfig.custom().setSoKeepAlive(true).setTcpNoDelay(true).build(); final ConnectionConfig connectionConfig = ConnectionConfig.custom().build(); final RequestConfig requestConfig = RequestConfig.custom().setConnectionRequestTimeout(30000) .setConnectTimeout(30000).setSocketTimeout(30000).setStaleConnectionCheckEnabled(false).build(); final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager(); connectionManager.setMaxTotal(256);/* w w w . j av a2s . co m*/ connectionManager.setDefaultMaxPerRoute(256); IdleConnectionMonitorThread staleMonitor = new IdleConnectionMonitorThread(connectionManager); staleMonitor.start(); try { staleMonitor.join(1000); } catch (InterruptedException ignored) { } final ConnectionKeepAliveStrategy connectionKeepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator iterator = new BasicHeaderElementIterator( response.headerIterator(CONN_KEEP_ALIVE)); while (iterator.hasNext()) { HeaderElement header = iterator.nextElement(); String param = header.getName(); String value = header.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return 5 * 1000; } }; return HttpClientBuilder.create().setConnectionManager(connectionManager) .setDefaultRequestConfig(requestConfig).setDefaultSocketConfig(socketConfig) .setDefaultConnectionConfig(connectionConfig).setKeepAliveStrategy(connectionKeepAliveStrategy) .build(); }
From source file:mx.bigdata.utils.pubsubhubbub.Subscriber.java
public Subscriber(CallbackServer webserver) { this.webserver = webserver; SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory())); schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory())); //ConnManagerParams.setMaxTotalConnections(params, 200); //ConnPerRouteBean connPerRoute = new ConnPerRouteBean(20); //connPerRoute.setDefaultMaxPerRoute(50); //ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry); cm.setMaxTotal(200);/*from w w w . j ava 2 s. c o m*/ cm.setDefaultMaxPerRoute(50); httpClient = new DefaultHttpClient(cm, new BasicHttpParams()); httpClient.setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { try { return Long.parseLong(value) * 1000; } catch (NumberFormatException ignore) { } } } return 30 * 1000; } }); }
From source file:eu.alefzero.owncloud.syncadapter.AbstractOwnCloudSyncAdapter.java
protected ConnectionKeepAliveStrategy getKeepAliveStrategy() { return new ConnectionKeepAliveStrategy() { public long getKeepAliveDuration(HttpResponse response, HttpContext context) { // Change keep alive straategy basing on response: ie // forbidden/not found/etc // should have keep alive 0 // default return: 5s int statusCode = response.getStatusLine().getStatusCode(); // HTTP 400, 500 Errors as well as HTTP 118 - Connection timed // out if ((statusCode >= 400 && statusCode <= 418) || (statusCode >= 421 && statusCode <= 426) || (statusCode >= 500 && statusCode <= 510) || statusCode == 118) { return 0; }/*w w w .ja va 2s .c o m*/ return 5 * 1000; } }; }
From source file:org.frontcache.client.FrontCacheClient.java
private FrontCacheClient(String frontcacheURL) { final RequestConfig requestConfig = RequestConfig.custom().setSocketTimeout(CONNECTION_TIMEOUT) .setConnectTimeout(CONNECTION_TIMEOUT).setCookieSpec(CookieSpecs.IGNORE_COOKIES).build(); ConnectionKeepAliveStrategy keepAliveStrategy = new ConnectionKeepAliveStrategy() { @Override//from www. ja v a2s .c o m public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement he = it.nextElement(); String param = he.getName(); String value = he.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return 10 * 1000; } }; client = HttpClients.custom().setDefaultRequestConfig(requestConfig) .setRetryHandler(new DefaultHttpRequestRetryHandler(0, false)) .setKeepAliveStrategy(keepAliveStrategy).setRedirectStrategy(new RedirectStrategy() { @Override public boolean isRedirected(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return false; } @Override public HttpUriRequest getRedirect(HttpRequest request, HttpResponse response, HttpContext context) throws ProtocolException { return null; } }).build(); this.frontCacheURL = frontcacheURL; if (frontcacheURL.endsWith("/")) this.frontCacheURI = frontcacheURL + IO_URI; else this.frontCacheURI = frontcacheURL + "/" + IO_URI; }
From source file:ingest.Application.java
@Bean public RestTemplate restTemplate() { RestTemplate restTemplate = new RestTemplate(); HttpClient httpClient = HttpClientBuilder.create().setMaxConnTotal(httpMaxTotal) .setMaxConnPerRoute(httpMaxRoute).setKeepAliveStrategy(new ConnectionKeepAliveStrategy() { @Override//from w ww . j a v a 2s . c o m public long getKeepAliveDuration(HttpResponse response, HttpContext context) { HeaderElementIterator it = new BasicHeaderElementIterator( response.headerIterator(HTTP.CONN_KEEP_ALIVE)); while (it.hasNext()) { HeaderElement headerElement = it.nextElement(); String param = headerElement.getName(); String value = headerElement.getValue(); if (value != null && param.equalsIgnoreCase("timeout")) { return Long.parseLong(value) * 1000; } } return (long) 5 * 1000; } }).build(); restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory(httpClient)); return restTemplate; }