Example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute

List of usage examples for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute

Introduction

In this page you can find the example usage for org.apache.http.impl.conn.tsccm ThreadSafeClientConnManager setDefaultMaxPerRoute.

Prototype

public void setDefaultMaxPerRoute(final int max) 

Source Link

Usage

From source file:com.netflix.http4.NFHttpClientTest.java

@Test
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);
    }/*from  ww  w .ja va  2 s  .  c  om*/

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

}

From source file:com.woonoz.proxy.servlet.ProxyServlet.java

public void init(ProxyServletConfig config) {
    targetServer = config.getTargetUrl();
    if (targetServer != null) {
        SchemeRegistry schemeRegistry = new SchemeRegistry();
        schemeRegistry.register(new Scheme(targetServer.getProtocol(), getPortOrDefault(targetServer.getPort()),
                PlainSocketFactory.getSocketFactory()));
        BasicHttpParams httpParams = new BasicHttpParams();
        HttpConnectionParams.setConnectionTimeout(httpParams, config.getConnectionTimeout());
        HttpConnectionParams.setSoTimeout(httpParams, config.getSocketTimeout());
        ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager(schemeRegistry);
        cm.setDefaultMaxPerRoute(config.getMaxConnections());
        cm.setMaxTotal(config.getMaxConnections());
        client = new DefaultHttpClient(cm, httpParams);
        client.removeResponseInterceptorByClass(ResponseProcessCookies.class);
        client.removeRequestInterceptorByClass(RequestAddCookies.class);

        final String remoteUserHeader = config.getRemoteUserHeader();
        if (null != remoteUserHeader) {
            client.addRequestInterceptor(new HttpRequestInterceptor() {

                @Override//  w  w w .  j a va 2 s .co m
                public void process(HttpRequest request, HttpContext context)
                        throws HttpException, IOException {
                    request.removeHeaders(remoteUserHeader);
                    HttpRequestHandler handler;
                    if (context != null && (handler = (HttpRequestHandler) context
                            .getAttribute(HttpRequestHandler.class.getName())) != null) {
                        String remoteUser = handler.getRequest().getRemoteUser();
                        if (remoteUser != null) {
                            request.addHeader(remoteUserHeader, remoteUser);
                        }
                    }
                }
            });
        }
    }
}

From source file:org.piraso.io.impl.HttpEntrySource.java

private void initReader() {
    alive = false;//from  w ww  . j ava2s  .c  o m

    ThreadSafeClientConnManager manager = new ThreadSafeClientConnManager();
    manager.setDefaultMaxPerRoute(2);
    manager.setMaxTotal(2);

    HttpParams params = new BasicHttpParams();

    // set timeout
    HttpConnectionParamBean connParamBean = new HttpConnectionParamBean(params);
    connParamBean.setConnectionTimeout(3000);
    connParamBean.setSoTimeout(1000 * 60 * 120);

    HttpClient client = new DefaultHttpClient(manager, params);
    HttpContext context = new BasicHttpContext();

    this.reader = new HttpPirasoEntryReader(client, context);

    reader.setUri(uri);
    reader.getStartHandler().setPreferences(preferences);

    if (watchedAddr != null) {
        reader.getStartHandler().setWatchedAddr(watchedAddr);
    }
}

From source file:com.flicklib.service.HttpClientSourceLoader.java

@Inject
public HttpClientSourceLoader(@Named(value = Constants.HTTP_TIMEOUT) final Integer timeout) {

    ThreadSafeClientConnManager tm = new ThreadSafeClientConnManager();
    tm.setDefaultMaxPerRoute(20);
    tm.setMaxTotal(200);/*from  www  .  j a v a2  s  . com*/
    client = new DefaultHttpClient(tm);

    if (timeout != null) {
        // wait max x sec
        HttpConnectionParams.setSoTimeout(client.getParams(), timeout);
    }
}

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 ww  w.  j ava 2  s .  co 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:com.soulgalore.crawler.guice.HttpClientProvider.java

/**
 * Get the client.// w  w  w .ja v a  2s  . c  o m
 * 
 * @return the client
 */
public HttpClient get() {
    final ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    cm.setMaxTotal(nrOfThreads);
    cm.setDefaultMaxPerRoute(maxToRoute);
    final DefaultHttpClient client = HTTPSFaker.getClientThatAllowAnyHTTPS(cm);

    client.getParams().setParameter("http.socket.timeout", socketTimeout);
    client.getParams().setParameter("http.connection.timeout", connectionTimeout);

    client.addRequestInterceptor(new RequestAcceptEncoding());
    client.addResponseInterceptor(new ResponseContentEncoding());

    CookieSpecFactory csf = new CookieSpecFactory() {
        public CookieSpec newInstance(HttpParams params) {
            return new BestMatchSpecWithURLErrorLog();
        }
    };

    client.getCookieSpecs().register("bestmatchwithurl", csf);
    client.getParams().setParameter(ClientPNames.COOKIE_POLICY, "bestmatchwithurl");

    if (!"".equals(proxy)) {
        StringTokenizer token = new StringTokenizer(proxy, ":");

        if (token.countTokens() == 3) {
            String proxyProtocol = token.nextToken();
            String proxyHost = token.nextToken();
            int proxyPort = Integer.parseInt(token.nextToken());

            HttpHost proxy = new HttpHost(proxyHost, proxyPort, proxyProtocol);
            client.getParams().setParameter(ConnRoutePNames.DEFAULT_PROXY, proxy);
        } else
            System.err.println("Invalid proxy configuration: " + proxy);
    }

    if (auths.size() > 0) {

        for (Auth authObject : auths) {
            client.getCredentialsProvider().setCredentials(
                    new AuthScope(authObject.getScope(), authObject.getPort()),
                    new UsernamePasswordCredentials(authObject.getUserName(), authObject.getPassword()));
        }
    }

    return client;
}

From source file:com.mnxfst.testing.handler.exec.client.PTestPlanExecutorClientCallable.java

/**
 * Initializes the plan executor/*from  w  w w .  j  ava  2 s .  co m*/
 * @param hostname
 * @param port
 * @param uri
 * @param testplan
 */
public PTestPlanExecutorClientCallable(String hostname, int port, String uri, byte[] testplan) {

    this.httpHost = new HttpHost(hostname, port);
    this.postMethod = new HttpPost(uri);

    try {
        String encodedTestplan = new String(testplan, "UTF-8");
        postMethod.setEntity(new StringEntity(PTestPlanExecutorClient.REQUEST_PARAMETER_TESTPLAN + "="
                + URLEncoder.encode(encodedTestplan, "UTF-8"), "UTF-8"));
    } catch (UnsupportedEncodingException e) {
        throw new RuntimeException("Unsupported encoding exception. Error: " + e.getMessage());
    }

    // TODO setting?
    SchemeRegistry schemeRegistry = new SchemeRegistry();
    schemeRegistry.register(new Scheme("http", 80, PlainSocketFactory.getSocketFactory()));
    schemeRegistry.register(new Scheme("https", 443, SSLSocketFactory.getSocketFactory()));

    ThreadSafeClientConnManager threadSafeClientConnectionManager = new ThreadSafeClientConnManager(
            schemeRegistry);
    threadSafeClientConnectionManager.setMaxTotal(20);
    threadSafeClientConnectionManager.setDefaultMaxPerRoute(20);

    this.httpClient = new DefaultHttpClient(threadSafeClientConnectionManager);
}

From source file:com.bigdata.rdf.sail.webapp.client.DefaultClientConnectionManagerFactory.java

public ClientConnectionManager newInstance() {

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

    // Increase max total connection to 200
    cm.setMaxTotal(200);//from   www.  j  a va2s.  c om

    // 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;

}

From source file:org.dataconservancy.ui.it.support.CreateIdApiRequestIT.java

/**
 * Generates {@link #countToGenerate} ids, and insures that they are all unique.  Uses multiple threads to
 * generate the ids.//from w ww  . j a va2 s.  c  om
 *
 * @throws Exception
 */
@Test
public void testGenerateUniqueIdsMultipleThreads() throws Exception {
    long start = Calendar.getInstance().getTimeInMillis();
    // The threads used to generate ids
    Thread threads[] = new Thread[5];

    // HttpClient requires a ThreadSafeClientConnectionManager
    final ThreadSafeClientConnManager conman = new ThreadSafeClientConnManager();
    conman.setMaxTotal(50);
    conman.setDefaultMaxPerRoute(5);
    hc = new DefaultHttpClient(conman);

    assertEquals("The number of threads (" + threads.length
            + ") must evenly divide into thenumber of ids to be " + "generated (" + countToGenerate + ")", 0,
            countToGenerate % threads.length);
    final int generatePerThread = countToGenerate / threads.length;

    // Launch a thread, with each thread being responsible for generating a portion of the total ids
    for (int j = 0; j < threads.length; j++) {
        threads[j] = new Thread(new Runnable() {
            @Override
            public void run() {
                for (int i = 0; i < generatePerThread; i++) {
                    double seed = Math.random();
                    log.trace("Seed is {}", seed);
                    Types t = selectType(seed, Types.values());
                    log.trace("Selected type {} with seed value {}", t, seed);

                    if (log.isDebugEnabled()) {
                        idTypeDistribution.putIfAbsent(t, new AtomicInteger(0));
                        idTypeDistribution.get(t).getAndAdd(1);
                    }

                    try {
                        generatedIds.add(reqFactory.createIdApiRequest(t).execute(hc));
                    } catch (IOException e) {
                        fail(e.getMessage());
                    }
                }
            }
        }, "ID Generation Thread " + j);
        threads[j].start();
    }

    // Wait for threads to stop
    for (int j = 0; j < threads.length; j++) {
        threads[j].join();
    }

    if (log.isDebugEnabled()) {
        StringBuilder sb = new StringBuilder("ID distribution:\n");
        int totalGenerated = 0;
        for (Types t : Types.values()) {
            final Integer typeTotal = idTypeDistribution.get(t).intValue();
            totalGenerated += typeTotal;
            sb.append("Type: ").append(t).append(" Count: ").append(typeTotal).append("\n");
        }
        sb.append("Total generated: ").append(totalGenerated).append("\n");
        sb.append("Unique generated: ").append(generatedIds.size()).append("\n");
        sb.append("Number of threads: ").append(threads.length).append("\n");
        sb.append("Execution time: ").append(Calendar.getInstance().getTimeInMillis() - start).append(" ms\n");
        log.debug(sb.toString());
    }

    // The number of generated IDs (stored in the Set) should equal 'countToGenerate'
    assertEquals("Expected " + countToGenerate + " to be generated, but the Set contained "
            + generatedIds.size() + ".  Some ids may not have been unique.", countToGenerate,
            generatedIds.size());

}

From source file:org.extensiblecatalog.ncip.v2.symphony.SymphonyHttpService.java

/**
 * Creates the default sort of connection manager for this class. In the
 * normal use case, this will be a thread-safe connection manager with
 * <code>maxConnections</code> available connections in total, with the
 * same value available per-<code>HttpRoute</code>.  This represents sensible
 * defaults for the situation where we are only ever connecting to a single
 * Sirsi host./*from  w  w  w . j  av a2  s .  c  o m*/
 * @return
 */
private ClientConnectionManager _createDefaultConnectionManager() {
    ThreadSafeClientConnManager cm = new ThreadSafeClientConnManager();
    cm.setMaxTotal(maxConnections);
    cm.setDefaultMaxPerRoute(maxConnections);
    return cm;
}