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 2009/05/08 08:44:43 UTC

svn commit: r772862 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/processor/ camel-core/src/test/java/org/apache/camel/processor/async/ components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/ components/camel-sprin...

Author: davsclaus
Date: Fri May  8 06:44:43 2009
New Revision: 772862

URL: http://svn.apache.org/viewvc?rev=772862&view=rev
Log:
CAMEL-1572: Improvements to asynd DSL.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteWaitIfReplyExpectedTest.java
      - copied, changed from r772859, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteNoWaitTest.java
    camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.java
      - copied, changed from r772853, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.java
    camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml
      - copied, changed from r772853, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/processor/AsyncProcessor.java

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/processor/AsyncProcessor.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/processor/AsyncProcessor.java?rev=772862&r1=772861&r2=772862&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/processor/AsyncProcessor.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/processor/AsyncProcessor.java Fri May  8 06:44:43 2009
@@ -77,10 +77,14 @@
             wait = exchange.getIn().getHeader(Exchange.ASYNC_WAIT, WaitForTaskToComplete.class);
         }
 
-        if (wait != WaitForTaskToComplete.Newer) {
+        if (wait == WaitForTaskToComplete.Always) {
             // wait for task to complete
             Exchange response = future.get();
             ExchangeHelper.copyResults(exchange, response);
+        } if (wait == WaitForTaskToComplete.IfReplyExpected && ExchangeHelper.isOutCapable(exchange)) {
+            // wait for task to complete as we expect a reply
+            Exchange response = future.get();
+            ExchangeHelper.copyResults(exchange, response);
         } else {
             // no we do not expect a reply so lets continue, set a handle to the future task
             // in case end user need it later

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteWaitIfReplyExpectedTest.java (from r772859, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteNoWaitTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteWaitIfReplyExpectedTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteWaitIfReplyExpectedTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteNoWaitTest.java&r1=772859&r2=772862&rev=772862&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteNoWaitTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteWaitIfReplyExpectedTest.java Fri May  8 06:44:43 2009
@@ -16,11 +16,8 @@
  */
 package org.apache.camel.processor.async;
 
-import java.util.concurrent.Future;
-
 import org.apache.camel.ContextTestSupport;
 import org.apache.camel.Exchange;
-import org.apache.camel.ExchangePattern;
 import org.apache.camel.Processor;
 import org.apache.camel.WaitForTaskToComplete;
 import org.apache.camel.builder.RouteBuilder;
@@ -28,7 +25,7 @@
 /**
  * @version $Revision$
  */
-public class AsyncRouteNoWaitTest extends ContextTestSupport {
+public class AsyncRouteWaitIfReplyExpectedTest extends ContextTestSupport {
 
     private static String route = "";
 
@@ -38,82 +35,35 @@
         route = "";
     }
 
-    public void testAsyncNoWaitRouteExchange() throws Exception {
+    public void testAsyncReplyExpected() throws Exception {
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
-        // send an in out to the direct endpoint using the classic API
-        Exchange exchange = template.send("direct:start", new Processor() {
-            public void process(Exchange exchange) throws Exception {
-                exchange.setPattern(ExchangePattern.InOut);
-                exchange.getIn().setBody("Hello");
-            }
-        });
+        Object out = template.requestBody("direct:start", "Hello");
+        assertNotNull(out);
+        assertIsInstanceOf(String.class, out);
 
-        // we should run before the async processor that sets B
+        // we should not run before the async processor that sets B
         route += "A";
 
-        // as it turns into a async route later we get a Future in the IN body
-        Object out = exchange.getOut().getBody();
-        assertIsInstanceOf(Future.class, out);
-
-        // cast to future
-        Future future = (Future) out;
-
-        assertFalse("Should not be done", future.isDone());
-
         assertMockEndpointsSatisfied();
 
-        assertEquals("AB", route);
-
-        // get the response from the future
-        String response = context.getTypeConverter().convertTo(String.class, future);
-        assertEquals("Bye World", response);
+        assertEquals("BA", route);
+        assertEquals("Bye World", out);
     }
 
-    public void testAsyncNoWaitRoute() throws Exception {
+    public void testAsyncNoReplyExpected() throws Exception {
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
-        // send a request reply to the direct start endpoint
-        Object out = template.requestBody("direct:start", "Hello");
+        template.sendBody("direct:start", "Hello");
 
         // we should run before the async processor that sets B
         route += "A";
 
-        // as it turns into a async route later we get a Future as response
-        assertIsInstanceOf(Future.class, out);
-
-        // cast to future
-        Future future = (Future) out;
-
-        assertFalse("Should not be done", future.isDone());
-
         assertMockEndpointsSatisfied();
 
         assertEquals("AB", route);
-
-        // get the response from the future
-        String response = context.getTypeConverter().convertTo(String.class, future);
-        assertEquals("Bye World", response);
-    }
-
-    public void testAsyncRouteNoWaitWithTypeConverted() throws Exception {
-        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
-        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
-
-        // send a request reply to the direct start endpoint, but will use
-        // future type converter that will wait for the response, even though the async
-        // is set to not wait. As the type converter will wait for us
-        String response = template.requestBody("direct:start", "Hello", String.class);
-
-        // we should wait for the async response as we ask for the result as a String body
-        route += "A";
-
-        assertMockEndpointsSatisfied();
-
-        assertEquals("Bye World", response);
-        assertEquals("BA", route);
     }
 
     @Override
@@ -128,10 +78,9 @@
                             // now turn the route into async from this point forward
                             // the caller will have a Future<Exchange> returned as response in OUT
                             // to be used to grap the async response when he fell like it
-                            // we do not want to wait for tasks to be complete so we instruct Camel
-                            // to not wait, and therefore Camel returns the Future<Exchange> handle we
-                            // can use to get the result when we want
-                        .async().waitForTaskToComplete(WaitForTaskToComplete.Newer)
+                            // we only want to wait for tasks to compelte if we expect a reply
+                            // otherwise not
+                        .async().waitForTaskToComplete(WaitForTaskToComplete.IfReplyExpected)
                             // from this point forward this is the async route doing its work
                             // so we do a bit of delay to simulate heavy work that takes time
                         .to("mock:foo")
@@ -155,4 +104,4 @@
             exchange.getOut().setBody("Bye World");
         }
     }
-}
+}
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.java (from r772853, camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.java?p2=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.java&p1=camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.java&r1=772853&r2=772862&rev=772862&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.java (original)
+++ camel/trunk/components/camel-spring/src/test/java/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.java Fri May  8 06:44:43 2009
@@ -17,16 +17,16 @@
 package org.apache.camel.spring.processor.async;
 
 import org.apache.camel.CamelContext;
-import org.apache.camel.processor.async.AsyncRouteTest;
+import org.apache.camel.processor.async.AsyncRouteWaitIfReplyExpectedTest;
 import static org.apache.camel.spring.processor.SpringTestHelper.createSpringCamelContext;
 
 /**
  * @version $Revision$
  */
-public class SpringAsyncRouteTest extends AsyncRouteTest {
+public class SpringAsyncRouteWaitIfReplyExpectedTest extends AsyncRouteWaitIfReplyExpectedTest {
 
     protected CamelContext createCamelContext() throws Exception {
-        return createSpringCamelContext(this, "org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml");
+        return createSpringCamelContext(this, "org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml");
     }
 
 }
\ No newline at end of file

Copied: camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml (from r772853, camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml)
URL: http://svn.apache.org/viewvc/camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml?p2=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml&p1=camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml&r1=772853&r2=772862&rev=772862&view=diff
==============================================================================
--- camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteTest.xml (original)
+++ camel/trunk/components/camel-spring/src/test/resources/org/apache/camel/spring/processor/async/SpringAsyncRouteWaitIfReplyExpectedTest.xml Fri May  8 06:44:43 2009
@@ -30,7 +30,7 @@
                 <constant>Hello World</constant>
             </transform>
 
-            <async>
+            <async waitForTaskToComplete="IfReplyExpected">
                 <to uri="mock:foo"/>
                 <delay>
                     <constant>100</constant>
@@ -42,7 +42,7 @@
         </route>
     </camelContext>
 
-    <bean id="myProcessor" class="org.apache.camel.processor.async.AsyncRouteTest$MyProcessor"/>
+    <bean id="myProcessor" class="org.apache.camel.processor.async.AsyncRouteWaitIfReplyExpectedTest$MyProcessor"/>
     <!-- END SNIPPET: e1 -->
 
 </beans>