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 17:41:30 UTC

svn commit: r773035 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ tests/camel-itest/src/test/java/org/apache/camel/itest/async/ tests/camel-itest/src/test/resources/

Author: davsclaus
Date: Fri May  8 15:41:29 2009
New Revision: 773035

URL: http://svn.apache.org/viewvc?rev=773035&view=rev
Log:
CAMEL-1572: Added async sample for show and tell of the new Async API.

Added:
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java   (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
    camel/trunk/tests/camel-itest/src/test/resources/log4j.properties

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java?rev=773035&r1=773034&r2=773035&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/util/ExchangeHelper.java Fri May  8 15:41:29 2009
@@ -511,6 +511,9 @@
     }
 
     private static <T> T doExtractBody(CamelContext context, Object result, Class<T> type) {
+        if (result == null) {
+            return null;
+        }
         if (type.isAssignableFrom(result.getClass())) {
             return type.cast(result);
         }

Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java?rev=773035&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java (added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java Fri May  8 15:41:29 2009
@@ -0,0 +1,81 @@
+/**
+ * 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.itest.async;
+
+import java.util.concurrent.Future;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+
+/**
+ * @version $Revision$
+ */
+public class HttpAsyncTest extends ContextTestSupport {
+
+    public void testAsyncAndSyncAtSameTimeWithHttp() throws Exception {
+        // START SNIPPET: e2
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        // we expect the name job to be faster than the async job even though the async job
+        // was started first
+        mock.expectedBodiesReceived("Claus", "Bye World");
+
+        // send a async request/reply message to the http endpoint
+        Future future = template.asyncRequestBody("http://0.0.0.0:9080/myservice", "Hello World");
+
+        // we got the future so in the meantime we can do other stuff, as this is Camel
+        // so lets invoke another request/reply route but this time is synchronous
+        String name = template.requestBody("direct:name", "Give me a name", String.class);
+        assertEquals("Claus", name);
+
+        // okay we got a name and we have done some other work at the same time
+        // the async route is running, but now its about time to wait and get
+        // get the response from the async task
+
+        // so we use the async extract body to return a string body response
+        // this allows us to do this in a single code line instead of using the
+        // JDK Future API to get hold of it, but you can also use that if you want
+        String response = template.asyncExtractBody(future, String.class);
+        assertEquals("Bye World", response);
+
+        assertMockEndpointsSatisfied();
+        // END SNIPPET: e2
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                // the mocks are here for unit test
+
+                // some other service to return a name, this is invoked
+                // synhronously
+                from("direct:name").transform(constant("Claus")).to("mock:result");
+
+                // simulate a slow http service we want to invoke async
+                from("jetty:http://0.0.0.0:9080/myservice")
+                    .delay(1000)
+                    .transform(constant("Bye World"))
+                    .to("mock:result");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/tests/camel-itest/src/test/resources/log4j.properties
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/resources/log4j.properties?rev=773035&r1=773034&r2=773035&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/log4j.properties (original)
+++ camel/trunk/tests/camel-itest/src/test/resources/log4j.properties Fri May  8 15:41:29 2009
@@ -23,7 +23,7 @@
 log4j.logger.org.springframework=WARN
 log4j.logger.org.apache.activemq=WARN
 #log4j.logger.org.apache.camel=TRACE
-#log4j.logger.org.apache.camel=DEBUG
+log4j.logger.org.apache.camel=DEBUG
 #log4j.logger.org.apache.camel.component.file=TRACE
 
 # CONSOLE appender not used by default



Re: svn commit: r773035 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ tests/camel-itest/src/test/java/org/apache/camel/itest/async/ tests/camel-itest/src/test/resources/

Posted by Claus Ibsen <cl...@gmail.com>.
On Sat, May 9, 2009 at 5:29 AM, Willem Jiang <wi...@gmail.com> wrote:
> davsclaus@apache.org wrote:
>> Author: davsclaus
>> Date: Fri May  8 15:41:29 2009
>> New Revision: 773035
>>
>> URL: http://svn.apache.org/viewvc?rev=773035&view=rev
>> Log:
>> CAMEL-1572: Added async sample for show and tell of the new Async API.
>>
>
>> +
>> +        // so we use the async extract body to return a string body response
>> +        // this allows us to do this in a single code line instead of using the
>> +        // JDK Future API to get hold of it, but you can also use that if you want
>> +        String response = template.asyncExtractBody(future, String.class);
>> +        assertEquals("Bye World", response);
>> +
> How about name the method with extractFutureBody?
> asyncExtractBody makes me think it's async method call, but in fact it
> will block the calling thread if the feature is not done yet.
Good idea willem, I was in doubt with a good name so I took the
internal extractBody name at first.
Your idea is much better, so lets use it.

>
> Willem
>



-- 
Claus Ibsen
Apache Camel Committer

Open Source Integration: http://fusesource.com
Blog: http://davsclaus.blogspot.com/
Twitter: http://twitter.com/davsclaus
Apache Camel Reference Card:
http://refcardz.dzone.com/refcardz/enterprise-integration
Interview with me:
http://architects.dzone.com/articles/interview-claus-ibsen-about?mz=7893-progress

Re: svn commit: r773035 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/util/ tests/camel-itest/src/test/java/org/apache/camel/itest/async/ tests/camel-itest/src/test/resources/

Posted by Willem Jiang <wi...@gmail.com>.
davsclaus@apache.org wrote:
> Author: davsclaus
> Date: Fri May  8 15:41:29 2009
> New Revision: 773035
> 
> URL: http://svn.apache.org/viewvc?rev=773035&view=rev
> Log:
> CAMEL-1572: Added async sample for show and tell of the new Async API.
> 

> +
> +        // so we use the async extract body to return a string body response
> +        // this allows us to do this in a single code line instead of using the
> +        // JDK Future API to get hold of it, but you can also use that if you want
> +        String response = template.asyncExtractBody(future, String.class);
> +        assertEquals("Bye World", response);
> +
How about name the method with extractFutureBody?
asyncExtractBody makes me think it's async method call, but in fact it
will block the calling thread if the feature is not done yet.

Willem