Java Code Examples for org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager#setDefaultMaxPerRoute()

The following examples show how to use org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager#setDefaultMaxPerRoute() . 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: HttpSendClientFactory.java    From PoseidonX with Apache License 2.0 6 votes vote down vote up
/**
 * 创建一个{@link HttpSendClient} 实例
 * <p>
 * 多态
 * 
 * @return
 */
public HttpSendClient newHttpSendClient() {
    HttpParams params = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(params, getConnectionTimeOut());
    HttpConnectionParams.setSoTimeout(params, getSoTimeOut());
    // HttpConnectionParams.setLinger(params, 1);

    HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
    HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
    // 解释: 握手的目的,是为了允许客户端在发送请求内容之前,判断源服务器是否愿意接受请求(基于请求头部)。
    // Expect:100-Continue握手需谨慎使用,因为遇到不支持HTTP/1.1协议的服务器或者代理时会引起问题。
    // 默认开启
    // HttpProtocolParams.setUseExpectContinue(params, false);
    HttpConnectionParams.setTcpNoDelay(params, true);
    HttpConnectionParams.setSocketBufferSize(params, getSocketBufferSize());

    ThreadSafeClientConnManager threadSafeClientConnManager = new ThreadSafeClientConnManager();
    threadSafeClientConnManager.setMaxTotal(getMaxTotalConnections());
    threadSafeClientConnManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute());

    DefaultHttpRequestRetryHandler retryHandler = new DefaultHttpRequestRetryHandler(getRetryCount(), false);

    DefaultHttpClient httpClient = new DefaultHttpClient(threadSafeClientConnManager, params);
    httpClient.setHttpRequestRetryHandler(retryHandler);
    return new HttpSendClient(httpClient);
}
 
Example 2
Source File: HttpLoadTestClient.java    From carbon-apimgt with Apache License 2.0 6 votes vote down vote up
@Override
public void run() {
    SchemeRegistry supportedSchemes = new SchemeRegistry();
    SocketFactory sf = PlainSocketFactory.getSocketFactory();
    supportedSchemes.register(new Scheme("http", sf, 80));
    ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager(supportedSchemes);
    connManager.setDefaultMaxPerRoute(1000);
    DefaultHttpClient client = new DefaultHttpClient(connManager);
    HttpParams params = client.getParams();
    HttpConnectionParams.setConnectionTimeout(params, 30000);
    HttpConnectionParams.setSoTimeout(params, 30000);
    client.setHttpRequestRetryHandler(new HttpRequestRetryHandler() {
        public boolean retryRequest(IOException e, int i, HttpContext httpContext) {
            return false;
        }
    });
    //test API call
    long t1 = System.currentTimeMillis();
    testEndpoint(client,apiEndpoint);
    long t2 = System.currentTimeMillis();
    timeElapsedForAPICall = t2 - t1;


}
 
Example 3
Source File: NFHttpClientTest.java    From ribbon with Apache License 2.0 6 votes vote down vote up
@Test
public void testNFHttpClient() throws Exception {
    NFHttpClient client = NFHttpClientFactory.getNFHttpClient("localhost", server.getServerPort());
    ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) client.getConnectionManager();
	cm.setDefaultMaxPerRoute(10);
    HttpGet get = new HttpGet(server.getServerURI());
    ResponseHandler<Integer> respHandler = new ResponseHandler<Integer>(){
    		public Integer handleResponse(HttpResponse response)
             throws ClientProtocolException, IOException {
    			HttpEntity entity = response.getEntity();
    			String contentStr = EntityUtils.toString(entity);
        		return contentStr.length();
    		}
    };
    long contentLen = client.execute(get, respHandler);
    assertTrue(contentLen > 0);
}
 
Example 4
Source File: GoogleAnalytics.java    From h2o-2 with Apache License 2.0 6 votes vote down vote up
protected HttpClient createHttpClient(GoogleAnalyticsConfig config) {
  ThreadSafeClientConnManager connManager = new ThreadSafeClientConnManager();
  connManager.setDefaultMaxPerRoute(getDefaultMaxPerRoute(config));

  BasicHttpParams params = new BasicHttpParams();

  if (isNotEmpty(config.getUserAgent())) {
    params.setParameter(CoreProtocolPNames.USER_AGENT, config.getUserAgent());
  }

  if (isNotEmpty(config.getProxyHost())) {
    params.setParameter(ConnRoutePNames.DEFAULT_PROXY, new HttpHost(config.getProxyHost(), config.getProxyPort()));
  }

  DefaultHttpClient client = new DefaultHttpClient(connManager, params);

  if (isNotEmpty(config.getProxyUserName())) {
    BasicCredentialsProvider credentialsProvider = new BasicCredentialsProvider();
    credentialsProvider.setCredentials(new AuthScope(config.getProxyHost(), config.getProxyPort()),
            new UsernamePasswordCredentials(config.getProxyUserName(), config.getProxyPassword()));
    client.setCredentialsProvider(credentialsProvider);
  }

  return client;
}
 
Example 5
Source File: URIHandle.java    From java-client-api with Apache License 2.0 5 votes vote down vote up
public URIHandle(String host, int port, String user, String password, Authentication authType) {
  super();

  SchemeRegistry schemeRegistry = new SchemeRegistry();
  schemeRegistry.register(
    new Scheme("http", port, PlainSocketFactory.getSocketFactory())
  );

  // PoolingClientConnectionManager connMgr = new PoolingClientConnectionManager(  // 4.2
  ThreadSafeClientConnManager connMgr = new ThreadSafeClientConnManager(
    schemeRegistry);
  connMgr.setDefaultMaxPerRoute(100);

  DefaultHttpClient defaultClient = new DefaultHttpClient(connMgr);

  List<String> prefList = new ArrayList<>();
  if (authType == Authentication.BASIC)
    prefList.add(AuthPolicy.BASIC);
  else if (authType == Authentication.DIGEST)
    prefList.add(AuthPolicy.DIGEST);
  else
    throw new IllegalArgumentException("Unknown authentication type "+authType.name());

  defaultClient.getParams().setParameter(AuthPNames.PROXY_AUTH_PREF, prefList);

  defaultClient.getCredentialsProvider().setCredentials(
    new AuthScope(host, port),
    new UsernamePasswordCredentials(user, password)
  );

  setClient(defaultClient);
}
 
Example 6
Source File: NFHttpClientTest.java    From ribbon with Apache License 2.0 5 votes vote down vote up
@Ignore
public void testMultiThreadedClient() throws Exception {

    NFHttpClient client = (NFHttpClient) NFHttpClientFactory
            .getNFHttpClient("hc.apache.org", 80);
   
    ThreadSafeClientConnManager cm = (ThreadSafeClientConnManager) client.getConnectionManager();
    cm.setDefaultMaxPerRoute(10);
    
    HttpHost target = new HttpHost("hc.apache.org", 80);

    // create an array of URIs to perform GETs on
    String[] urisToGet = { "/", "/httpclient-3.x/status.html",
            "/httpclient-3.x/methods/",
            "http://svn.apache.org/viewvc/httpcomponents/oac.hc3x/" };

    // create a thread for each URI
    GetThread[] threads = new GetThread[urisToGet.length];
    for (int i = 0; i < threads.length; i++) {
        HttpGet get = new HttpGet(urisToGet[i]);
        threads[i] = new GetThread(client, target, get, i + 1);
    }

    // start the threads
    for (int j = 0; j < threads.length; j++) {
        threads[j].start();
    }

}
 
Example 7
Source File: AbstractProtocolTest.java    From database with GNU General Public License v2.0 4 votes vote down vote up
protected ClientConnectionManager newInstance() {

        final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(
                newSchemeRegistry());

        // 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 to 50
        final HttpHost localhost = new HttpHost("locahost");

        cm.setMaxForRoute(new HttpRoute(localhost), 50);

        return cm;

    }