You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@camel.apache.org by ra...@apache.org on 2012/09/25 23:43:48 UTC

svn commit: r1390169 - in /camel/branches/camel-2.10.x/camel-core/src: main/java/org/apache/camel/processor/Enricher.java test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java

Author: raulk
Date: Tue Sep 25 21:43:48 2012
New Revision: 1390169

URL: http://svn.apache.org/viewvc?rev=1390169&view=rev
Log:
CAMEL-5636: Enricher with async routing engine was not behaving properly when an exception was thrown from the AggregationStrategy.

Added:
    camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java
Modified:
    camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/processor/Enricher.java

Modified: camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/processor/Enricher.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/processor/Enricher.java?rev=1390169&r1=1390168&r2=1390169&view=diff
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/processor/Enricher.java (original)
+++ camel/branches/camel-2.10.x/camel-core/src/main/java/org/apache/camel/processor/Enricher.java Tue Sep 25 21:43:48 2012
@@ -126,10 +126,17 @@ public class Enricher extends ServiceSup
 
                     // prepare the exchanges for aggregation
                     ExchangeHelper.prepareAggregation(exchange, resourceExchange);
-                    Exchange aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
-                    if (aggregatedExchange != null) {
-                        // copy aggregation result onto original exchange (preserving pattern)
-                        copyResultsPreservePattern(exchange, aggregatedExchange);
+                    Exchange aggregatedExchange;
+                    try {
+                        aggregatedExchange = aggregationStrategy.aggregate(exchange, resourceExchange);
+                        if (aggregatedExchange != null) {
+                            // copy aggregation result onto original exchange (preserving pattern)
+                            copyResultsPreservePattern(exchange, aggregatedExchange);
+                        }
+                    } catch (Throwable e) {
+                        // if the aggregationStrategy threw an exception, set it on the original exchange
+                        exchange.setException(new CamelExchangeException("Error occurred during aggregation", exchange, e));
+                        callback.done(false);
                     }
                 }
 

Added: camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java
URL: http://svn.apache.org/viewvc/camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java?rev=1390169&view=auto
==============================================================================
--- camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java (added)
+++ camel/branches/camel-2.10.x/camel-core/src/test/java/org/apache/camel/processor/enricher/EnricherAsyncUnhandledExceptionTest.java Tue Sep 25 21:43:48 2012
@@ -0,0 +1,94 @@
+/**
+ * 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.enricher;
+
+import java.util.concurrent.ExecutionException;
+import java.util.concurrent.Future;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.CamelExchangeException;
+import org.apache.camel.CamelExecutionException;
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.processor.aggregate.AggregationStrategy;
+import org.apache.camel.processor.async.MyAsyncComponent;
+import org.apache.camel.spi.ShutdownStrategy;
+import org.junit.Test;
+
+/**
+ * @version 
+ */
+public class EnricherAsyncUnhandledExceptionTest extends ContextTestSupport {
+
+    @Test
+    public void testInOutWithRequestBody() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:pickedUp");
+        mock.expectedMessageCount(1);
+        // this direct endpoint should receive an exception
+        try {
+            Future<Object> obj = template.asyncRequestBody("direct:in", "Hello World");
+            // wait five seconds at most; else, let's assume something went wrong
+            obj.get(5000, TimeUnit.MILLISECONDS);
+        } catch (Exception e) {
+            // if we receive an exception, the async routing engine is working correctly
+            // before the Enricher was fixed for cases where routing was async and the AggregationStrategy 
+            // threw an exception, the call to requestBody would stall indefinitely
+            // unwrap the exception chain
+            assertTrue(e instanceof ExecutionException);
+            assertTrue(e.getCause() instanceof CamelExecutionException);
+            assertTrue(e.getCause().getCause() instanceof CamelExchangeException);
+            assertTrue(e.getCause().getCause().getCause() instanceof RuntimeException);
+            assertTrue(e.getCause().getCause().getCause().getMessage().equals("Bang! Unhandled exception"));
+            mock.assertIsSatisfied();
+            return;
+        }
+        fail("Expected an RuntimeException");
+    }
+
+
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext camelContext = super.createCamelContext();
+        ShutdownStrategy shutdownStrategy = camelContext.getShutdownStrategy();
+        camelContext.addComponent("async", new MyAsyncComponent());
+        shutdownStrategy.setTimeout(1000);
+        shutdownStrategy.setTimeUnit(TimeUnit.MILLISECONDS);
+        shutdownStrategy.setShutdownNowOnTimeout(true);
+        return camelContext;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            public void configure() throws Exception {
+                from("direct:in")
+                    .to("mock:pickedUp")
+                    // using the async utility component to ensure that the async routing engine kicks in
+                    .enrich("async:out?reply=Reply", new AggregationStrategy() {
+                        @Override
+                        public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+                            throw new RuntimeException("Bang! Unhandled exception");
+                        }
+                    });
+                
+            }
+        };
+    }
+
+}