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 2014/12/11 12:18:30 UTC
camel git commit: CAMEL-8144: Allow camel-restlet to use synchronous
restlet client handler to avoid potential performance drop issue
Repository: camel
Updated Branches:
refs/heads/camel-2.14.x b1fd76db9 -> 413e0aa3c
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/413e0aa3
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/413e0aa3
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/413e0aa3
Branch: refs/heads/camel-2.14.x
Commit: 413e0aa3c8a9f5ee95cd28f8500a82e133f11352
Parents: b1fd76d
Author: Claus Ibsen <da...@apache.org>
Authored: Thu Dec 11 11:22:54 2014 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Thu Dec 11 12:18:11 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/413e0aa3/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 c5f582a..954c24b 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
@@ -78,6 +78,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());
@@ -93,6 +94,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);
@@ -503,6 +507,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/413e0aa3/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/413e0aa3/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");
+ }
+ });
+ }
+ };
+ }
+}