Java Code Examples for org.apache.http.conn.params.ConnManagerParams#setMaxConnectionsPerRoute()

The following examples show how to use org.apache.http.conn.params.ConnManagerParams#setMaxConnectionsPerRoute() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpEngine.java    From letv with Apache License 2.0 6 votes vote down vote up
private HttpParams createHttpParams() {
    HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    HttpProtocolParams.setUseExpectContinue(params, false);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(3));
    ConnManagerParams.setMaxTotalConnections(params, 3);
    ConnManagerParams.setTimeout(params, 1000);
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, 8192);
    HttpClientParams.setRedirecting(params, false);
    return params;
}
 
Example 2
Source File: MetricsClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 3
Source File: ClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 4
Source File: EMQClientFactory.java    From galaxy-sdk-java with Apache License 2.0 6 votes vote down vote up
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);
}
 
Example 5
Source File: HttpClientFactory.java    From miappstore with Apache License 2.0 6 votes vote down vote up
private static HttpParams createHttpParams() {
	final HttpParams params = new BasicHttpParams();
	// 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。
	// 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间
	HttpConnectionParams.setStaleCheckingEnabled(params, false);
	HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间
	HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间
	HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小
	HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟)
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本
	HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制
	HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码
	HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向

	ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时
	ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数
	ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数
	return params;
}
 
Example 6
Source File: AsyncHttpClient.java    From MiBandDecompiled with Apache License 2.0 6 votes vote down vote up
public AsyncHttpClient(SchemeRegistry schemeregistry)
{
    a = 10;
    b = 10000;
    h = true;
    BasicHttpParams basichttpparams = new BasicHttpParams();
    ConnManagerParams.setTimeout(basichttpparams, b);
    ConnManagerParams.setMaxConnectionsPerRoute(basichttpparams, new ConnPerRouteBean(a));
    ConnManagerParams.setMaxTotalConnections(basichttpparams, 10);
    HttpConnectionParams.setSoTimeout(basichttpparams, b);
    HttpConnectionParams.setConnectionTimeout(basichttpparams, b);
    HttpConnectionParams.setTcpNoDelay(basichttpparams, true);
    HttpConnectionParams.setSocketBufferSize(basichttpparams, 8192);
    HttpProtocolParams.setVersion(basichttpparams, HttpVersion.HTTP_1_1);
    ThreadSafeClientConnManager threadsafeclientconnmanager = new ThreadSafeClientConnManager(basichttpparams, schemeregistry);
    e = getDefaultThreadPool();
    f = new WeakHashMap();
    g = new HashMap();
    d = new SyncBasicHttpContext(new BasicHttpContext());
    c = new DefaultHttpClient(threadsafeclientconnmanager, basichttpparams);
    c.addRequestInterceptor(new a(this));
    c.addResponseInterceptor(new b(this));
    c.addRequestInterceptor(new c(this), 0);
    c.setHttpRequestRetryHandler(new z(5, 1500));
}
 
Example 7
Source File: HttpClientFactory.java    From NetEasyNews with GNU General Public License v3.0 6 votes vote down vote up
private static HttpParams createHttpParams() {
	final HttpParams params = new BasicHttpParams();
	// 设置是否启用旧连接检查,默认是开启的。关闭这个旧连接检查可以提高一点点性能,但是增加了I/O错误的风险(当服务端关闭连接时)。
	// 开启这个选项则在每次使用老的连接之前都会检查连接是否可用,这个耗时大概在15-30ms之间
	HttpConnectionParams.setStaleCheckingEnabled(params, false);
	HttpConnectionParams.setConnectionTimeout(params, TIMEOUT);// 设置链接超时时间
	HttpConnectionParams.setSoTimeout(params, TIMEOUT);// 设置socket超时时间
	HttpConnectionParams.setSocketBufferSize(params, SOCKET_BUFFER_SIZE);// 设置缓存大小
	HttpConnectionParams.setTcpNoDelay(params, true);// 是否不使用延迟发送(true为不延迟)
	HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1); // 设置协议版本
	HttpProtocolParams.setUseExpectContinue(params, true);// 设置异常处理机制
	HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);// 设置编码
	HttpClientParams.setRedirecting(params, false);// 设置是否采用重定向

	ConnManagerParams.setTimeout(params, TIMEOUT);// 设置超时
	ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(MAX_CONNECTIONS));// 多线程最大连接数
	ConnManagerParams.setMaxTotalConnections(params, 10); // 多线程总连接数
	return params;
}
 
Example 8
Source File: NetworkHelper.java    From fanfouapp-opensource with Apache License 2.0 6 votes vote down vote up
private static final HttpParams createHttpParams() {
    final HttpParams params = new BasicHttpParams();
    HttpProtocolParams.setUseExpectContinue(params, false);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    ConnManagerParams.setTimeout(params, NetworkHelper.SOCKET_TIMEOUT_MS);
    HttpConnectionParams.setConnectionTimeout(params,
            NetworkHelper.CONNECTION_TIMEOUT_MS);
    HttpConnectionParams.setSoTimeout(params,
            NetworkHelper.SOCKET_TIMEOUT_MS);

    ConnManagerParams.setMaxConnectionsPerRoute(params,
            new ConnPerRouteBean(NetworkHelper.MAX_TOTAL_CONNECTIONS));
    ConnManagerParams.setMaxTotalConnections(params,
            NetworkHelper.MAX_TOTAL_CONNECTIONS);

    HttpConnectionParams.setStaleCheckingEnabled(params, false);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params,
            NetworkHelper.SOCKET_BUFFER_SIZE);
    HttpClientParams.setRedirecting(params, false);
    HttpProtocolParams.setUserAgent(params, "FanFou for Android/"
            + AppContext.appVersionName);
    return params;
}
 
Example 9
Source File: AsyncHttpClient.java    From sealtalk-android with MIT License 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 *
 * @param maxConnections maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
    if (maxConnections < 1)
        maxConnections = DEFAULT_MAX_CONNECTIONS;
    this.maxConnections = maxConnections;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 10
Source File: AsyncHttpClient.java    From android-project-wo2b with Apache License 2.0 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 *
 * @param maxConnections maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
    if (maxConnections < 1)
        maxConnections = DEFAULT_MAX_CONNECTIONS;
    this.maxConnections = maxConnections;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 11
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 *
 * @param maxConnections maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
    if (maxConnections < 1)
        maxConnections = DEFAULT_MAX_CONNECTIONS;
    this.maxConnections = maxConnections;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 12
Source File: AsyncHttpClient.java    From Android-Basics-Codes with Artistic License 2.0 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 *
 * @param maxConnections maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
    if (maxConnections < 1)
        maxConnections = DEFAULT_MAX_CONNECTIONS;
    this.maxConnections = maxConnections;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 13
Source File: SyncHttpClient.java    From sealtalk-android with MIT License 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 * 
 * @param maxConnections
 *            maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
	if (maxConnections < 1) maxConnections = DEFAULT_MAX_CONNECTIONS;
	this.maxConnections = maxConnections;
	final HttpParams httpParams = this.httpClient.getParams();
	ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 14
Source File: AsyncHttpClient.java    From MiBandDecompiled with Apache License 2.0 5 votes vote down vote up
public void setMaxConnections(int i)
{
    if (i < 1)
    {
        i = 10;
    }
    a = i;
    ConnManagerParams.setMaxConnectionsPerRoute(c.getParams(), new ConnPerRouteBean(a));
}
 
Example 15
Source File: ApacheHttpTransport.java    From google-http-java-client with Apache License 2.0 5 votes vote down vote up
/** Returns a new instance of the default HTTP parameters we use. */
static HttpParams newDefaultHttpParams() {
  HttpParams params = new BasicHttpParams();
  // 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);
  HttpConnectionParams.setSocketBufferSize(params, 8192);
  ConnManagerParams.setMaxTotalConnections(params, 200);
  ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(20));
  return params;
}
 
Example 16
Source File: AsyncHttpClient.java    From AndroidWear-OpenWear with MIT License 5 votes vote down vote up
/**
 * Sets maximum limit of parallel connections
 *
 * @param maxConnections maximum parallel connections, must be at least 1
 */
public void setMaxConnections(int maxConnections) {
    if (maxConnections < 1)
        maxConnections = DEFAULT_MAX_CONNECTIONS;
    this.maxConnections = maxConnections;
    final HttpParams httpParams = this.httpClient.getParams();
    ConnManagerParams.setMaxConnectionsPerRoute(httpParams, new ConnPerRouteBean(this.maxConnections));
}
 
Example 17
Source File: ApacheClient.java    From android-lite-http with Apache License 2.0 5 votes vote down vote up
/**
 * initialize HttpParams , initialize settings such as total connextions,timeout ...
 */
private BasicHttpParams createHttpParams() {
    BasicHttpParams params = new BasicHttpParams();
    ConnManagerParams.setTimeout(params, DEFAULT_TIMEOUT);
    ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(DEFAULT_MAX_CONN_PER_ROUT));
    ConnManagerParams.setMaxTotalConnections(params, DEFAULT_MAX_CONN_TOTAL);
    HttpConnectionParams.setTcpNoDelay(params, TCP_NO_DELAY);
    HttpConnectionParams.setConnectionTimeout(params, DEFAULT_TIMEOUT);
    HttpConnectionParams.setSoTimeout(params, DEFAULT_TIMEOUT);
    HttpConnectionParams.setSocketBufferSize(params, DEFAULT_BUFFER_SIZE);
    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setUserAgent(params, userAgent);
    // settingOthers(params);
    return params;
}
 
Example 18
Source File: BuddycloudHTTPHelper.java    From buddycloud-android with Apache License 2.0 5 votes vote down vote up
public static HttpClient createHttpClient(Context context) {
	try {

		SchemeRegistry registry = new SchemeRegistry();
		SocketFactory socketFactory = createSecureSocketFactory();

		registry.register(new Scheme("http", PlainSocketFactory
				.getSocketFactory(), 80));
		registry.register(new Scheme("https", socketFactory, 443));

		HttpParams connManagerParams = new BasicHttpParams();
		ConnManagerParams.setMaxTotalConnections(connManagerParams, 20);
		ConnManagerParams.setMaxConnectionsPerRoute(connManagerParams,
				new ConnPerRouteBean(20));

		ClientConnectionManager ccm = new ThreadSafeClientConnManager(
				connManagerParams, registry);

		DefaultHttpClient client = new DefaultHttpClient(ccm, null);
		client.setHttpRequestRetryHandler(new DefaultHttpRequestRetryHandler(
				0, true));

		return client;
	} catch (Exception e) {
		throw new RuntimeException(e);
	}
}
 
Example 19
Source File: AbstractGoogleClientFactory.java    From nexus-blobstore-google-cloud with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Replicates default connection and protocol parameters used within
 * {@link ApacheHttpTransport#newDefaultHttpClient()} with one exception:
 *
 * Stale checking is enabled.
 */
HttpParams newDefaultHttpParams() {
  HttpParams params = new BasicHttpParams();
  HttpConnectionParams.setStaleCheckingEnabled(params, true);
  HttpConnectionParams.setSocketBufferSize(params, 8192);
  ConnManagerParams.setMaxTotalConnections(params, 200);
  ConnManagerParams.setMaxConnectionsPerRoute(params, new ConnPerRouteBean(200));
  return params;
}
 
Example 20
Source File: Utilities.java    From TurkcellUpdater_android_sdk with Apache License 2.0 4 votes vote down vote up
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;
}