camel-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From davscl...@apache.org
Subject svn commit: r773337 - in /camel/trunk: camel-core/src/main/java/org/apache/camel/model/ camel-core/src/test/java/org/apache/camel/processor/async/ tests/camel-itest/src/test/java/org/apache/camel/itest/async/ tests/camel-itest/src/test/resources/
Date Sun, 10 May 2009 13:03:54 GMT
Author: davsclaus
Date: Sun May 10 13:03:53 2009
New Revision: 773337

URL: http://svn.apache.org/viewvc?rev=773337&view=rev
Log:
CAMEL-1572: More samples for async() DSL for show and tell.

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
  (with props)
    camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.java
  (with props)
Modified:
    camel/trunk/camel-core/src/main/java/org/apache/camel/model/AsyncDefinition.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDeadLetterChannelTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDefaultErrorHandlerTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java
    camel/trunk/tests/camel-itest/src/test/resources/log4j.properties

Modified: camel/trunk/camel-core/src/main/java/org/apache/camel/model/AsyncDefinition.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/main/java/org/apache/camel/model/AsyncDefinition.java?rev=773337&r1=773336&r2=773337&view=diff
==============================================================================
--- camel/trunk/camel-core/src/main/java/org/apache/camel/model/AsyncDefinition.java (original)
+++ camel/trunk/camel-core/src/main/java/org/apache/camel/model/AsyncDefinition.java Sun May
10 13:03:53 2009
@@ -45,7 +45,7 @@
     @XmlAttribute(required = false)
     private Integer poolSize;
     @XmlAttribute(required = false)
-    private WaitForTaskToComplete waitForTaskToComplete = WaitForTaskToComplete.Always;
+    private WaitForTaskToComplete waitForTaskToComplete = WaitForTaskToComplete.IfReplyExpected;
 
     @Override
     public Processor createProcessor(RouteContext routeContext) throws Exception {

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDeadLetterChannelTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDeadLetterChannelTest.java?rev=773337&r1=773336&r2=773337&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDeadLetterChannelTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDeadLetterChannelTest.java
Sun May 10 13:03:53 2009
@@ -62,7 +62,7 @@
         mock.message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(2);
 
         try {
-            template.sendBody("direct:in", "Hello World");
+            template.requestBody("direct:in", "Hello World");
             fail("Should have thrown a CamelExecutionException");
         } catch (CamelExecutionException e) {
             assertEquals("Forced exception by unit test", e.getCause().getMessage());
@@ -97,7 +97,7 @@
         mock.message(0).header(Exchange.REDELIVERED).isEqualTo(Boolean.TRUE);
         mock.message(0).header(Exchange.REDELIVERY_COUNTER).isEqualTo(2);
 
-        template.sendBody("direct:in", "Hello World");
+        template.requestBody("direct:in", "Hello World");
 
         assertMockEndpointsSatisfied();
     }

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDefaultErrorHandlerTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDefaultErrorHandlerTest.java?rev=773337&r1=773336&r2=773337&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDefaultErrorHandlerTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncDefaultErrorHandlerTest.java
Sun May 10 13:03:53 2009
@@ -54,7 +54,7 @@
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
 
         try {
-            template.sendBody("direct:in", "Hello World");
+            template.requestBody("direct:in", "Hello World");
             fail("Should have thrown a CamelExecutionException");
         } catch (CamelExecutionException e) {
             assertEquals("Forced exception by unit test", e.getCause().getMessage());
@@ -83,7 +83,7 @@
 
         // as it turns into async and we do not wait for the task to complete
         // we will not get notified of the exception
-        template.sendBody("direct:in", "Hello World");
+        template.requestBody("direct:in", "Hello World");
 
         assertMockEndpointsSatisfied();
     }

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java?rev=773337&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
(added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
Sun May 10 13:03:53 2009
@@ -0,0 +1,53 @@
+/**
+ * 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.processor.async;
+
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.WaitForTaskToComplete;
+
+/**
+ * @version $Revision$
+ */
+public class AsyncRouteIfReplyExpectedTest extends AsyncRouteTest {
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // we start this route async
+                from("direct:start")
+                        // we play a bit with the message
+                        .transform(body().append(" World"))
+                            // 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
+                            // only wait if we expect a reply (also default for async)
+                        .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")
+                        .delay(100)
+                            // and we also work with the message so we can prepare a response
+                        .process(new MyProcessor())
+                            // and we use mocks for unit testing
+                        .to("mock:result");
+            }
+        };
+    }
+
+}

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteIfReplyExpectedTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date

Modified: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java?rev=773337&r1=773336&r2=773337&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java
(original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/async/AsyncRouteTest.java
Sun May 10 13:03:53 2009
@@ -16,9 +16,12 @@
  */
 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.Processor;
+import org.apache.camel.ExchangePattern;
 import org.apache.camel.builder.RouteBuilder;
 
 /**
@@ -34,7 +37,7 @@
         route = "";
     }
 
-    public void testAsyncRoute() throws Exception {
+    public void testAsyncRequestReplyRoute() throws Exception {
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
@@ -57,7 +60,7 @@
         assertEquals("Bye World", response);
     }
 
-    public void testAsyncRouteWithTypeConverted() throws Exception {
+    public void testAsyncRequestReplyRouteWithTypeConverted() throws Exception {
         getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
         getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
 
@@ -74,6 +77,46 @@
         assertEquals("BA", route);
     }
 
+    public void testAsyncRequestOnlyRoute() throws Exception {
+        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+
+        // send a request only to the direct start endpoint
+        // it will NOT wait for the async response so we get the full response
+        template.sendBody("direct:start", "Hello");
+
+        // we should run before the async processor that sets B
+        route += "A";
+
+        assertMockEndpointsSatisfied();
+        assertEquals("AB", route);
+    }
+
+    public void testAsyncRequestOnlyRouteWithExchange() throws Exception {
+        getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Bye World");
+
+        // send a request only to the direct start endpoint
+        // it will NOT wait for the async response so we get the full response
+        Exchange out = template.send("direct:start", new Processor() {
+            public void process(Exchange exchange) throws Exception {
+                exchange.setPattern(ExchangePattern.InOnly);
+                exchange.getIn().setBody("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(Exchange.class, out);
+        Future future = out.getOut().getBody(Future.class);
+
+        assertMockEndpointsSatisfied();
+        assertTrue("Should be done", future.isDone());
+        assertEquals("AB", route);
+    }
+
     @Override
     protected RouteBuilder createRouteBuilder() throws Exception {
         return new RouteBuilder() {

Added: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.java?rev=773337&view=auto
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.java
(added)
+++ camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.java
Sun May 10 13:03:53 2009
@@ -0,0 +1,131 @@
+/**
+ * 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 javax.naming.Context;
+
+import org.apache.activemq.camel.component.ActiveMQComponent;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.impl.JndiRegistry;
+import org.apache.camel.impl.StringDataFormat;
+import org.apache.camel.spi.DataFormat;
+import org.apache.camel.util.jndi.JndiContext;
+
+/**
+ * @version $Revision$
+ */
+public class HttpAsyncDslTest extends ContextTestSupport {
+
+    private static String order = "";
+
+    public void testRequestOnly() throws Exception {
+        getMockEndpoint("mock:validate").expectedMessageCount(1);
+        // even though its request only the message is still continued being processed
+        getMockEndpoint("mock:order").expectedMessageCount(1);
+
+        template.sendBody("jms:queue:order", "Order: Camel in Action");
+        order += "C";
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals("CAB", order);
+    }
+
+    public void testRequestReply() throws Exception {
+        getMockEndpoint("mock:validate").expectedMessageCount(1);
+        // even though its request only the message is still continued being processed
+        getMockEndpoint("mock:order").expectedMessageCount(1);
+
+        String response = template.requestBody("jms:queue:order", "Order: Camel in Action",
String.class);
+        order += "C";
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals("ABC", order);
+        assertEquals("Order OK", response);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        order = "";
+        super.setUp();
+    }
+
+    @Override
+    protected JndiRegistry createRegistry() throws Exception {
+        JndiRegistry jndi =  super.createRegistry();
+        jndi.bind("validateOrder", new MyValidateOrderBean());
+        jndi.bind("handleOrder", new MyHandleOrderBean());
+        return jndi;
+    }
+
+    @Override
+    protected Context createJndiContext() throws Exception {
+        JndiContext answer = new JndiContext();
+
+        // add ActiveMQ with embedded broker
+        ActiveMQComponent amq = ActiveMQComponent.activeMQComponent("vm://localhost?broker.persistent=false");
+        amq.setCamelContext(context);
+        answer.bind("jms", amq);
+        return answer;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                // START SNIPPET: e1
+                // just a unit test but imaging using your own data format that does complex
+                // and CPU heavy processing for decrypting the message
+                DataFormat mySecureDataFormat = new StringDataFormat("iso-8859-1");
+
+                // list on the JMS queue for new orders
+                from("jms:queue:order")
+                    // do some sanity check validation
+                    .to("bean:validateOrder")
+                    .to("mock:validate")
+                    // turn the route async as some others do not expect a reply
+                    // and a few does then we can use the async DSL as a turning point
+                    // if the JMS ReplyTo was set then we expect a reply, otherwise not
+                    // use a pool of 20 threads for the point forward
+                    .async(20)
+                    // do some CPU heavy processing of the message (we simulate and delay
just 100 ms)
+                    .unmarshal(mySecureDataFormat).delay(100).to("bean:handleOrder").to("mock:order");
+                // END SNIPPET: e1
+            }
+        };
+    }
+
+    public static class MyValidateOrderBean {
+
+        public void validateOrder(byte[] payload) {
+            order += "A";
+            // noop
+        }
+    }
+
+    public static class MyHandleOrderBean {
+
+        public String handleOrder(String message) {
+            order += "B";
+            return "Order OK";
+            // noop
+        }
+    }
+}

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

Propchange: camel/trunk/tests/camel-itest/src/test/java/org/apache/camel/itest/async/HttpAsyncDslTest.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=773337&r1=773336&r2=773337&view=diff
==============================================================================
--- camel/trunk/tests/camel-itest/src/test/resources/log4j.properties (original)
+++ camel/trunk/tests/camel-itest/src/test/resources/log4j.properties Sun May 10 13:03:53
2009
@@ -18,12 +18,12 @@
 #
 # The logging properties used for eclipse testing, We want to see debug output on the console.
 #
-log4j.rootLogger=WARN, out
+log4j.rootLogger=INFO, file
 
 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
@@ -33,8 +33,8 @@
 #log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
 
 # File appender
-log4j.appender.out=org.apache.log4j.FileAppender
-log4j.appender.out.layout=org.apache.log4j.PatternLayout
-log4j.appender.out.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
-log4j.appender.out.file=target/camel-itest-test.log
-log4j.appender.out.append=true
+log4j.appender.file=org.apache.log4j.FileAppender
+log4j.appender.file.layout=org.apache.log4j.PatternLayout
+log4j.appender.file.layout.ConversionPattern=%d [%-15.15t] %-5p %-30.30c{1} - %m%n
+log4j.appender.file.file=target/camel-itest-test.log
+log4j.appender.file.append=true



Mime
View raw message