/** * @since 4.1 */ public ConnPoolByRoute( final ClientConnectionOperator operator, final ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, final TimeUnit connTTLTimeUnit) { super(); if (operator == null) { throw new IllegalArgumentException("Connection operator may not be null"); } if (connPerRoute == null) { throw new IllegalArgumentException("Connections per route may not be null"); } this.poolLock = super.poolLock; this.leasedConnections = super.leasedConnections; this.operator = operator; this.connPerRoute = connPerRoute; this.maxTotalConnections = maxTotalConnections; this.freeConnections = createFreeConnQueue(); this.waitingThreads = createWaitingThreadQueue(); this.routeToPool = createRouteToPoolMap(); this.connTTL = connTTL; this.connTTLTimeUnit = connTTLTimeUnit; }
/** * @since 4.1 */ public ConnPoolByRoute( final ClientConnectionOperator operator, final ConnPerRoute connPerRoute, final int maxTotalConnections, final long connTTL, final TimeUnit connTTLTimeUnit) { super(); Args.notNull(operator, "Connection operator"); Args.notNull(connPerRoute, "Connections per route"); this.poolLock = super.poolLock; this.leasedConnections = super.leasedConnections; this.operator = operator; this.connPerRoute = connPerRoute; this.maxTotalConnections = maxTotalConnections; this.freeConnections = createFreeConnQueue(); this.waitingThreads = createWaitingThreadQueue(); this.routeToPool = createRouteToPoolMap(); this.connTTL = connTTL; this.connTTLTimeUnit = connTTLTimeUnit; }
public static HttpClient generateHttpClient(final int maxTotalConnections, final int maxTotalConnectionsPerRoute, int connTimeout) { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute route) { return maxTotalConnectionsPerRoute; } }); HttpConnectionParams .setConnectionTimeout(params, connTimeout); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory. ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(conMgr, params); }
public static HttpClient generateHttpClient(final int maxTotalConnections, final int maxTotalConnectionsPerRoute, int connTimeout) { HttpParams params = new BasicHttpParams(); ConnManagerParams.setMaxTotalConnections(params, maxTotalConnections); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute route) { return maxTotalConnectionsPerRoute; } }); HttpConnectionParams .setConnectionTimeout(params, connTimeout); SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register( new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); SSLSocketFactory sslSocketFactory = SSLSocketFactory.getSocketFactory(); sslSocketFactory.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params, schemeRegistry); return new DefaultHttpClient(conMgr, params); }
/** * Creates a new connection pool, managed by route. * * @since 4.1 */ public ConnPoolByRoute( final ClientConnectionOperator operator, final ConnPerRoute connPerRoute, int maxTotalConnections) { this(operator, connPerRoute, maxTotalConnections, -1, TimeUnit.MILLISECONDS); }
/** * Creates a new route-specific pool. * * @param route the route for which to pool * @param connPerRoute the connections per route configuration */ public RouteSpecificPool(HttpRoute route, ConnPerRoute connPerRoute) { this.route = route; this.connPerRoute = connPerRoute; this.maxEntries = connPerRoute.getMaxForRoute(route); this.freeEntries = new LinkedList<BasicPoolEntry>(); this.waitingThreads = new LinkedList<WaitingThread>(); this.numEntries = 0; }
/** * Creates a new connection pool, managed by route. * * @since 4.1 */ public ConnPoolByRoute( final ClientConnectionOperator operator, final ConnPerRoute connPerRoute, final int maxTotalConnections) { this(operator, connPerRoute, maxTotalConnections, -1, TimeUnit.MILLISECONDS); }
/** * @deprecated (4.1) use {@link RouteSpecificPool#RouteSpecificPool(HttpRoute, ConnPerRoute)} */ @Deprecated public RouteSpecificPool(final HttpRoute route, final int maxEntries) { this.route = route; this.maxEntries = maxEntries; this.connPerRoute = new ConnPerRoute() { @Override public int getMaxForRoute(final HttpRoute unused) { return RouteSpecificPool.this.maxEntries; } }; this.freeEntries = new LinkedList<BasicPoolEntry>(); this.waitingThreads = new LinkedList<WaitingThread>(); this.numEntries = 0; }
/** * Creates a new route-specific pool. * * @param route the route for which to pool * @param connPerRoute the connections per route configuration */ public RouteSpecificPool(final HttpRoute route, final ConnPerRoute connPerRoute) { this.route = route; this.connPerRoute = connPerRoute; this.maxEntries = connPerRoute.getMaxForRoute(route); this.freeEntries = new LinkedList<BasicPoolEntry>(); this.waitingThreads = new LinkedList<WaitingThread>(); this.numEntries = 0; }
private static ClientConnectionManager createClientConnectionManager() { HttpParams params = new BasicHttpParams(); params.setIntParameter(ConnManagerPNames.MAX_TOTAL_CONNECTIONS, MAX_CONNECTIONS); params.setParameter(ConnManagerParams.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute httpRoute) { return MAX_CONNECTIONS_PER_ROUTE; } }); return new ThreadSafeClientConnManager(params, prepareSchemeRegistry()); }
/** * @return the default HttpParams * @see <a href="http://developer.android.com/reference/android/net/http/AndroidHttpClient.html#newInstance(java.lang.String, android.content.Context)"> * android.net.http.AndroidHttpClient#newInstance(String, Context)</a> */ protected HttpParams getParams() { final HttpParams params = new BasicHttpParams(); HttpConnectionParams.setConnectionTimeout(params, TIMEOUT); HttpConnectionParams.setSoTimeout(params, TIMEOUT); HttpConnectionParams.setSocketBufferSize(params, BUFFER_SIZE); ConnManagerParams.setMaxTotalConnections(params, MAX_TOTAL_CONNECTIONS); // Turn off stale checking. Our connections break all the time anyway, // and it's not worth it to pay the penalty of checking every time. HttpConnectionParams.setStaleCheckingEnabled(params, false); // fix contributed by Bjorn Roche XXX check if still needed params.setBooleanParameter("http.protocol.expect-continue", false); params.setParameter(ConnManagerPNames.MAX_CONNECTIONS_PER_ROUTE, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute httpRoute) { if (env.isApiHost(httpRoute.getTargetHost())) { // there will be a lot of concurrent request to the API host return MAX_TOTAL_CONNECTIONS; } else { return ConnPerRouteBean.DEFAULT_MAX_CONNECTIONS_PER_ROUTE; } } }); // apply system proxy settings final String proxyHost = System.getProperty("http.proxyHost"); final String proxyPort = System.getProperty("http.proxyPort"); if (proxyHost != null) { int port = 80; try { port = Integer.parseInt(proxyPort); } catch (NumberFormatException ignored) { } params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(proxyHost, port)); } return params; }
public NamedConnectionPool(String name, ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, TimeUnit connTTLTimeUnit) { super(operator, connPerRoute, maxTotalConnections, connTTL, connTTLTimeUnit); initMonitors(name); }
public NamedConnectionPool(String name, ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections) { super(operator, connPerRoute, maxTotalConnections); initMonitors(name); }
NamedConnectionPool(ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, TimeUnit connTTLTimeUnit) { super(operator, connPerRoute, maxTotalConnections, connTTL, connTTLTimeUnit); }
NamedConnectionPool(ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections) { super(operator, connPerRoute, maxTotalConnections); }
private CougarConnPoolByRoute(ClientConnectionOperator operator, ConnPerRoute connPerRoute, int maxTotalConnections, long connTTL, TimeUnit connTTLTimeUnit) { super(operator, connPerRoute, maxTotalConnections, connTTL, connTTLTimeUnit); }
private HttpManager(Boolean debug, String version) { // Set basic data HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, "UTF-8"); HttpProtocolParams.setUseExpectContinue(params, true); HttpProtocolParams.setUserAgent(params, HttpUtils.userAgent); // Make pool ConnPerRoute connPerRoute = new ConnPerRouteBean(12); ConnManagerParams.setMaxConnectionsPerRoute(params, connPerRoute); ConnManagerParams.setMaxTotalConnections(params, 20); // Set timeout HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); HttpConnectionParams.setSoTimeout(params, 20 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); // Some client params HttpClientParams.setRedirecting(params, false); // Register http/s schemas! SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); if (debug) { // Install the all-trusting trust manager // Create a trust manager that does not validate certificate chains TrustManager[] trustManagers = new X509TrustManager[1]; trustManagers[0] = new TrustAllManager(); try { SSLContext sc = SSLContext.getInstance("SSL"); sc.init(null, trustManagers, null); schReg.register(new Scheme("https", (SocketFactory) sc .getSocketFactory(), 443)); } catch (Exception e) { ; } } else { schReg.register(new Scheme("https", SSLSocketFactory .getSocketFactory(), 443)); } ClientConnectionManager conMgr = new ThreadSafeClientConnManager( params, schReg); client = new DefaultHttpClient(conMgr, params); }
public HttpClient getHttpClient() { HttpParams params = new BasicHttpParams(); HttpProtocolParams.setUseExpectContinue(params, false); HttpProtocolParams.setUserAgent(params, ConnectioinConfig.userAgent); ConnManagerParams.setMaxTotalConnections(params, ConnectioinConfig.maxConnections); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { @Override public int getMaxForRoute(HttpRoute httproute) { return 32; } }); HttpClientParams.setRedirecting(params, true); params.setParameter( CoreConnectionPNames.CONNECTION_TIMEOUT, ConnectioinConfig.connectionTimeout); params.setParameter(CoreConnectionPNames.SO_TIMEOUT, ConnectioinConfig.socketTimeout); params.setParameter( CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024); params.setParameter(ClientPNames.HANDLE_REDIRECTS, Boolean.FALSE); params.setParameter( CoreProtocolPNames.HTTP_CONTENT_CHARSET, "UTF-8"); // 默认为ISO-8859-1 params.setParameter( CoreProtocolPNames.HTTP_ELEMENT_CHARSET, "UTF-8"); // 默认为US-ASCII params.removeParameter(ConnRouteParams.DEFAULT_PROXY); SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory .getSocketFactory(), 443)); ThreadSafeClientConnManager conMgr = new ThreadSafeClientConnManager( params, schReg); DefaultHttpClient client = new DefaultHttpClient(conMgr, params); //Auto retry. client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() { @Override public boolean retryRequest(IOException exception, int executionCount, HttpContext context) { if(executionCount > ConnectioinConfig.maxRetryCount) return false; if(exception instanceof NoHttpResponseException) return true; if(exception instanceof SocketTimeoutException) return true; return false; } }); return client; }
/** * Get a singleton, thread-safe HttpClient for making HTTP requests. */ public static synchronized HttpClient getHttpClient() { if (customHttpClient == null) { final HttpParams params = new BasicHttpParams(); HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); HttpProtocolParams.setContentCharset(params, HTTP.DEFAULT_CONTENT_CHARSET); HttpProtocolParams.setUseExpectContinue(params, false); ConnManagerParams.setTimeout(params, 100); HttpConnectionParams.setConnectionTimeout(params, 10000); HttpConnectionParams.setSoTimeout(params, 10000); final SchemeRegistry schReg = new SchemeRegistry(); schReg.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80)); schReg.register(new Scheme("https", SSLSocketFactory.getSocketFactory(), 443)); ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { @Override public int getMaxForRoute(final HttpRoute httproute) { return 20; } }); final ClientConnectionManager conMgr = new ThreadSafeClientConnManager(params,schReg); customHttpClient = new DefaultHttpClient(conMgr, params); requestRetryHandler = new DefaultHttpRequestRetryHandler(5, false){ @Override public boolean retryRequest(IOException ex, int count, HttpContext cx) { if(super.retryRequest(ex, count, cx)){ Log.d(TAG, "Retrying request " + cx.toString()); try { Thread.sleep(2000); } catch (InterruptedException e) { e.printStackTrace(); } return true; } else{ return false; } } }; customHttpClient.setHttpRequestRetryHandler(requestRetryHandler); } return customHttpClient; }
static DefaultHttpClient createClient(String userAgent, boolean acceptAllSslCertificates) { HttpParams params = new BasicHttpParams(); HttpConnectionParams.setStaleCheckingEnabled(params, false); HttpConnectionParams.setConnectionTimeout(params, 20 * 1000); // to make connection pool more fault tolerant ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRoute() { public int getMaxForRoute(HttpRoute route) { return 10; } }); HttpConnectionParams.setSoTimeout(params, 20 * 1000); HttpConnectionParams.setSocketBufferSize(params, 8192); HttpClientParams.setRedirecting(params, false); HttpProtocolParams.setUserAgent(params, userAgent); SSLSocketFactory sslSocketFactory = null; if (acceptAllSslCertificates) { try { sslSocketFactory = new AcceptAllSocketFactory(); } catch (Exception e) { // omitted } } if (sslSocketFactory == null) { sslSocketFactory = SSLSocketFactory.getSocketFactory(); } SchemeRegistry schemeRegistry = new SchemeRegistry(); schemeRegistry.register(new Scheme("http", PlainSocketFactory .getSocketFactory(), 80)); schemeRegistry.register(new Scheme("https", sslSocketFactory, 443)); ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager( params, schemeRegistry); final DefaultHttpClient client = new DefaultHttpClient(manager, params); return client; }
/** * Creates a new route-specific pool. * Called by {@link #getRoutePool} when necessary. * * @param route the route * * @return the new pool */ protected RouteSpecificPool newRouteSpecificPool(HttpRoute route) { ConnPerRoute connPerRoute = ConnManagerParams.getMaxConnectionsPerRoute(params); return new RouteSpecificPool(route, connPerRoute.getMaxForRoute(route)); }