camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject [camel] 04/12: Add connection pool to yql component
Date Mon, 06 Nov 2017 10:58:38 GMT
This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git

commit b490da3ef9aca0713b458f8c7b333659937ad284
Author: Carl-Philipp Harmant <cp.harmant@gmail.com>
AuthorDate: Tue Oct 31 15:55:45 2017 -0500

    Add connection pool to yql component
---
 .../apache/camel/component/yql/YqlComponent.java   | 30 +++++++++++++++++++++-
 .../apache/camel/component/yql/YqlEndpoint.java    | 25 +++++++++++++++++-
 .../apache/camel/component/yql/YqlProducer.java    |  6 ++---
 .../camel/component/yql/client/YqlClient.java      | 10 +++++---
 4 files changed, 63 insertions(+), 8 deletions(-)

diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
index 6c64a77..02b778f 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlComponent.java
@@ -21,19 +21,47 @@ import org.apache.camel.Endpoint;
 import org.apache.camel.component.yql.configuration.YqlConfiguration;
 import org.apache.camel.component.yql.configuration.YqlConfigurationValidator;
 import org.apache.camel.impl.DefaultComponent;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 
 public class YqlComponent extends DefaultComponent {
 
+    private HttpClientConnectionManager localConnectionManager;
+
     @Override
     protected Endpoint createEndpoint(final String uri, final String remaining, final Map<String,
Object> parameters) throws Exception {
         final YqlConfiguration configuration = new YqlConfiguration();
         configuration.setQuery(remaining);
 
-        final Endpoint endpoint = new YqlEndpoint(uri, this, configuration);
+        final HttpClientConnectionManager connectionManager = createConnectionManager();
+
+        final Endpoint endpoint = new YqlEndpoint(uri, this, configuration, connectionManager);
 
         YqlConfigurationValidator.validateProperties(configuration);
 
         setProperties(configuration, parameters);
         return endpoint;
     }
+
+    @Override
+    protected void doStop() throws Exception {
+      if (localConnectionManager != null) {
+        localConnectionManager.shutdown();
+      }
+    }
+
+    public void setConnectionManager(final HttpClientConnectionManager connectionManager){
+      this.localConnectionManager = connectionManager;
+    }
+
+    private HttpClientConnectionManager createConnectionManager(){
+      if (localConnectionManager != null) {
+        return localConnectionManager;
+      }
+      final PoolingHttpClientConnectionManager connectionManager = new PoolingHttpClientConnectionManager();
+      connectionManager.setMaxTotal(200);
+      connectionManager.setDefaultMaxPerRoute(20);
+      setConnectionManager(connectionManager);
+      return connectionManager;
+    }
 }
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlEndpoint.java
b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlEndpoint.java
index af283e4..90384c2 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlEndpoint.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlEndpoint.java
@@ -23,16 +23,23 @@ import org.apache.camel.component.yql.configuration.YqlConfiguration;
 import org.apache.camel.impl.DefaultEndpoint;
 import org.apache.camel.spi.UriEndpoint;
 import org.apache.camel.spi.UriParam;
+import org.apache.http.conn.HttpClientConnectionManager;
+import org.apache.http.impl.client.CloseableHttpClient;
+import org.apache.http.impl.client.HttpClients;
+import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
 
 @UriEndpoint(firstVersion = "2.21.0", scheme = "yql", title = "Yahoo Query Language", syntax
= "yql:query", producerOnly = true, label = "api,http")
 public class YqlEndpoint extends DefaultEndpoint {
 
     @UriParam
     private final YqlConfiguration configuration;
+    private final HttpClientConnectionManager connectionManager;
+    private CloseableHttpClient httpClient;
 
-    YqlEndpoint(final String uri, final YqlComponent component, final YqlConfiguration configuration)
{
+    YqlEndpoint(final String uri, final YqlComponent component, final YqlConfiguration configuration,
final HttpClientConnectionManager connectionManager) {
         super(uri, component);
         this.configuration = configuration;
+        this.connectionManager = connectionManager;
     }
 
     @Override
@@ -50,6 +57,22 @@ public class YqlEndpoint extends DefaultEndpoint {
         return true;
     }
 
+    @Override
+    protected void doStop() throws Exception {
+        if (httpClient != null) {
+            httpClient.close();
+        }
+    }
+
+    synchronized CloseableHttpClient getHttpClient() {
+        if (httpClient == null) {
+            httpClient = HttpClients.custom()
+                .setConnectionManager(connectionManager)
+                .build();
+        }
+        return httpClient;
+    }
+
     YqlConfiguration getConfiguration() {
         return configuration;
     }
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
index c63dc7b..c429c85 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/YqlProducer.java
@@ -31,18 +31,18 @@ public class YqlProducer extends DefaultProducer {
     static final String CAMEL_YQL_HTTP_REQUEST = "CamelYqlHttpRequest";
 
     private final YqlEndpoint endpoint;
-    private final YqlClient yahooClient;
+    private final YqlClient yqlClient;
 
     YqlProducer(final YqlEndpoint endpoint) {
         super(endpoint);
         this.endpoint = endpoint;
-        this.yahooClient = new YqlClient();
+        this.yqlClient = new YqlClient(endpoint.getHttpClient());
     }
 
     @Override
     public void process(final Exchange exchange) throws Exception {
         final YqlConfiguration configuration = endpoint.getConfiguration();
-        final YqlResponse yqlResponse = yahooClient.get(
+        final YqlResponse yqlResponse = yqlClient.get(
                 configuration.getQuery(),
                 configuration.getFormat(),
                 configuration.isDiagnostics(),
diff --git a/components/camel-yql/src/main/java/org/apache/camel/component/yql/client/YqlClient.java
b/components/camel-yql/src/main/java/org/apache/camel/component/yql/client/YqlClient.java
index 0d5f00d..650243c 100644
--- a/components/camel-yql/src/main/java/org/apache/camel/component/yql/client/YqlClient.java
+++ b/components/camel-yql/src/main/java/org/apache/camel/component/yql/client/YqlClient.java
@@ -21,7 +21,6 @@ import org.apache.http.client.methods.CloseableHttpResponse;
 import org.apache.http.client.methods.HttpGet;
 import org.apache.http.client.utils.URIBuilder;
 import org.apache.http.impl.client.CloseableHttpClient;
-import org.apache.http.impl.client.HttpClients;
 import org.apache.http.util.EntityUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
@@ -29,7 +28,12 @@ import org.slf4j.LoggerFactory;
 public class YqlClient {
 
     private static final Logger LOG = LoggerFactory.getLogger(YqlClient.class);
-    private static final CloseableHttpClient HTTP_CLIENT = HttpClients.createDefault();
+
+    private final CloseableHttpClient httpClient;
+
+    public YqlClient(final CloseableHttpClient httpClient){
+        this.httpClient = httpClient;
+    }
 
     public YqlResponse get(final String query, final String format, final boolean diagnostics,
final String callback) throws Exception {
 
@@ -47,7 +51,7 @@ public class YqlClient {
         LOG.debug("YQL query: {}", uri);
 
         final HttpGet httpget = new HttpGet(uri);
-        try (final CloseableHttpResponse response = HTTP_CLIENT.execute(httpget)) {
+        try (final CloseableHttpResponse response = httpClient.execute(httpget)) {
             final YqlResponse yqlResponse = YqlResponse.builder()
                     .httpRequest(uri.toString())
                     .status(response.getStatusLine().getStatusCode())

-- 
To stop receiving notification emails like this one, please contact
"commits@camel.apache.org" <commits@camel.apache.org>.

Mime
View raw message