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 2008/12/06 11:34:04 UTC

svn commit: r723966 - in /activemq/camel/trunk/camel-core/src: main/java/org/apache/camel/model/ProcessorType.java main/java/org/apache/camel/util/CollectionHelper.java test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java

Author: davsclaus
Date: Sat Dec  6 02:34:03 2008
New Revision: 723966

URL: http://svn.apache.org/viewvc?rev=723966&view=rev
Log:
CAMEL-1150: Fixed aggregator not working with onException in a route

Added:
    activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java   (with props)
Modified:
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
    activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CollectionHelper.java

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java?rev=723966&r1=723965&r2=723966&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/model/ProcessorType.java Sat Dec  6 02:34:03 2008
@@ -38,6 +38,7 @@
 import org.apache.camel.Predicate;
 import org.apache.camel.Processor;
 import org.apache.camel.Route;
+import org.apache.camel.util.CollectionHelper;
 import org.apache.camel.builder.DataFormatClause;
 import org.apache.camel.builder.DeadLetterChannelBuilder;
 import org.apache.camel.builder.ErrorHandlerBuilder;
@@ -780,8 +781,8 @@
      * @return the expression clause to be used as builder to configure the correlation expression
      */
     public ExpressionClause<AggregatorType> aggregate() {
-        if (!getOutputs().isEmpty()) {
-            throw new IllegalArgumentException("Aggregator must be the only output added to the route: " + this);
+        if (CollectionHelper.filterList(getOutputs(), ExceptionType.class).isEmpty()) {
+            LOG.warn("Aggregator must be the only output added to the route: " + this);
         }
         AggregatorType answer = new AggregatorType();
         addOutput(answer);
@@ -796,8 +797,8 @@
      * @return the expression clause to be used as builder to configure the correlation expression
      */
     public ExpressionClause<AggregatorType> aggregate(AggregationStrategy aggregationStrategy) {
-        if (!getOutputs().isEmpty()) {
-            throw new IllegalArgumentException("Aggregator must be the only output added to the route: " + this);
+        if (CollectionHelper.filterList(getOutputs(), ExceptionType.class).isEmpty()) {
+            LOG.warn("Aggregator must be the only output added to the route: " + this);
         }
         AggregatorType answer = new AggregatorType();
         answer.setAggregationStrategy(aggregationStrategy);
@@ -813,8 +814,8 @@
      * @return the builder
      */
     public AggregatorType aggregate(AggregationCollection aggregationCollection) {
-        if (!getOutputs().isEmpty()) {
-            throw new IllegalArgumentException("Aggregator must be the only output added to the route: " + this);
+        if (CollectionHelper.filterList(getOutputs(), ExceptionType.class).isEmpty()) {
+            LOG.warn("Aggregator must be the only output added to the route: " + this);
         }
         AggregatorType answer = new AggregatorType();
         answer.setAggregationCollection(aggregationCollection);
@@ -833,8 +834,8 @@
      * @return the builder
      */
     public AggregatorType aggregate(Expression correlationExpression) {
-        if (!getOutputs().isEmpty()) {
-            throw new IllegalArgumentException("Aggregator must be the only output added to the route: " + this);
+        if (CollectionHelper.filterList(getOutputs(), ExceptionType.class).isEmpty()) {
+            LOG.warn("Aggregator must be the only output added to the route: " + this);
         }
         AggregatorType answer = new AggregatorType(correlationExpression);
         addOutput(answer);
@@ -853,8 +854,8 @@
      * @return the builder
      */
     public AggregatorType aggregate(Expression correlationExpression, AggregationStrategy aggregationStrategy) {
-        if (!getOutputs().isEmpty()) {
-            throw new IllegalArgumentException("Aggregator must be the only output added to the route: " + this);
+        if (CollectionHelper.filterList(getOutputs(), ExceptionType.class).isEmpty()) {
+            LOG.warn("Aggregator must be the only output added to the route: " + this);
         }
         AggregatorType answer = new AggregatorType(correlationExpression, aggregationStrategy);
         addOutput(answer);

Modified: activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CollectionHelper.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CollectionHelper.java?rev=723966&r1=723965&r2=723966&view=diff
==============================================================================
--- activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CollectionHelper.java (original)
+++ activemq/camel/trunk/camel-core/src/main/java/org/apache/camel/util/CollectionHelper.java Sat Dec  6 02:34:03 2008
@@ -39,6 +39,9 @@
 
     /**
      * Returns the size of the collection if it can be determined to be a collection
+     *
+     * @param value the collection
+     * @return the size, or <tt>null</tt> if not a collection
      */
     public static Integer size(Object value) {
         if (value != null) {
@@ -71,7 +74,6 @@
      * @param value the value to put in the map
      */
     public static void appendValue(Map map, Object key, Object value) {
-
         Object oldValue = map.get(key);
         if (oldValue != null) {
             List list;
@@ -86,4 +88,23 @@
             map.put(key, value);
         }
     }
+
+    /**
+     * Filters the given list to skip instanceof filter objects.
+     * 
+     * @param list  the list
+     * @param filters  objects to skip
+     * @return a new list without the filtered objects
+     */
+    public static List filterList(List list, Object... filters) {
+        List answer = new ArrayList();
+        for (Object o : list) {
+            for (Object filter : filters) {
+                if (!o.getClass().isInstance(filter)) {
+                    answer.add(o);
+                }
+            }
+        }
+        return answer;
+    }
 }

Added: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java
URL: http://svn.apache.org/viewvc/activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java?rev=723966&view=auto
==============================================================================
--- activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java (added)
+++ activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java Sat Dec  6 02:34:03 2008
@@ -0,0 +1,64 @@
+/**
+ * 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.aggregator;
+
+import org.apache.camel.CamelException;
+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.aggregate.PredicateAggregationCollection;
+
+/**
+ * Unit test inspired by user forum.
+ */
+public class AggregatorAndOnExceptionTest extends ContextTestSupport {
+
+    public void testAggregateAndOnException() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Bye World");
+
+        template.sendBodyAndHeader("seda:start", "Hello World", "id", 123);
+        template.sendBodyAndHeader("seda:start", "Bye World", "id", 123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                errorHandler(deadLetterChannel("mock:error"));
+
+                onException(CamelException.class).maximumRedeliveries(2);
+
+                from("seda:start")
+                        .aggregate(new PredicateAggregationCollection(header("id"),
+                                        new AggregationStrategy() {
+                                            public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+                                                return newExchange;
+                                            }
+                                        },
+                                        header(Exchange.AGGREGATED_COUNT).isEqualTo(2)))
+                        .batchTimeout(500L)
+                        .to("mock:result");
+            }
+        };
+    }
+}

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: activemq/camel/trunk/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatorAndOnExceptionTest.java
------------------------------------------------------------------------------
    svn:keywords = Rev Date