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.ambraproject.service.article.AIArticleClassifier.java

/**
 * @inheritDoc/*from   ww  w . j  a v  a  2  s .c o m*/
 */
public void testThesaurus(final OutputStream os, final String doi, final String thesaurus) throws Exception {
    String full_doi = String.format(XML_URL_FULLDOI, doi);
    String xml = fetchXml(full_doi);
    PrintStream ps = new PrintStream(os);

    Document dom = DocumentBuilderFactoryCreator.createFactory().newDocumentBuilder()
            .parse(new ByteArrayInputStream(xml.getBytes("utf-8")));

    AIArticleClassifier classifier = new AIArticleClassifier();

    ps.println("Content to send to taxonomy server:");
    ps.println("\n\n" + classifier.getCategorizationContent(dom) + "\n\n");

    classifier.setServiceUrl("https://plos.accessinn.com:9136/servlet/dh");
    classifier.setThesaurus(thesaurus);
    classifier.setHttpClient(new HttpClient(new MultiThreadedHttpConnectionManager()));

    List<String> rawOutput = classifier.getRawTerms(dom);

    ps.println("\n\nTerms returned by taxonomy server:");

    for (String s : rawOutput) {

        // Strip out XML wrapping
        s = s.replace("<TERM>", "");
        s = s.replace("</TERM>", "");

        // Replicate the hack in classifyArticle() above, so that this main method only shows what
        // we would actually store in mysql.
        if (!s.startsWith("/Earth sciences/Geography/Locations/")) {
            ps.println(s);
        }
    }
    ps.println("\n\n");
}

From source file:org.apache.abdera.protocol.client.AbderaClient.java

/**
 * Create an AbderaClient instance using the specified Abdera instance and useragent name
 * /*from  ww w .j  ava  2s .  com*/
 * @param abdera
 * @param useragent
 */
public AbderaClient(Abdera abdera, String useragent, Cache cache) {
    this.abdera = abdera;
    this.cache = cache;
    MultiThreadedHttpConnectionManager connManager = new MultiThreadedHttpConnectionManager();
    client = new HttpClient(connManager);
    client.getParams().setParameter(HttpClientParams.USER_AGENT, useragent);
    client.getParams().setBooleanParameter(HttpClientParams.USE_EXPECT_CONTINUE, true);
    client.getParams().setCookiePolicy(CookiePolicy.BROWSER_COMPATIBILITY);
    setAuthenticationSchemeDefaults();
    setMaximumRedirects(DEFAULT_MAX_REDIRECTS);
}

From source file:org.apache.airavata.client.stub.interpretor.WorkflowInterpretorStub.java

/**
 * Constructor that takes in a configContext and useseperate listner
 *///from w w  w  .j a  va 2 s.  com
public WorkflowInterpretorStub(org.apache.axis2.context.ConfigurationContext configurationContext,
        java.lang.String targetEndpoint, boolean useSeparateListener) throws org.apache.axis2.AxisFault {
    // To populate AxisService
    populateAxisService();
    populateFaults();
    MultiThreadedHttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.getParams().setMaxTotalConnections(10000);
    httpConnectionManager.getParams().setMaxConnectionsPerHost(HostConfiguration.ANY_HOST_CONFIGURATION, 100);
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(200);
    HttpClient httpClient = new HttpClient(httpConnectionManager);
    _serviceClient = new org.apache.axis2.client.ServiceClient(configurationContext, _service);

    configurationContext = _serviceClient.getServiceContext().getConfigurationContext();
    configurationContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, true);
    configurationContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
    configurationContext.setProperty(HTTPConstants.AUTO_RELEASE_CONNECTION, true);
    _serviceClient.getOptions().setTo(new org.apache.axis2.addressing.EndpointReference(targetEndpoint));
    _serviceClient.getOptions().setUseSeparateListener(useSeparateListener);
}

From source file:org.apache.axis.transport.http.CommonsHTTPSender.java

protected void initialize() {
    MultiThreadedHttpConnectionManager cm = new MultiThreadedHttpConnectionManager();
    this.clientProperties = CommonsHTTPClientPropertiesFactory.create();
    cm.getParams().setDefaultMaxConnectionsPerHost(clientProperties.getMaximumConnectionsPerHost());
    cm.getParams().setMaxTotalConnections(clientProperties.getMaximumTotalConnections());
    // If defined, set the default timeouts
    // Can be overridden by the MessageContext
    if (this.clientProperties.getDefaultConnectionTimeout() > 0) {
        cm.getParams().setConnectionTimeout(this.clientProperties.getDefaultConnectionTimeout());
    }//from  w  w  w .  j ava 2 s. co m
    if (this.clientProperties.getDefaultSoTimeout() > 0) {
        cm.getParams().setSoTimeout(this.clientProperties.getDefaultSoTimeout());
    }
    this.connectionManager = cm;
}

From source file:org.apache.axis2.transport.http.AbstractHTTPSender.java

protected HttpClient getHttpClient(MessageContext msgContext) {
    ConfigurationContext configContext = msgContext.getConfigurationContext();

    HttpClient httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

    if (httpClient == null) {
        httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
    }/*from   w  ww .j  ava2 s  .  c o  m*/

    if (httpClient != null) {
        return httpClient;
    }

    synchronized (this) {
        httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

        if (httpClient == null) {
            httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
        }

        if (httpClient != null) {
            return httpClient;
        }

        HttpConnectionManager connManager = (HttpConnectionManager) msgContext
                .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        if (connManager == null) {
            connManager = (HttpConnectionManager) msgContext
                    .getProperty(HTTPConstants.MUTTITHREAD_HTTP_CONNECTION_MANAGER);
        }
        if (connManager == null) {
            // reuse HttpConnectionManager
            synchronized (configContext) {
                connManager = (HttpConnectionManager) configContext
                        .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
                if (connManager == null) {
                    log.trace("Making new ConnectionManager");
                    connManager = new MultiThreadedHttpConnectionManager();
                    configContext.setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, connManager);
                }
            }
        }
        /*
         * Create a new instance of HttpClient since the way
         * it is used here it's not fully thread-safe.
         */
        httpClient = new HttpClient(connManager);

        // Set the default timeout in case we have a connection pool starvation to 30sec
        httpClient.getParams().setConnectionManagerTimeout(30000);

        // Get the timeout values set in the runtime
        initializeTimeouts(msgContext, httpClient);

        return httpClient;
    }
}

From source file:org.apache.axis2.transport.http.CommonsHTTPTransportSender.java

public void init(ConfigurationContext confContext, TransportOutDescription transportOut) throws AxisFault {
    this.transportOut = transportOut;

    // <parameter name="PROTOCOL">HTTP/1.0</parameter> or
    // <parameter name="PROTOCOL">HTTP/1.1</parameter> is
    // checked//  ww  w .j a va2  s. c  o  m
    Parameter version = transportOut.getParameter(HTTPConstants.PROTOCOL_VERSION);
    if (version != null) {
        if (HTTPConstants.HEADER_PROTOCOL_11.equals(version.getValue())) {
            defaultHttpVersion = HTTPConstants.HEADER_PROTOCOL_11;

            Parameter transferEncoding = transportOut.getParameter(HTTPConstants.HEADER_TRANSFER_ENCODING);

            if ((transferEncoding != null)
                    && HTTPConstants.HEADER_TRANSFER_ENCODING_CHUNKED.equals(transferEncoding.getValue())) {
                defaultChunked = true;
            }
        } else if (HTTPConstants.HEADER_PROTOCOL_10.equals(version.getValue())) {
            defaultHttpVersion = HTTPConstants.HEADER_PROTOCOL_10;
        } else {
            throw new AxisFault("Parameter " + HTTPConstants.PROTOCOL_VERSION
                    + " Can have values only HTTP/1.0 or HTTP/1.1");
        }
    }

    // Get the timeout values from the configuration
    try {
        Parameter tempSoTimeoutParam = transportOut.getParameter(HTTPConstants.SO_TIMEOUT);
        Parameter tempConnTimeoutParam = transportOut.getParameter(HTTPConstants.CONNECTION_TIMEOUT);

        if (tempSoTimeoutParam != null) {
            soTimeout = Integer.parseInt((String) tempSoTimeoutParam.getValue());
        }

        if (tempConnTimeoutParam != null) {
            connectionTimeout = Integer.parseInt((String) tempConnTimeoutParam.getValue());
        }
    } catch (NumberFormatException nfe) {

        // If there's a problem log it and use the default values
        log.error("Invalid timeout value format: not a number", nfe);
    }

    Parameter cacheHttpClientParam = transportOut.getParameter(HTTPConstants.CACHE_HTTP_CLIENT);
    if (cacheHttpClientParam != null && "true".equals(cacheHttpClientParam.getValue())) {

        Parameter defaultMaxConnectionsPerHostParam = transportOut
                .getParameter(HTTPConstants.DEFAULT_MAX_CONNECTIONS_PER_HOST);

        Parameter totalConnectionsParam = transportOut.getParameter(HTTPConstants.MAX_TOTAL_CONNECTIONS);

        HttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();

        if (defaultMaxConnectionsPerHostParam != null && defaultMaxConnectionsPerHostParam.getValue() != null) {

            try {
                int defaultMaxConnectionsPerHost = Integer
                        .parseInt((String) defaultMaxConnectionsPerHostParam.getValue());
                connectionManager.getParams().setDefaultMaxConnectionsPerHost(defaultMaxConnectionsPerHost);
            } catch (NumberFormatException nfe) {
                // If there's a problem log it and use the default values
                log.error("Invalid defaultMaxConnectionsPerHost " + "value format: not a number", nfe);
            }
        } else {
            connectionManager.getParams().setDefaultMaxConnectionsPerHost(100);
        }

        if (totalConnectionsParam != null && totalConnectionsParam.getValue() != null) {
            try {
                int totalConnections = Integer.parseInt((String) totalConnectionsParam.getValue());
                connectionManager.getParams().setMaxTotalConnections(totalConnections);
            } catch (NumberFormatException nfe) {
                // If there's a problem log it and use the default values
                log.error("Invalid totalConnections value format: not a number", nfe);
            }
        } else {
            connectionManager.getParams().setMaxTotalConnections(1000);
        }

        HttpClient httpClient = new HttpClient(connectionManager);

        confContext.setProperty(HTTPConstants.REUSE_HTTP_CLIENT, "true");
        confContext.setProperty(HTTPConstants.CACHED_HTTP_CLIENT, httpClient);
    }
}

From source file:org.apache.axis2.transport.http.impl.httpclient3.HTTPSenderImpl.java

protected HttpClient getHttpClient(MessageContext msgContext) {
    ConfigurationContext configContext = msgContext.getConfigurationContext();

    HttpClient httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

    if (httpClient == null) {
        httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
    }/*from   w w  w . j a  va2s  . c o m*/

    if (httpClient != null) {
        return httpClient;
    }

    synchronized (this) {
        httpClient = (HttpClient) msgContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);

        if (httpClient == null) {
            httpClient = (HttpClient) configContext.getProperty(HTTPConstants.CACHED_HTTP_CLIENT);
        }

        if (httpClient != null) {
            return httpClient;
        }

        HttpConnectionManager connManager = (HttpConnectionManager) msgContext
                .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        if (connManager == null) {
            connManager = (HttpConnectionManager) msgContext
                    .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
        }
        if (connManager == null) {
            // reuse HttpConnectionManager
            synchronized (configContext) {
                connManager = (HttpConnectionManager) configContext
                        .getProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER);
                if (connManager == null) {
                    log.trace("Making new ConnectionManager");
                    connManager = new MultiThreadedHttpConnectionManager();
                    configContext.setProperty(HTTPConstants.MULTITHREAD_HTTP_CONNECTION_MANAGER, connManager);
                }
            }
        }
        /*
         * Create a new instance of HttpClient since the way it is used here
         * it's not fully thread-safe.
         */
        httpClient = new HttpClient(connManager);

        // Set the default timeout in case we have a connection pool
        // starvation to 30sec
        httpClient.getParams().setConnectionManagerTimeout(30000);

        // Get the timeout values set in the runtime
        initializeTimeouts(msgContext, httpClient);

        return httpClient;
    }
}

From source file:org.apache.camel.component.http.HttpComponent.java

@Override
protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object> parameters)
        throws Exception {
    String addressUri = "http://" + remaining;
    if (uri.startsWith("https:")) {
        addressUri = "https://" + remaining;
    }/*from   w  w w  .ja va 2s .  c om*/
    Map<String, Object> httpClientParameters = new HashMap<String, Object>(parameters);
    // must extract well known parameters before we create the endpoint
    // TODO cmueller: remove the "httpBindingRef" look up in Camel 3.0
    HttpBinding binding = resolveAndRemoveReferenceParameter(parameters, "httpBindingRef", HttpBinding.class);
    if (binding == null) {
        // try without ref
        binding = resolveAndRemoveReferenceParameter(parameters, "httpBinding", HttpBinding.class);
    }
    String proxyHost = getAndRemoveParameter(parameters, "proxyHost", String.class);
    Integer proxyPort = getAndRemoveParameter(parameters, "proxyPort", Integer.class);
    String authMethodPriority = getAndRemoveParameter(parameters, "authMethodPriority", String.class);
    HeaderFilterStrategy headerFilterStrategy = resolveAndRemoveReferenceParameter(parameters,
            "headerFilterStrategy", HeaderFilterStrategy.class);
    UrlRewrite urlRewrite = resolveAndRemoveReferenceParameter(parameters, "urlRewrite", UrlRewrite.class);
    // http client can be configured from URI options
    HttpClientParams clientParams = new HttpClientParams();
    IntrospectionSupport.setProperties(clientParams, parameters, "httpClient.");
    // validate that we could resolve all httpClient. parameters as this component is lenient
    validateParameters(uri, parameters, "httpClient.");
    // http client can be configured from URI options
    HttpConnectionManagerParams connectionManagerParams = new HttpConnectionManagerParams();
    // setup the httpConnectionManagerParams
    IntrospectionSupport.setProperties(connectionManagerParams, parameters, "httpConnectionManager.");
    validateParameters(uri, parameters, "httpConnectionManager.");
    // make sure the component httpConnectionManager is take effect
    HttpConnectionManager thisHttpConnectionManager = httpConnectionManager;
    if (thisHttpConnectionManager == null) {
        // only set the params on the new created http connection manager
        thisHttpConnectionManager = new MultiThreadedHttpConnectionManager();
        thisHttpConnectionManager.setParams(connectionManagerParams);
    }
    // create the configurer to use for this endpoint (authMethods contains the used methods created by the configurer)
    final Set<AuthMethod> authMethods = new LinkedHashSet<AuthMethod>();
    HttpClientConfigurer configurer = createHttpClientConfigurer(parameters, authMethods);
    addressUri = UnsafeUriCharactersEncoder.encodeHttpURI(addressUri);
    URI endpointUri = URISupport.createRemainingURI(new URI(addressUri), httpClientParameters);

    // create the endpoint and connectionManagerParams already be set
    HttpEndpoint endpoint = new HttpEndpoint(endpointUri.toString(), this, clientParams,
            thisHttpConnectionManager, configurer);

    if (headerFilterStrategy != null) {
        endpoint.setHeaderFilterStrategy(headerFilterStrategy);
    } else {
        setEndpointHeaderFilterStrategy(endpoint);
    }
    if (urlRewrite != null) {
        // let CamelContext deal with the lifecycle of the url rewrite
        // this ensures its being shutdown when Camel shutdown etc.
        getCamelContext().addService(urlRewrite);
        endpoint.setUrlRewrite(urlRewrite);
    }

    // prefer to use endpoint configured over component configured
    if (binding == null) {
        // fallback to component configured
        binding = getHttpBinding();
    }
    if (binding != null) {
        endpoint.setBinding(binding);
    }
    if (proxyHost != null) {
        endpoint.setProxyHost(proxyHost);
        endpoint.setProxyPort(proxyPort);
    } else if (httpConfiguration != null) {
        endpoint.setProxyHost(httpConfiguration.getProxyHost());
        endpoint.setProxyPort(httpConfiguration.getProxyPort());
    }
    if (authMethodPriority != null) {
        endpoint.setAuthMethodPriority(authMethodPriority);
    } else if (httpConfiguration != null && httpConfiguration.getAuthMethodPriority() != null) {
        endpoint.setAuthMethodPriority(httpConfiguration.getAuthMethodPriority());
    } else {
        // no explicit auth method priority configured, so use convention over configuration
        // and set priority based on auth method
        if (!authMethods.isEmpty()) {
            authMethodPriority = CollectionHelper.collectionAsCommaDelimitedString(authMethods);
            endpoint.setAuthMethodPriority(authMethodPriority);
        }
    }
    setProperties(endpoint, parameters);
    // restructure uri to be based on the parameters left as we dont want to include the Camel internal options
    URI httpUri = URISupport.createRemainingURI(new URI(addressUri), parameters);

    // validate http uri that end-user did not duplicate the http part that can be a common error
    String part = httpUri.getSchemeSpecificPart();
    if (part != null) {
        part = part.toLowerCase();
        if (part.startsWith("//http//") || part.startsWith("//https//") || part.startsWith("//http://")
                || part.startsWith("//https://")) {
            throw new ResolveEndpointFailedException(uri,
                    "The uri part is not configured correctly. You have duplicated the http(s) protocol.");
        }
    }
    endpoint.setHttpUri(httpUri);
    return endpoint;
}

From source file:org.apache.camel.component.jetty.MultiThreadedHttpGetTest.java

@Test
public void testHttpGetWithoutConversion() throws Exception {

    // This is needed as by default there are 2 parallel
    // connections to some host and there is nothing that
    // closes the http connection here.
    // Need to set the httpConnectionManager 
    HttpConnectionManager httpConnectionManager = new MultiThreadedHttpConnectionManager();
    httpConnectionManager.getParams().setDefaultMaxConnectionsPerHost(5);
    context.getComponent("http", HttpComponent.class).setHttpConnectionManager(httpConnectionManager);

    String endpointName = "seda:withoutConversion?concurrentConsumers=5";
    sendMessagesTo(endpointName, 5);/*from  w w w . j  a v  a 2  s.c  om*/
}

From source file:org.apache.commons.httpclient.demo.MultiThreadedHttpDemo.java

public static void main(String[] args) {
    MultiThreadedHttpConnectionManager connectionManager = new MultiThreadedHttpConnectionManager();
    HttpClient client = new HttpClient(connectionManager);
    ///*from w ww.j a v a 2  s  .c  om*/
    //client.getHostConfiguration().setProxy("90.0.12.21",808);
    //GETHTTPSURLhttphttps
    HttpMethod method = new GetMethod("http://java.sun.com");
    //POST
    //HttpMethod method = new PostMethod("http://java.sun.com");
    try {
        client.executeMethod(method);
    } catch (IOException ex) {
    }
    //
    System.out.println("===================================");
    System.out.println("");
    System.out.println(method.getStatusLine());
    System.out.println("===================================");
    //
    System.out.println("===================================");
    System.out.println(":");
    try {
        System.out.println(method.getResponseBodyAsString());
    } catch (IOException ex1) {
    }
    System.out.println("===================================");
    //
    method.releaseConnection();
}