Example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager

List of usage examples for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager

Introduction

In this page you can find the example usage for org.apache.commons.httpclient MultiThreadedHttpConnectionManager MultiThreadedHttpConnectionManager.

Prototype

public MultiThreadedHttpConnectionManager() 

Source Link

Usage

From source file:org.apache.forrest.core.reader.HTTPReader.java

public void init() {
    this.client = new HttpClient(new MultiThreadedHttpConnectionManager());
    this.client.getHttpConnectionManager().getParams().setConnectionTimeout(30000);
}

From source file:org.apache.hadoop.hbase.rest.client.Client.java

/**
 * Constructor//from   w  ww.jav  a 2 s  . c  o m
 * @param cluster the cluster definition
 */
public Client(Cluster cluster) {
    this.cluster = cluster;
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams managerParams = manager.getParams();
    managerParams.setConnectionTimeout(2000); // 2 s
    managerParams.setDefaultMaxConnectionsPerHost(10);
    managerParams.setMaxTotalConnections(100);
    extraHeaders = new ConcurrentHashMap<String, String>();
    this.httpClient = new HttpClient(manager);
    HttpClientParams clientParams = httpClient.getParams();
    clientParams.setVersion(HttpVersion.HTTP_1_1);
}

From source file:org.apache.hadoop.hbase.stargate.client.Client.java

/**
 * Constructor/*from  www  .  j a v a2  s  .  c  o  m*/
 * @param cluster the cluster definition
 */
public Client(Cluster cluster) {
    this.cluster = cluster;
    MultiThreadedHttpConnectionManager manager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams managerParams = manager.getParams();
    managerParams.setConnectionTimeout(2000); // 2 s
    managerParams.setDefaultMaxConnectionsPerHost(10);
    managerParams.setMaxTotalConnections(100);
    this.httpClient = new HttpClient(manager);
    HttpClientParams clientParams = httpClient.getParams();
    clientParams.setVersion(HttpVersion.HTTP_1_1);
}

From source file:org.apache.http.client.benchmark.TestHttpClient3.java

public TestHttpClient3() {
    super();/*from www  .ja  va2s  .  c  om*/
    this.mgr = new MultiThreadedHttpConnectionManager();
    this.httpclient = new HttpClient(this.mgr);
    this.httpclient.getParams().setVersion(HttpVersion.HTTP_1_1);
    this.httpclient.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, false);
    this.httpclient.getHttpConnectionManager().getParams().setStaleCheckingEnabled(false);
    this.httpclient.getParams().setSoTimeout(15000);

    HttpMethodRetryHandler retryhandler = new HttpMethodRetryHandler() {

        public boolean retryMethod(final HttpMethod httpmethod, final IOException ex, int count) {
            return false;
        }

    };
    this.httpclient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, retryhandler);
}

From source file:org.apache.ivy.util.url.HttpClientHandler.java

private HttpClient getClient() {
    if (httpClient == null) {
        final MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager();
        httpClient = new HttpClient(connManager);

        Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
            public void run() {
                connManager.shutdown();/*from  w  w  w .j a  v a  2  s  .  c om*/
            }
        }));

        List authPrefs = new ArrayList(3);
        authPrefs.add(AuthPolicy.DIGEST);
        authPrefs.add(AuthPolicy.BASIC);
        authPrefs.add(AuthPolicy.NTLM); // put it at the end to give less priority (IVY-213)
        httpClient.getParams().setParameter(AuthPolicy.AUTH_SCHEME_PRIORITY, authPrefs);

        if (useProxy()) {
            httpClient.getHostConfiguration().setProxy(proxyHost, proxyPort);
            if (useProxyAuthentication()) {
                httpClient.getState().setProxyCredentials(
                        new AuthScope(proxyHost, proxyPort, AuthScope.ANY_REALM),
                        createCredentials(proxyUserName, proxyPasswd));
            }
        }

        // user-agent
        httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT, getUserAgent());

        // authentication
        httpClient.getParams().setParameter(CredentialsProvider.PROVIDER, new IvyCredentialsProvider());
    }

    return httpClient;
}

From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java

public RepositoryServiceImpl(String uri, IdFactory idFactory, NameFactory nameFactory, PathFactory pathFactory,
        QValueFactory qValueFactory) throws RepositoryException {
    if (uri == null || "".equals(uri)) {
        throw new RepositoryException("Invalid repository uri '" + uri + "'.");
    }/*from   w  ww  . j  a v a2 s.  c  om*/

    if (idFactory == null || qValueFactory == null) {
        throw new RepositoryException("IdFactory and QValueFactory may not be null.");
    }
    this.idFactory = idFactory;
    this.nameFactory = nameFactory;
    this.pathFactory = pathFactory;
    this.qValueFactory = qValueFactory;

    try {
        domFactory = DomUtil.BUILDER_FACTORY.newDocumentBuilder().newDocument();
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    }

    try {
        URI repositoryUri = new URI((uri.endsWith("/")) ? uri : uri + "/", true);
        hostConfig = new HostConfiguration();
        hostConfig.setHost(repositoryUri);

        nsCache = new NamespaceCache();
        uriResolver = new URIResolverImpl(repositoryUri, this, domFactory);
        NamePathResolver resolver = new NamePathResolverImpl(nsCache);
        valueFactory = new ValueFactoryQImpl(qValueFactory, resolver);

    } catch (URIException e) {
        throw new RepositoryException(e);
    }
    connectionManager = new MultiThreadedHttpConnectionManager();
}

From source file:org.apache.jackrabbit.spi2dav.RepositoryServiceImpl.java

/**
 * Creates a new instance of this repository service.
 *
 * @param uri The server uri./*from   w w  w.  ja  v  a2  s.  c om*/
 * @param idFactory The id factory.
 * @param nameFactory The name factory.
 * @param pathFactory The path factory.
 * @param qValueFactory The value factory.
 * @param itemInfoCacheSize The size of the item info cache.
 * @param maximumHttpConnections A int &gt;0 defining the maximum number of
 * connections per host to be configured on
 * {@link HttpConnectionManagerParams#setDefaultMaxConnectionsPerHost(int)}.
 * @throws RepositoryException If an error occurs.
 */
public RepositoryServiceImpl(String uri, IdFactory idFactory, NameFactory nameFactory, PathFactory pathFactory,
        QValueFactory qValueFactory, int itemInfoCacheSize, int maximumHttpConnections)
        throws RepositoryException {
    if (uri == null || "".equals(uri)) {
        throw new RepositoryException("Invalid repository uri '" + uri + "'.");
    }

    if (idFactory == null || qValueFactory == null) {
        throw new RepositoryException("IdFactory and QValueFactory may not be null.");
    }
    this.idFactory = idFactory;
    this.nameFactory = nameFactory;
    this.pathFactory = pathFactory;
    this.qValueFactory = qValueFactory;
    this.itemInfoCacheSize = itemInfoCacheSize;

    try {
        URI repositoryUri = computeRepositoryUri(uri);
        hostConfig = new HostConfiguration();
        hostConfig.setHost(repositoryUri);

        nsCache = new NamespaceCache();
        uriResolver = new URIResolverImpl(repositoryUri, this, DomUtil.createDocument());
        NamePathResolver resolver = new NamePathResolverImpl(nsCache);
        valueFactory = new ValueFactoryQImpl(qValueFactory, resolver);

    } catch (URIException e) {
        throw new RepositoryException(e);
    } catch (ParserConfigurationException e) {
        throw new RepositoryException(e);
    }

    connectionManager = new MultiThreadedHttpConnectionManager();
    if (maximumHttpConnections > 0) {
        HttpConnectionManagerParams connectionParams = connectionManager.getParams();
        connectionParams.setDefaultMaxConnectionsPerHost(maximumHttpConnections);
        connectionParams.setMaxTotalConnections(maximumHttpConnections);
    }

    // This configuration of the clients cache assumes that the level of
    // concurrency on this map will be equal to the default number of maximum
    // connections allowed on the httpClient level.
    // TODO: review again
    int concurrencyLevel = MAX_CONNECTIONS_DEFAULT;
    int initialCapacity = MAX_CONNECTIONS_DEFAULT;
    if (maximumHttpConnections > 0) {
        concurrencyLevel = maximumHttpConnections;
        initialCapacity = maximumHttpConnections;
    }
    clients = new ConcurrentHashMap<Object, HttpClient>(concurrencyLevel, .75f, initialCapacity);
}

From source file:org.apache.maven.doxia.linkcheck.validation.OnlineHTTPLinkValidator.java

/** Initialize the HttpClient. */
private void initHttpClient() {
    LOG.debug("A new HttpClient instance is needed ...");

    this.cl = new HttpClient(new MultiThreadedHttpConnectionManager());

    // Default params
    if (this.http.getTimeout() != 0) {
        this.cl.getHttpConnectionManager().getParams().setConnectionTimeout(this.http.getTimeout());
        this.cl.getHttpConnectionManager().getParams().setSoTimeout(this.http.getTimeout());
    }/*w  ww.  j  a va 2s . c  om*/
    this.cl.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

    HostConfiguration hc = new HostConfiguration();

    HttpState state = new HttpState();
    if (StringUtils.isNotEmpty(this.http.getProxyHost())) {
        hc.setProxy(this.http.getProxyHost(), this.http.getProxyPort());

        if (LOG.isDebugEnabled()) {
            LOG.debug("Proxy Host:" + this.http.getProxyHost());
            LOG.debug("Proxy Port:" + this.http.getProxyPort());
        }

        if (StringUtils.isNotEmpty(this.http.getProxyUser()) && this.http.getProxyPassword() != null) {
            if (LOG.isDebugEnabled()) {
                LOG.debug("Proxy User:" + this.http.getProxyUser());
            }

            Credentials credentials;
            if (StringUtils.isNotEmpty(this.http.getProxyNtlmHost())) {
                credentials = new NTCredentials(this.http.getProxyUser(), this.http.getProxyPassword(),
                        this.http.getProxyNtlmHost(), this.http.getProxyNtlmDomain());
            } else {
                credentials = new UsernamePasswordCredentials(this.http.getProxyUser(),
                        this.http.getProxyPassword());
            }

            state.setProxyCredentials(AuthScope.ANY, credentials);
        }
    } else {
        LOG.debug("Not using a proxy");
    }

    this.cl.setHostConfiguration(hc);
    this.cl.setState(state);

    LOG.debug("New HttpClient instance created.");
}

From source file:org.apache.maven.plugin.doap.DoapUtil.java

/**
 * Fetch an URL/* w  w w. j  a  va2 s .c  o m*/
 *
 * @param settings the user settings used to fetch the url with an active proxy, if defined.
 * @param url the url to fetch
 * @throws IOException if any
 * @see #DEFAULT_TIMEOUT
 * @since 1.1
 */
public static void fetchURL(Settings settings, URL url) throws IOException {
    if (url == null) {
        throw new IllegalArgumentException("The url is null");
    }

    if ("file".equals(url.getProtocol())) {
        InputStream in = null;
        try {
            in = url.openStream();
        } finally {
            IOUtil.close(in);
        }

        return;
    }

    // http, https...
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    httpClient.getHttpConnectionManager().getParams().setConnectionTimeout(DEFAULT_TIMEOUT);
    httpClient.getHttpConnectionManager().getParams().setSoTimeout(DEFAULT_TIMEOUT);
    httpClient.getParams().setBooleanParameter(HttpClientParams.ALLOW_CIRCULAR_REDIRECTS, true);

    // Some web servers don't allow the default user-agent sent by httpClient
    httpClient.getParams().setParameter(HttpMethodParams.USER_AGENT,
            "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.0)");

    if (settings != null && settings.getActiveProxy() != null) {
        Proxy activeProxy = settings.getActiveProxy();

        ProxyInfo proxyInfo = new ProxyInfo();
        proxyInfo.setNonProxyHosts(activeProxy.getNonProxyHosts());

        if (StringUtils.isNotEmpty(activeProxy.getHost())
                && !ProxyUtils.validateNonProxyHosts(proxyInfo, url.getHost())) {
            httpClient.getHostConfiguration().setProxy(activeProxy.getHost(), activeProxy.getPort());

            if (StringUtils.isNotEmpty(activeProxy.getUsername()) && activeProxy.getPassword() != null) {
                Credentials credentials = new UsernamePasswordCredentials(activeProxy.getUsername(),
                        activeProxy.getPassword());

                httpClient.getState().setProxyCredentials(AuthScope.ANY, credentials);
            }
        }
    }

    GetMethod getMethod = new GetMethod(url.toString());
    try {
        int status;
        try {
            status = httpClient.executeMethod(getMethod);
        } catch (SocketTimeoutException e) {
            // could be a sporadic failure, one more retry before we give up
            status = httpClient.executeMethod(getMethod);
        }

        if (status != HttpStatus.SC_OK) {
            throw new FileNotFoundException(url.toString());
        }
    } finally {
        getMethod.releaseConnection();
    }
}

From source file:org.apache.ode.axis2.ODEServer.java

private void initHttpConnectionManager() throws ServletException {
    httpConnectionManager = new MultiThreadedHttpConnectionManager();
    // settings may be overridden from ode-axis2.properties using the same properties as HttpClient
    // /!\ If the size of the conn pool is smaller than the size of the thread pool, the thread pool might get starved.
    int max_per_host = Integer.parseInt(_odeConfig.getProperty(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS,
            "" + _odeConfig.getPoolMaxSize()));
    int max_total = Integer.parseInt(_odeConfig.getProperty(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS,
            "" + _odeConfig.getPoolMaxSize()));
    if (__log.isDebugEnabled()) {
        __log.debug(HttpConnectionManagerParams.MAX_HOST_CONNECTIONS + "=" + max_per_host);
        __log.debug(HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS + "=" + max_total);
    }// www . ja  v  a2  s  .co m
    if (max_per_host < 1 || max_total < 1) {
        String errmsg = HttpConnectionManagerParams.MAX_HOST_CONNECTIONS + " and "
                + HttpConnectionManagerParams.MAX_TOTAL_CONNECTIONS + " must be positive integers!";
        __log.error(errmsg);
        throw new ServletException(errmsg);
    }
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(max_per_host);
    httpConnectionManager.getParams().setMaxTotalConnections(max_total);

    // Register the connection manager to a idle check thread
    idleConnectionTimeoutThread = new IdleConnectionTimeoutThread();
    idleConnectionTimeoutThread.setName("Http_Idle_Connection_Timeout_Thread");
    long idleConnectionTimeout = Long
            .parseLong(_odeConfig.getProperty("http.idle.connection.timeout", "30000"));
    long idleConnectionCheckInterval = Long
            .parseLong(_odeConfig.getProperty("http.idle.connection.check.interval", "30000"));

    if (__log.isDebugEnabled()) {
        __log.debug("http.idle.connection.timeout=" + idleConnectionTimeout);
        __log.debug("http.idle.connection.check.interval=" + idleConnectionCheckInterval);
    }
    idleConnectionTimeoutThread.setConnectionTimeout(idleConnectionTimeout);
    idleConnectionTimeoutThread.setTimeoutInterval(idleConnectionCheckInterval);

    idleConnectionTimeoutThread.addConnectionManager(httpConnectionManager);
    idleConnectionTimeoutThread.start();
}