camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r1057238 - in /camel/trunk/components/camel-jetty/src: main/java/org/apache/camel/component/jetty/ test/java/org/apache/camel/component/jetty/async/
Date Mon, 10 Jan 2011 15:22:54 GMT
Author: davsclaus
Date: Mon Jan 10 15:22:53 2011
New Revision: 1057238

URL: http://svn.apache.org/viewvc?rev=1057238&view=rev
Log:
CAMEL-3524: Added option to set timeout when using Jetty continuation. The default timeout
of 30 sec. is sometime to slow for some systems.

Added:
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java
  (contents, props changed)
      - copied, changed from r1057163, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncTest.java
    camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
Modified:
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
    camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java?rev=1057238&r1=1057237&r2=1057238&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/CamelContinuationServlet.java
Mon Jan 10 15:22:53 2011
@@ -42,9 +42,15 @@ public class CamelContinuationServlet ex
     static final String EXCHANGE_ATTRIBUTE_ID = "CamelExchangeId";
 
     private static final long serialVersionUID = 1L;
+    // jetty will by default use 30000 millis as default timeout
+    private Long continuationTimeout;
 
     @Override
     protected void service(HttpServletRequest request, HttpServletResponse response) throws
ServletException, IOException {
+        if (log.isTraceEnabled()) {
+            log.trace("Service: " + request);
+        }
+
         // is there a consumer registered for the request.
         HttpConsumer consumer = resolve(request);
         if (consumer == null) {
@@ -56,6 +62,9 @@ public class CamelContinuationServlet ex
         if (result == null) {
             // no asynchronous result so leverage continuation
             final Continuation continuation = ContinuationSupport.getContinuation(request);
+            if (continuationTimeout != null) {
+                continuation.setTimeout(continuationTimeout);
+            }
 
             // are we suspended and a request is dispatched initially?
             if (consumer.isSuspended() && continuation.isInitial()) {
@@ -106,7 +115,7 @@ public class CamelContinuationServlet ex
 
         try {
             if (log.isTraceEnabled()) {
-                log.trace("Resuming continuation of exchangeId: " + result.getExchangeId());
+                log.trace("Resumed continuation and writing response for exchangeId: " +
result.getExchangeId());
             }
             // now lets output to the response
             consumer.getBinding().writeResponse(result, response);
@@ -116,4 +125,11 @@ public class CamelContinuationServlet ex
         }
     }
 
+    public Long getContinuationTimeout() {
+        return continuationTimeout;
+    }
+
+    public void setContinuationTimeout(Long continuationTimeout) {
+        this.continuationTimeout = continuationTimeout;
+    }
 }

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java?rev=1057238&r1=1057237&r2=1057238&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpComponent.java
Mon Jan 10 15:22:53 2011
@@ -94,6 +94,7 @@ public class JettyHttpComponent extends 
     protected MBeanContainer mbContainer;
     protected boolean enableJmx;
     protected JettyHttpBinding jettyHttpBinding;
+    protected Long continuationTimeout;
 
     class ConnectorRef {
         Server server;
@@ -138,6 +139,7 @@ public class JettyHttpComponent extends 
         Boolean enableMultipartFilter = getAndRemoveParameter(parameters, "enableMultipartFilter",
                                                               Boolean.class, true);
         Filter multipartFilter = resolveAndRemoveReferenceParameter(parameters, "multipartFilterRef",
Filter.class);
+        Long continuationTimeout = getAndRemoveParameter(parameters, "continuationTimeout",
Long.class);
 
         // configure http client if we have url configuration for it
         // http client is only used for jetty http producer (hence not very commonly used)
@@ -214,6 +216,10 @@ public class JettyHttpComponent extends 
             endpoint.setEnableMultipartFilter(true);
         }
 
+        if (continuationTimeout != null) {
+            endpoint.setContinuationTimeout(continuationTimeout);
+        }
+
         setProperties(endpoint, parameters);
         return endpoint;
     }
@@ -248,7 +254,7 @@ public class JettyHttpComponent extends 
                 }
                 server.addConnector(connector);
 
-                connectorRef = new ConnectorRef(server, connector, createServletForConnector(server,
connector, endpoint.getHandlers()));
+                connectorRef = new ConnectorRef(server, connector, createServletForConnector(server,
connector, endpoint.getHandlers(), endpoint));
                 // must enable session before we start
                 if (endpoint.isSessionSupport()) {
                     enableSessionSupport(connectorRef.server, connectorKey);
@@ -639,9 +645,18 @@ public class JettyHttpComponent extends 
         sslSocketConnectorProperties.put(key, value);
     }
 
+    public Long getContinuationTimeout() {
+        return continuationTimeout;
+    }
+
+    public void setContinuationTimeout(Long continuationTimeout) {
+        this.continuationTimeout = continuationTimeout;
+    }
+
     // Implementation methods
     // -------------------------------------------------------------------------
-    protected CamelServlet createServletForConnector(Server server, Connector connector,
List<Handler> handlers) throws Exception {
+    protected CamelServlet createServletForConnector(Server server, Connector connector,
+                                                     List<Handler> handlers, JettyHttpEndpoint
endpoint) throws Exception {
         ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.NO_SECURITY
| ServletContextHandler.NO_SESSIONS);
         context.setConnectorNames(new String[] {connector.getName()});
 
@@ -660,7 +675,16 @@ public class JettyHttpComponent extends 
         }
 
         // use Jetty continuations
-        CamelServlet camelServlet = new CamelContinuationServlet();
+        CamelContinuationServlet camelServlet = new CamelContinuationServlet();
+        // configure timeout and log it so end user know what we are using
+        Long timeout = endpoint.getContinuationTimeout() != null ? endpoint.getContinuationTimeout()
: getContinuationTimeout();
+        if (timeout != null) {
+            LOG.info("Using Jetty continuation timeout: " + timeout + " millis for: " + endpoint);
+            camelServlet.setContinuationTimeout(timeout);
+        } else {
+            LOG.info("Using default Jetty continuation timeout for: " + endpoint);
+        }
+
         ServletHolder holder = new ServletHolder();
         holder.setServlet(camelServlet);
         context.addServlet(holder, "/*");

Modified: camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java?rev=1057238&r1=1057237&r2=1057238&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
(original)
+++ camel/trunk/components/camel-jetty/src/main/java/org/apache/camel/component/jetty/JettyHttpEndpoint.java
Mon Jan 10 15:22:53 2011
@@ -43,6 +43,7 @@ public class JettyHttpEndpoint extends H
     private boolean enableJmx;
     private boolean enableMultipartFilter;
     private Filter multipartFilter;
+    private Long continuationTimeout;
 
     public JettyHttpEndpoint(JettyHttpComponent component, String uri, URI httpURL) throws
URISyntaxException {
         super(uri, component, httpURL);
@@ -133,4 +134,12 @@ public class JettyHttpEndpoint extends H
     public Filter getMultipartFilter() {
         return multipartFilter;
     }
+
+    public Long getContinuationTimeout() {
+        return continuationTimeout;
+    }
+
+    public void setContinuationTimeout(Long continuationTimeout) {
+        this.continuationTimeout = continuationTimeout;
+    }
 }

Copied: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java
(from r1057163, camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java?p2=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java&p1=camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncTest.java&r1=1057163&r2=1057238&rev=1057238&view=diff
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncTest.java
(original)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java
Mon Jan 10 15:22:53 2011
@@ -23,10 +23,10 @@ import org.junit.Test;
 /**
  * @version $Revision$
  */
-public class JettyAsyncTest extends BaseJettyTest {
+public class JettyAsyncContinuationTimeoutOkTest extends BaseJettyTest {
 
     @Test
-    public void testJettyAsync() throws Exception {
+    public void testJettyAsyncTimeoutOk() throws Exception {
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
         String reply = template.requestBody("http://localhost:{{port}}/myservice", null,
String.class);
@@ -42,7 +42,7 @@ public class JettyAsyncTest extends Base
             public void configure() throws Exception {
                 context.addComponent("async", new MyAsyncComponent());
 
-                from("jetty:http://localhost:{{port}}/myservice")
+                from("jetty:http://localhost:{{port}}/myservice?continuationTimeout=3000")
                     .to("async:Bye World")
                     .to("mock:result");
             }

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutOkTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Added: camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java?rev=1057238&view=auto
==============================================================================
--- camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
(added)
+++ camel/trunk/components/camel-jetty/src/test/java/org/apache/camel/component/jetty/async/JettyAsyncContinuationTimeoutTest.java
Mon Jan 10 15:22:53 2011
@@ -0,0 +1,67 @@
+/**
+ * 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.jetty.async;
+
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.http.HttpOperationFailedException;
+import org.apache.camel.component.jetty.BaseJettyTest;
+import org.apache.camel.util.StopWatch;
+import org.junit.Test;
+
+/**
+ * @version $Revision: 1027376 $
+ */
+public class JettyAsyncContinuationTimeoutTest extends BaseJettyTest {
+
+    @Test
+    public void testJettyAsyncTimeout() throws Exception {
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+
+        StopWatch watch = new StopWatch();
+        try {
+            template.requestBody("http://localhost:{{port}}/myservice", null, String.class);
+            fail("Should have thrown an exception");
+        } catch (CamelExecutionException e) {
+            log.info("Timeout hit and client got reply with failure status code");
+
+            long taken = watch.stop();
+
+            HttpOperationFailedException cause = assertIsInstanceOf(HttpOperationFailedException.class,
e.getCause());
+            assertEquals(503, cause.getStatusCode());
+
+            // should be approx 3-4 sec.
+            assertTrue("Timeout should occur faster than " + taken, taken < 4500);
+        }
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                context.addComponent("async", new MyAsyncComponent());
+
+                from("jetty:http://localhost:{{port}}/myservice?continuationTimeout=3000")
+                    .to("async:Bye World?delay=6000")
+                    .to("mock:result");
+            }
+        };
+    }
+}



Mime
View raw message