Search in sources :

Example 1 with HttpClientTransport

use of org.eclipse.jetty.client.HttpClientTransport in project camel by apache.

the class JettyHttpComponent method createHttpClient.

/**
     * Creates a new {@link HttpClient} and configures its proxy/thread pool and SSL based on this
     * component settings.
     *
     * @param endpoint   the instance of JettyHttpEndpoint
     * @param minThreads optional minimum number of threads in client thread pool
     * @param maxThreads optional maximum number of threads in client thread pool
     * @param ssl        option SSL parameters
     */
public CamelHttpClient createHttpClient(JettyHttpEndpoint endpoint, Integer minThreads, Integer maxThreads, SSLContextParameters ssl) throws Exception {
    SslContextFactory sslContextFactory = createSslContextFactory(ssl);
    HttpClientTransport transport = createHttpClientTransport(maxThreads);
    CamelHttpClient httpClient = createCamelHttpClient(transport, sslContextFactory);
    CamelContext context = endpoint.getCamelContext();
    if (context != null && ObjectHelper.isNotEmpty(context.getProperty("http.proxyHost")) && ObjectHelper.isNotEmpty(context.getProperty("http.proxyPort"))) {
        String host = context.getProperty("http.proxyHost");
        int port = Integer.parseInt(context.getProperty("http.proxyPort"));
        LOG.debug("CamelContext properties http.proxyHost and http.proxyPort detected. Using http proxy host: {} port: {}", host, port);
        httpClient.setProxy(host, port);
    }
    if (ObjectHelper.isNotEmpty(endpoint.getProxyHost()) && endpoint.getProxyPort() > 0) {
        String host = endpoint.getProxyHost();
        int port = endpoint.getProxyPort();
        LOG.debug("proxyHost and proxyPort options detected. Using http proxy host: {} port: {}", host, port);
        httpClient.setProxy(host, port);
    }
    // must have both min and max
    if (minThreads != null || maxThreads != null) {
        // must have both options
        if (minThreads == null || maxThreads == null) {
            throw new IllegalArgumentException("Both min and max thread pool sizes must be provided.");
        }
        // use QueueThreadPool as the default bounded is deprecated (see SMXCOMP-157)
        QueuedThreadPool qtp = new QueuedThreadPool();
        qtp.setMinThreads(minThreads.intValue());
        qtp.setMaxThreads(maxThreads.intValue());
        // and we want to use daemon threads
        qtp.setDaemon(true);
        // let the thread names indicate they are from the client
        qtp.setName("CamelJettyClient(" + ObjectHelper.getIdentityHashCode(httpClient) + ")");
        httpClient.setThreadPoolOrExecutor(qtp);
    }
    if (LOG.isDebugEnabled()) {
        if (minThreads != null) {
            LOG.debug("Created HttpClient with thread pool {}-{} -> {}", new Object[] { minThreads, maxThreads, httpClient });
        } else {
            LOG.debug("Created HttpClient with default thread pool size -> {}", httpClient);
        }
    }
    return httpClient;
}
Also used : CamelContext(org.apache.camel.CamelContext) SslContextFactory(org.eclipse.jetty.util.ssl.SslContextFactory) HttpClientTransport(org.eclipse.jetty.client.HttpClientTransport) QueuedThreadPool(org.eclipse.jetty.util.thread.QueuedThreadPool) Endpoint(org.apache.camel.Endpoint) HttpCommonEndpoint(org.apache.camel.http.common.HttpCommonEndpoint)

Aggregations

CamelContext (org.apache.camel.CamelContext)1 Endpoint (org.apache.camel.Endpoint)1 HttpCommonEndpoint (org.apache.camel.http.common.HttpCommonEndpoint)1 HttpClientTransport (org.eclipse.jetty.client.HttpClientTransport)1 SslContextFactory (org.eclipse.jetty.util.ssl.SslContextFactory)1 QueuedThreadPool (org.eclipse.jetty.util.thread.QueuedThreadPool)1