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:com.osafe.services.MelissaDataHelper.java

private static HttpClient getHttpClient() {
    HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
    return httpClient;
}

From source file:com.trendmicro.hdfs.webdav.test.MiniClusterTestUtil.java

public HttpClient getClient() {
    HostConfiguration hostConfig = new HostConfiguration();
    hostConfig.setHost("localhost");
    HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpConnectionManagerParams params = new HttpConnectionManagerParams();
    params.setMaxConnectionsPerHost(hostConfig, 100);
    connectionManager.setParams(params);
    HttpClient client = new HttpClient(connectionManager);
    client.setHostConfiguration(hostConfig);
    return client;
}

From source file:ac.elements.io.Signature.java

/**
 * Configure HttpClient with set of defaults as well as configuration from
 * AmazonEC2Config instance./*from w  w w. j  a v a2 s  . c om*/
 * 
 * @return the http client
 */
private static HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, USER_AGENT);
    httpClientParams.setParameter(HttpMethodParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > MAX_RETRY_ERROR) {
                log.warn("Maximum Number of Retry attempts " + "reached, will not retry");
                return false;
            }
            log.warn("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.warn("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.warn("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.warn("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.warn("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(MAX_CONNECTIONS);
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, MAX_CONNECTIONS);

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    // if (config.isSetProxyHost() && config.isSetProxyPort()) {
    // log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() +
    // "Proxy Port: " + config.getProxyPort() );
    // hostConfiguration.setProxy(config.getProxyHost(),
    // config.getProxyPort());
    // if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
    // httpClient.getState().setProxyCredentials (new AuthScope(
    // config.getProxyHost(),
    // config.getProxyPort()),
    // new UsernamePasswordCredentials(
    // config.getProxyUsername(),
    // config.getProxyPassword()));
    //        
    // }
    // }
    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:com.cordys.coe.ac.httpconnector.config.ServerConnection.java

/**
 * @see com.cordys.coe.ac.httpconnector.config.IServerConnection#open()
 *//*from  w w  w.j av  a2  s .  co m*/
@Override
public void open() {
    m_connManager = new MultiThreadedHttpConnectionManager();
    m_client = new HttpClient(m_connManager);

    // Set the host information in the client.
    HostConfiguration hostConfig = m_client.getHostConfiguration();
    String protoName = m_url.getProtocol();
    String hostName = m_url.getHost();
    int port;

    port = m_url.getPort();

    if (port <= 0) {
        port = m_url.getDefaultPort();
    }

    Protocol protocol;
    if (HTTPS_PROTOCOL_PREFIX.equals(protoName)) {
        if (m_checkServerCertificate) {
            if (LOG.isInfoEnabled()) {
                LOG.info(Messages.USING_CORDYS_SOCKET_FACTORY);
            }
            protocol = new Protocol(HTTPS_PROTOCOL_PREFIX, createSocketFactory(), port);

        } else {
            if (LOG.isInfoEnabled()) {
                LOG.info(Messages.USING_DUMMY_SOCKET_FACTORY);
            }
            protocol = new Protocol("https", (ProtocolSocketFactory) new DummySSLSocketFactory(), port);

        }
    } else {
        protocol = Protocol.getProtocol(protoName);
    }
    hostConfig.setHost(hostName, port, protocol);

    if (m_proxyHost != null) {
        hostConfig.setProxy(m_proxyHost, m_proxyPort);

        if (m_proxyUsername != null) {
            Credentials defaultcreds = new UsernamePasswordCredentials(m_proxyUsername, m_proxyPassword);

            m_client.getState().setProxyCredentials(new AuthScope(hostName, port, AuthScope.ANY_REALM),
                    defaultcreds);
        }
    }

    if (m_username != null) {
        if (m_authenticateAlways) {
            m_client.getParams().setAuthenticationPreemptive(true);
        }

        Credentials defaultcreds = new UsernamePasswordCredentials(m_username, m_password);

        m_client.getState().setCredentials(new AuthScope(hostName, port, AuthScope.ANY_REALM), defaultcreds);
    }
}

From source file:com.zimbra.common.util.ZimbraHttpConnectionManager.java

private ZimbraHttpConnectionManager(String name, ZimbraConnMgrParams zimbraConnMgrParams) {
    this.name = name;
    this.zimbraConnMgrParams = zimbraConnMgrParams;

    this.httpConnMgr = new MultiThreadedHttpConnectionManager();
    this.httpConnMgr.setParams(this.zimbraConnMgrParams.getConnMgrParams());
    this.defaultHttpClient = createHttpClient();

    // Instantiate the reaper object.  
    // Note: Reaper thread is not started until ZimbraHttpConnectionManager.startReaperThread 
    // is called.
    this.idleReaper = new IdleReaper(this);
}

From source file:com.bigdata.rdf.sail.remoting.GraphRepositoryClient.java

private HttpClient getHttpClient() {
    if (httpClient == null) {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());

        httpClient.getParams().setParameter(HttpMethodParams.SO_TIMEOUT, new Integer(300000));
        // httpClient.getParams().setParameter(HttpMethodParams.HEAD_BODY_CHECK_TIMEOUT,
        // new Integer(300000));
        httpClient.getParams().setParameter(HttpMethodParams.RETRY_HANDLER, new NeverRetryHandler());

        httpClient.getParams().setParameter(HttpClientParams.CONNECTION_MANAGER_TIMEOUT, new Long(300000));

        httpClient.getParams().setParameter(HttpConnectionParams.CONNECTION_TIMEOUT, new Integer(300000));
        httpClient.getParams().setParameter(HttpConnectionParams.SO_TIMEOUT, new Integer(300000));
    }// w w w  . j a  v a  2 s .c o  m
    if (proxyHost != null) {
        httpClient.getHostConfiguration().setProxyHost(proxyHost);
    }
    return httpClient;
}

From source file:com.cloud.utils.UriUtils.java

public static void checkUrlExistence(String url) {
    if (url.toLowerCase().startsWith("http") || url.toLowerCase().startsWith("https")) {
        HttpClient httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        HeadMethod httphead = new HeadMethod(url);
        try {/*from   www. j ava2 s. c  o m*/
            if (httpClient.executeMethod(httphead) != HttpStatus.SC_OK) {
                throw new IllegalArgumentException("Invalid URL: " + url);
            }
        } catch (HttpException hte) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        } catch (IOException ioe) {
            throw new IllegalArgumentException("Cannot reach URL: " + url);
        }
    }
}

From source file:autohit.call.modules.SimpleHttpModule.java

/**
 * Start method for an HTTP session. It will set the target address for the client, as well as
 * clearing any state./*from  w w  w. j  a v  a 2s  .c om*/
 * 
 * @param addr
 *            the address. Do not include protocol, but you may add port
 *            (ie. "www.goatland.com:80").
 * @throws CallException
 */
private void start(String addr, int port) throws CallException {

    try {
        httpClient = new HttpClient(new MultiThreadedHttpConnectionManager());
        creds = null;
        HttpState initialState = new HttpState();
        initialState.setCookiePolicy(CookiePolicy.COMPATIBILITY);
        httpClient.setState(initialState);
        httpClient.setConnectionTimeout(DEFAULT_TIMEOUT);
        httpClient.getHostConfiguration().setHost(addr, port, "http");

    } catch (Exception ex) {
        throw new CallException(
                "Serious fault while creating session with start method.  Session is not valid.  error="
                        + ex.getMessage(),
                CallException.CODE_MODULE_FAULT, ex);
    }

    // NO CODE AFTER THIS!
    started = true;
}

From source file:com.amazonaws.elasticmapreduce.AmazonElasticMapReduceClient.java

/**
 * Configure HttpClient with set of defaults as well as configuration
 * from AmazonElasticMapReduceConfig instance
 *
 *///from   w ww  .j ava2s  . co  m
private HttpClient configureHttpClient() {

    /* Set http client parameters */
    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setParameter(HttpMethodParams.USER_AGENT, config.getUserAgent());
    httpClientParams.setParameter(HttpClientParams.RETRY_HANDLER, new HttpMethodRetryHandler() {

        public boolean retryMethod(HttpMethod method, IOException exception, int executionCount) {
            if (executionCount > 3) {
                log.debug("Maximum Number of Retry attempts reached, will not retry");
                return false;
            }
            log.debug("Retrying request. Attempt " + executionCount);
            if (exception instanceof NoHttpResponseException) {
                log.debug("Retrying on NoHttpResponseException");
                return true;
            }
            if (exception instanceof InterruptedIOException) {
                log.debug("Will not retry on InterruptedIOException", exception);
                return false;
            }
            if (exception instanceof UnknownHostException) {
                log.debug("Will not retry on UnknownHostException", exception);
                return false;
            }
            if (!method.isRequestSent()) {
                log.debug("Retrying on failed sent request");
                return true;
            }
            return false;
        }
    });

    /* Set host configuration */
    HostConfiguration hostConfiguration = new HostConfiguration();

    /* Set connection manager parameters */
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    connectionManagerParams.setConnectionTimeout(50000);
    connectionManagerParams.setSoTimeout(50000);
    connectionManagerParams.setStaleCheckingEnabled(true);
    connectionManagerParams.setTcpNoDelay(true);
    connectionManagerParams.setMaxTotalConnections(config.getMaxConnections());
    connectionManagerParams.setMaxConnectionsPerHost(hostConfiguration, config.getMaxConnections());

    /* Set connection manager */
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    connectionManager.setParams(connectionManagerParams);

    /* Set http client */
    httpClient = new HttpClient(httpClientParams, connectionManager);

    /* Set proxy if configured */
    if (config.isSetProxyHost() && config.isSetProxyPort()) {
        log.info("Configuring Proxy. Proxy Host: " + config.getProxyHost() + "Proxy Port: "
                + config.getProxyPort());
        hostConfiguration.setProxy(config.getProxyHost(), config.getProxyPort());
        if (config.isSetProxyUsername() && config.isSetProxyPassword()) {
            httpClient.getState().setProxyCredentials(
                    new AuthScope(config.getProxyHost(), config.getProxyPort()),
                    new UsernamePasswordCredentials(config.getProxyUsername(), config.getProxyPassword()));

        }
    }

    httpClient.setHostConfiguration(hostConfiguration);
    return httpClient;
}

From source file:de.ingrid.mdek.quartz.jobs.URLValidatorJob.java

@Override
protected void executeInternal(JobExecutionContext jobExecutionContext) throws JobExecutionException {

    ExecutorService executorService = Executors.newFixedThreadPool(NUM_THREADS);
    JobDataMap mergedJobDataMap = jobExecutionContext.getMergedJobDataMap();
    Map<String, URLState> urlMap = (Map<String, URLState>) mergedJobDataMap.get(URL_MAP);
    List<URLValidator> validatorTasks = new ArrayList<URLValidator>(urlMap.size());

    Map<String, URLState> capabilitiesMap = (Map<String, URLState>) mergedJobDataMap.get(CAP_URL_MAP);
    List<CapabilitiesValidator> capabilitiesValidatorTasks = new ArrayList<CapabilitiesValidator>(
            capabilitiesMap.size());//from   ww w .  j  a v  a  2 s  .c  om

    HttpClientParams httpClientParams = new HttpClientParams();
    httpClientParams.setConnectionManagerTimeout(0);
    httpClientParams.setSoTimeout(SOCKET_TIMEOUT);
    HttpConnectionParams httpParams = new HttpConnectionParams();
    httpParams.setConnectionTimeout(CONNECTION_TIMEOUT);
    httpClientParams.setDefaults(httpParams);
    HttpClient httpClient = new HttpClient(httpClientParams, new MultiThreadedHttpConnectionManager());
    if (System.getProperty("http.proxyHost") != null && System.getProperty("http.proxyPort") != null) {
        httpClient.getHostConfiguration().setProxy(System.getProperty("http.proxyHost"),
                Integer.parseInt(System.getProperty("http.proxyPort")));
    }
    for (URLState urlState : urlMap.values()) {
        validatorTasks.add(new URLValidator(httpClient, urlState));
    }
    for (URLState urlState : capabilitiesMap.values()) {
        capabilitiesValidatorTasks.add(new CapabilitiesValidator(httpClient, urlState));
    }

    log.debug("Starting url validation...");
    long startTime = System.currentTimeMillis();
    List<Future<URLState>> resultFutureList = new ArrayList<Future<URLState>>();
    for (URLValidator validator : validatorTasks) {
        resultFutureList.add(executorService.submit(validator));
    }
    for (CapabilitiesValidator validator : capabilitiesValidatorTasks) {
        resultFutureList.add(executorService.submit(validator));
    }

    for (Future<URLState> future : resultFutureList) {
        try {
            if (!cancelJob) {
                future.get();

            } else {
                log.debug("forcing shutdown of executor service...");
                executorService.shutdownNow();
                break;
            }

        } catch (Exception ex) {
            log.debug("Exception while fetching result from future.", ex);
        }
    }
    long endTime = System.currentTimeMillis();
    log.debug("URL Validation took " + (endTime - startTime) + " ms.");

    executorService.shutdown();

    // Only store if job was not cancelled
    if (!cancelJob) {
        Map<String, List<URLObjectReference>> map = new HashMap<String, List<URLObjectReference>>();
        map.put(MdekKeys.URL_RESULT, (List<URLObjectReference>) mergedJobDataMap.get(URL_OBJECT_REFERENCES));
        map.put(MdekKeys.CAP_RESULT, (List<URLObjectReference>) mergedJobDataMap.get(CAPABILITIES_REFERENCES));
        jobExecutionContext.setResult(map);
    }
}