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 2011/06/09 13:31:58 UTC

svn commit: r1133799 - in /camel/trunk/camel-core/src/test/java/org/apache/camel/processor: SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java SplitSubUnitOfWorkStopOnExceptionTest.java

Author: davsclaus
Date: Thu Jun  9 11:31:58 2011
New Revision: 1133799

URL: http://svn.apache.org/viewvc?rev=1133799&view=rev
Log:
CAMEL-4078: Added shareUnitOfWork option to splitter,multicast and recipient list. This helps support some fairly common use cased in Camel routes more easily for end users. To keep API compatible I added a SubUnitOfWork instead of breaking API in current UnitOfWork which we can do in Camel 3.0

Added:
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java
    camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionTest.java
      - copied, changed from r1133795, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkTest.java

Added: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java?rev=1133799&view=auto
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java (added)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionAndParallelTest.java Thu Jun  9 11:31:58 2011
@@ -0,0 +1,100 @@
+/**
+ * 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;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Processor;
+import org.apache.camel.builder.RouteBuilder;
+
+/**
+ *
+ */
+public class SplitSubUnitOfWorkStopOnExceptionAndParallelTest extends ContextTestSupport {
+
+    private static int counter;
+
+    public void testOK() throws Exception {
+        counter = 0;
+
+        getMockEndpoint("mock:dead").expectedMessageCount(0);
+        getMockEndpoint("mock:a").expectedBodiesReceived("Tiger,Camel");
+        getMockEndpoint("mock:b").expectedBodiesReceivedInAnyOrder("Tiger", "Camel");
+        getMockEndpoint("mock:result").expectedBodiesReceived("Tiger,Camel");
+        getMockEndpoint("mock:line").expectedBodiesReceivedInAnyOrder("Tiger", "Camel");
+
+        template.sendBody("direct:start", "Tiger,Camel");
+
+        assertMockEndpointsSatisfied();
+    }
+
+    public void testError() throws Exception {
+        counter = 0;
+
+        getMockEndpoint("mock:dead").expectedBodiesReceived("Tiger,Donkey,Camel");
+        getMockEndpoint("mock:a").expectedMessageCount(1);
+        // b should get between 2 or 3 depending when we stop (we run parallel)
+        getMockEndpoint("mock:b").expectedMinimumMessageCount(2);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+        // line should get between 1 or 2 depending when we stop (we run parallel)
+        getMockEndpoint("mock:line").expectedMinimumMessageCount(1);
+
+        template.sendBody("direct:start", "Tiger,Donkey,Camel");
+
+        assertMockEndpointsSatisfied();
+
+        assertEquals(4, counter); // 1 first + 3 redeliveries
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                errorHandler(deadLetterChannel("mock:dead").useOriginalMessage()
+                        .maximumRedeliveries(3).redeliveryDelay(0));
+
+                from("direct:start")
+                    .to("mock:a")
+                    .split(body().tokenize(",")).shareUnitOfWork()
+                        .stopOnException().parallelProcessing()
+                        .to("mock:b")
+                        .to("direct:line")
+                    .end()
+                    .to("mock:result");
+
+                from("direct:line")
+                    .to("log:line")
+                    .process(new MyProcessor())
+                    .to("mock:line");
+            }
+        };
+    }
+
+    public static class MyProcessor implements Processor {
+
+        @Override
+        public void process(Exchange exchange) throws Exception {
+            String body = exchange.getIn().getBody(String.class);
+            if (body.contains("Donkey")) {
+                counter++;
+                throw new IllegalArgumentException("Donkey not allowed");
+            }
+        }
+    }
+
+}

Copied: camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionTest.java (from r1133795, camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkTest.java)
URL: http://svn.apache.org/viewvc/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionTest.java?p2=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionTest.java&p1=camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkTest.java&r1=1133795&r2=1133799&rev=1133799&view=diff
==============================================================================
--- camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkTest.java (original)
+++ camel/trunk/camel-core/src/test/java/org/apache/camel/processor/SplitSubUnitOfWorkStopOnExceptionTest.java Thu Jun  9 11:31:58 2011
@@ -24,7 +24,7 @@ import org.apache.camel.builder.RouteBui
 /**
  *
  */
-public class SplitSubUnitOfWorkTest extends ContextTestSupport {
+public class SplitSubUnitOfWorkStopOnExceptionTest extends ContextTestSupport {
 
     private static int counter;
 
@@ -47,9 +47,9 @@ public class SplitSubUnitOfWorkTest exte
 
         getMockEndpoint("mock:dead").expectedBodiesReceived("Tiger,Donkey,Camel");
         getMockEndpoint("mock:a").expectedMessageCount(1);
-        getMockEndpoint("mock:b").expectedBodiesReceived("Tiger", "Donkey", "Camel");
+        getMockEndpoint("mock:b").expectedBodiesReceived("Tiger", "Donkey");
         getMockEndpoint("mock:result").expectedMessageCount(0);
-        getMockEndpoint("mock:line").expectedBodiesReceived("Tiger", "Camel");
+        getMockEndpoint("mock:line").expectedBodiesReceived("Tiger");
 
         template.sendBody("direct:start", "Tiger,Donkey,Camel");
 
@@ -63,16 +63,13 @@ public class SplitSubUnitOfWorkTest exte
         return new RouteBuilder() {
             @Override
             public void configure() throws Exception {
-                // START SNIPPET: e1
                 errorHandler(deadLetterChannel("mock:dead").useOriginalMessage()
                         .maximumRedeliveries(3).redeliveryDelay(0));
 
                 from("direct:start")
                     .to("mock:a")
-                    // share unit of work in the splitter, which tells Camel to propagate failures from
-                    // processing the splitted messages back to the result of the splitter, which allows
-                    // it to act as a combined unit of work
                     .split(body().tokenize(",")).shareUnitOfWork()
+                        .stopOnException()
                         .to("mock:b")
                         .to("direct:line")
                     .end()
@@ -82,7 +79,6 @@ public class SplitSubUnitOfWorkTest exte
                     .to("log:line")
                     .process(new MyProcessor())
                     .to("mock:line");
-                // END SNIPPET: e1
             }
         };
     }