camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject camel git commit: CAMEL-8144: Allow camel-restlet to use synchronous restlet client handler to avoid potential performance drop issue
Date Thu, 11 Dec 2014 11:18:51 GMT
Repository: camel
Updated Branches:
  refs/heads/master 9a42f37e6 -> 3fef41ebc


CAMEL-8144: Allow camel-restlet to use synchronous restlet client handler to avoid potential
performance drop issue


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/3fef41eb
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/3fef41eb
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/3fef41eb

Branch: refs/heads/master
Commit: 3fef41ebc087ddcd4d81c2abb82af77b39c9c3b2
Parents: 9a42f37
Author: Claus Ibsen <davsclaus@apache.org>
Authored: Thu Dec 11 11:22:54 2014 +0100
Committer: Claus Ibsen <davsclaus@apache.org>
Committed: Thu Dec 11 12:18:42 2014 +0100

----------------------------------------------------------------------
 .../component/restlet/RestletComponent.java     | 12 ++++
 .../component/restlet/RestletProducer.java      | 39 +++++++++++-
 .../RestletProducerSynchronouslyTest.java       | 65 ++++++++++++++++++++
 3 files changed, 114 insertions(+), 2 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/3fef41eb/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
index 3205fb7..44fb434 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletComponent.java
@@ -85,6 +85,7 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
     private Boolean reuseAddress;
     private boolean disableStreamCache;
     private int port;
+    private Boolean synchronous;
 
     public RestletComponent() {
         this(new Component());
@@ -100,6 +101,9 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
     @Override
     protected Endpoint createEndpoint(String uri, String remaining, Map<String, Object>
parameters) throws Exception {
         RestletEndpoint result = new RestletEndpoint(this, remaining);
+        if (synchronous != null) {
+            result.setSynchronous(synchronous);
+        }
         result.setDisableStreamCache(isDisableStreamCache());
         setEndpointHeaderFilterStrategy(result);
         setProperties(result, parameters);
@@ -579,6 +583,14 @@ public class RestletComponent extends HeaderFilterStrategyComponent implements
R
         this.port = port;
     }
 
+    public Boolean getSynchronous() {
+        return synchronous;
+    }
+
+    public void setSynchronous(Boolean synchronous) {
+        this.synchronous = synchronous;
+    }
+
     @Override
     public Consumer createConsumer(CamelContext camelContext, Processor processor, String
verb, String basePath, String uriTemplate,
                                    String consumes, String produces, Map<String, Object>
parameters) throws Exception {

http://git-wip-us.apache.org/repos/asf/camel/blob/3fef41eb/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
index 7a0edd0..5e94a64 100644
--- a/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
+++ b/components/camel-restlet/src/main/java/org/apache/camel/component/restlet/RestletProducer.java
@@ -66,9 +66,44 @@ public class RestletProducer extends DefaultAsyncProducer {
     }
 
     @Override
+    public void process(Exchange exchange) throws Exception {
+        RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
+
+        final RestletBinding binding = endpoint.getRestletBinding();
+        Request request;
+        String resourceUri = buildUri(endpoint, exchange);
+        request = new Request(endpoint.getRestletMethod(), resourceUri);
+        binding.populateRestletRequestFromExchange(request, exchange);
+
+        LOG.debug("Sending request synchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
+        Response response = client.handle(request);
+        LOG.debug("Received response synchronously: {} for exchangeId: {}", response, exchange.getExchangeId());
+        if (response != null) {
+            Integer respCode = response.getStatus().getCode();
+            if (respCode > 207 && throwException) {
+                exchange.setException(populateRestletProducerException(exchange, response,
respCode));
+            } else {
+                binding.populateExchangeFromRestletResponse(exchange, response);
+            }
+        }
+    }
+
+    @Override
     public boolean process(final Exchange exchange, final AsyncCallback callback) {
         RestletEndpoint endpoint = (RestletEndpoint) getEndpoint();
 
+        // force processing synchronously using different api
+        if (endpoint.isSynchronous()) {
+            try {
+                process(exchange);
+            } catch (Exception e) {
+                exchange.setException(e);
+            }
+            return true;
+        }
+
+        LOG.trace("Processing asynchronously");
+
         final RestletBinding binding = endpoint.getRestletBinding();
         Request request;
         try {
@@ -83,11 +118,11 @@ public class RestletProducer extends DefaultAsyncProducer {
         }
 
         // process the request asynchronously
-        LOG.debug("Sending request: {} for exchangeId: {}", request, exchange.getExchangeId());
+        LOG.debug("Sending request asynchronously: {} for exchangeId: {}", request, exchange.getExchangeId());
         client.handle(request, new Uniform() {
             @Override
             public void handle(Request request, Response response) {
-                LOG.debug("Received response: {} for exchangeId: {}", response, exchange.getExchangeId());
+                LOG.debug("Received response asynchronously: {} for exchangeId: {}", response,
exchange.getExchangeId());
                 try {
                     if (response != null) {
                         Integer respCode = response.getStatus().getCode();

http://git-wip-us.apache.org/repos/asf/camel/blob/3fef41eb/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
----------------------------------------------------------------------
diff --git a/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
new file mode 100644
index 0000000..7346603
--- /dev/null
+++ b/components/camel-restlet/src/test/java/org/apache/camel/component/restlet/RestletProducerSynchronouslyTest.java
@@ -0,0 +1,65 @@
+/**
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.camel.component.restlet;
+
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.rest.RestComponent;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class RestletProducerSynchronouslyTest extends RestletTestSupport {
+
+    @Test
+    public void testRestletProducerGet() throws Exception {
+        String out = template.requestBodyAndHeader("direct:start", null, "id", 123, String.class);
+        assertEquals("123;Donald Duck", out);
+    }
+    
+    @Test
+    public void testRestletProducerDelete() throws Exception {
+        String out = template.requestBodyAndHeader("direct:delete", null, "id", 123, String.class);
+        assertEquals("123;Donald Duck", out);
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // force synchronous processing using restlet
+                RestletComponent restlet = context.getComponent("restlet", RestletComponent.class);
+                restlet.setSynchronous(true);
+
+                from("direct:start").to("restlet:http://localhost:" + portNum + "/users/123/basic").to("log:reply");
+                
+                from("direct:delete").to("restlet:http://localhost:" + portNum + "/users/123/basicrestletMethod=DELETE");
+
+                from("restlet:http://localhost:" + portNum + "/users/{id}/basic?restletMethods=GET,DELETE")
+                    .process(new Processor() {
+                        public void process(Exchange exchange) throws Exception {
+                            String id = exchange.getIn().getHeader("id", String.class);
+                            exchange.getOut().setBody(id + ";Donald Duck");
+                        }
+                    });
+            }
+        };
+    }
+}


Mime
View raw message