You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by da...@apache.org on 2012/12/15 13:34:28 UTC

svn commit: r1422233 - in /camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty: JettyHttpComponent.java JettyHttpEndpoint.java JettyHttpProducer.java

Author: davsclaus
Date: Sat Dec 15 12:34:26 2012
New Revision: 1422233

URL: http://svn.apache.org/viewvc?rev=1422233&view=rev
Log:
CAMEL-5867: Fixed issue with camel-jetty always creating a http client even if not needed.

Modified:
    camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
    camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java

Modified: camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=1422233&r1=1422232&r2=1422233&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java (original)
+++ camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java Sat Dec 15 12:34:26 2012
@@ -183,10 +183,6 @@ public class JettyHttpComponent extends 
                 
                 ((CamelHttpClient) client).setSSLContext(sslContextParameters.createSSLContext());
             }
-        } else {
-            // Either we use the default one created by the component or we are using
-            // one explicitly set by the end user, either way, we just use it as is.
-            client = getHttpClient();
         }
         // keep the configure parameters for the http client
         for (String key : parameters.keySet()) {
@@ -651,6 +647,19 @@ public class JettyHttpComponent extends 
     }
     
     public CamelHttpClient getNewHttpClient() throws Exception {
+        return createHttpClient(httpClientMinThreads, httpClientMaxThreads, sslContextParameters);
+    }
+
+
+    /**
+     * Creates a new {@link HttpClient} and configures its proxy/thread pool and SSL based on this
+     * component settings.
+     *
+     * @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 static CamelHttpClient createHttpClient(Integer minThreads, Integer maxThreads, SSLContextParameters ssl) throws Exception {
         CamelHttpClient httpClient = new CamelHttpClient();
         httpClient.setConnectorType(HttpClient.CONNECTOR_SELECT_CHANNEL);
 
@@ -663,15 +672,18 @@ public class JettyHttpComponent extends 
             httpClient.setProxy(new Address(host, port));
         }
 
-        // use QueueThreadPool as the default bounded is deprecated (see SMXCOMP-157)
-        if (getHttpClientThreadPool() == null) {
-            QueuedThreadPool qtp = new QueuedThreadPool();
-            if (httpClientMinThreads != null) {
-                qtp.setMinThreads(httpClientMinThreads.intValue());
-            }
-            if (httpClientMaxThreads != null) {
-                qtp.setMaxThreads(httpClientMaxThreads.intValue());
+        // 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());
             // let the thread names indicate they are from the client
             qtp.setName("CamelJettyClient(" + ObjectHelper.getIdentityHashCode(httpClient) + ")");
             try {
@@ -679,14 +691,21 @@ public class JettyHttpComponent extends 
             } catch (Exception e) {
                 throw new RuntimeCamelException("Error starting JettyHttpClient thread pool: " + qtp, e);
             }
-            setHttpClientThreadPool(qtp);
+            httpClient.setThreadPool(qtp);
         }
-        httpClient.setThreadPool(getHttpClientThreadPool());
-        
-        if (this.sslContextParameters != null) {
-            httpClient.setSSLContext(this.sslContextParameters.createSSLContext());
+
+        if (ssl != null) {
+            httpClient.setSSLContext(ssl.createSSLContext());
         }
-        
+
+        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;
     }
 

Modified: camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=1422233&r1=1422232&r2=1422233&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java (original)
+++ camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java Sat Dec 15 12:34:26 2012
@@ -60,7 +60,17 @@ public class JettyHttpEndpoint extends H
 
     @Override
     public Producer createProducer() throws Exception {
-        JettyHttpProducer answer = new JettyHttpProducer(this, getClient());
+        JettyHttpProducer answer = new JettyHttpProducer(this);
+        if (client != null) {
+            // use shared client
+            answer.setSharedClient(client);
+        } else {
+            // create a new client
+            // thread pool min/max from endpoint take precedence over from component
+            Integer min = getComponent().getHttpClientMinThreads();
+            Integer max = getComponent().getHttpClientMaxThreads();
+            answer.setClient(JettyHttpComponent.createHttpClient(min, max, sslContextParameters));
+        }
         answer.setBinding(getJettyBinding());
         if (isSynchronous()) {
             return new SynchronousDelegateProducer(answer);
@@ -170,4 +180,21 @@ public class JettyHttpEndpoint extends H
     public void setSslContextParameters(SSLContextParameters sslContextParameters) {
         this.sslContextParameters = sslContextParameters;
     }
+
+    @Override
+    protected void doStart() throws Exception {
+        if (client != null) {
+            client.start();
+        }
+        super.doStart();
+    }
+
+    @Override
+    protected void doStop() throws Exception {
+        super.doStop();
+        if (client != null) {
+            client.stop();
+        }
+    }
+
 }

Modified: camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java?rev=1422233&r1=1422232&r2=1422233&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java (original)
+++ camel/branches/camel-2.10.x/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpProducer.java Sat Dec 15 12:34:26 2012
@@ -52,13 +52,31 @@ import org.slf4j.LoggerFactory;
  */
 public class JettyHttpProducer extends DefaultProducer implements AsyncProcessor {
     private static final transient Logger LOG = LoggerFactory.getLogger(JettyHttpProducer.class);
-    private final HttpClient client;
+    private HttpClient client;
+    private boolean sharedClient;
     private JettyHttpBinding binding;
 
+    /**
+     * Creates this producer.
+     * <p/>
+     * A client must be set before use, eg either {@link #setClient(org.eclipse.jetty.client.HttpClient)}
+     * or {@link #setSharedClient(org.eclipse.jetty.client.HttpClient)}.
+     *
+     * @param endpoint  the endpoint
+     */
+    public JettyHttpProducer(Endpoint endpoint) {
+        super(endpoint);
+    }
+
+    /**
+     * Creates this producer
+     *
+     * @param endpoint  the endpoint
+     * @param client    the non-shared client to use
+     */
     public JettyHttpProducer(Endpoint endpoint, HttpClient client) {
         super(endpoint);
-        this.client = client;
-        ObjectHelper.notNull(client, "HttpClient", this);
+        setClient(client);
     }
 
     @Override
@@ -240,15 +258,43 @@ public class JettyHttpProducer extends D
         this.binding = binding;
     }
 
+    public HttpClient getClient() {
+        return client;
+    }
+
+    public void setClient(HttpClient client) {
+        this.client = client;
+        this.sharedClient = false;
+    }
+
+    public HttpClient getSharedClient() {
+        if (sharedClient) {
+            return client;
+        } else {
+            return null;
+        }
+    }
+
+    public void setSharedClient(HttpClient sharedClient) {
+        this.client = sharedClient;
+        this.sharedClient = true;
+    }
+
     @Override
     protected void doStart() throws Exception {
-        client.start();
+        // only start non-shared client
+        if (!sharedClient && client != null) {
+            client.start();
+        }
         super.doStart();
     }
 
     @Override
     protected void doStop() throws Exception {
         super.doStop();
-        client.stop();
+        // only stop non-shared client
+        if (!sharedClient && client != null) {
+            client.stop();
+        }
     }
 }