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 2015/03/23 11:54:14 UTC

[2/5] camel git commit: CAMEL-7433: Allow aggregation strategy to determine pre complete when using aggregator.

CAMEL-7433: Allow aggregation strategy to determine pre complete when using aggregator.


Project: http://git-wip-us.apache.org/repos/asf/camel/repo
Commit: http://git-wip-us.apache.org/repos/asf/camel/commit/0c7b6d22
Tree: http://git-wip-us.apache.org/repos/asf/camel/tree/0c7b6d22
Diff: http://git-wip-us.apache.org/repos/asf/camel/diff/0c7b6d22

Branch: refs/heads/master
Commit: 0c7b6d22be17f82375ce94aaac4477f45260a171
Parents: 5094596
Author: Claus Ibsen <da...@apache.org>
Authored: Mon Mar 23 08:08:42 2015 +0100
Committer: Claus Ibsen <da...@apache.org>
Committed: Mon Mar 23 11:55:58 2015 +0100

----------------------------------------------------------------------
 .../processor/aggregate/AggregateProcessor.java | 46 ++++++++++++++---
 .../BodyInPreCompleteAggregatingStrategy.java   | 40 +++++++++++++++
 .../AggregatePredicateAwareStrategyTest.java    | 53 ++++++++++++++++++++
 3 files changed, 133 insertions(+), 6 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/camel/blob/0c7b6d22/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
----------------------------------------------------------------------
diff --git a/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java b/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
index 9b93c36..b365442 100644
--- a/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
+++ b/camel-core/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
@@ -375,6 +375,8 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
     private List<Exchange> doAggregation(String key, Exchange newExchange) throws CamelExchangeException {
         LOG.trace("onAggregation +++ start +++ with correlation key: {}", key);
 
+        List<Exchange> list = new ArrayList<Exchange>();
+
         Exchange answer;
         Exchange originalExchange = aggregationRepository.get(newExchange.getContext(), key);
         Exchange oldExchange = originalExchange;
@@ -390,9 +392,25 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
             size++;
         }
 
+        // prepare the exchanges for aggregation
+        ExchangeHelper.prepareAggregation(oldExchange, newExchange);
+
+        // check if we are pre complete
+        boolean preComplete;
+        try {
+            // put the current aggregated size on the exchange so its avail during completion check
+            newExchange.setProperty(Exchange.AGGREGATED_SIZE, size);
+            preComplete = onPreCompletionAggregation(oldExchange, newExchange);
+            // remove it afterwards
+            newExchange.removeProperty(Exchange.AGGREGATED_SIZE);
+        } catch (Throwable e) {
+            // must catch any exception from aggregation
+            throw new CamelExchangeException("Error occurred during preComplete", newExchange, e);
+        }
+
         // check if we are complete
         String complete = null;
-        if (isEagerCheckCompletion()) {
+        if (!preComplete && isEagerCheckCompletion()) {
             // put the current aggregated size on the exchange so its avail during completion check
             newExchange.setProperty(Exchange.AGGREGATED_SIZE, size);
             complete = isCompleted(key, newExchange);
@@ -400,12 +418,23 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
             newExchange.removeProperty(Exchange.AGGREGATED_SIZE);
         }
 
-        // prepare the exchanges for aggregation and then aggregate them
-        ExchangeHelper.prepareAggregation(oldExchange, newExchange);
-        // must catch any exception from aggregation
+        if (preComplete) {
+            // need to pre complete the current group before we aggregate
+            doAggregationComplete("strategy", list, key, originalExchange, oldExchange);
+            // as we complete the current group eager, we should indicate the new group is not complete
+            complete = null;
+            // and clear old/original exchange as we start on a new group
+            oldExchange = null;
+            originalExchange = null;
+            // and reset the size to 1
+            size = 1;
+        }
+
+        // aggregate the exchanges
         try {
             answer = onAggregation(oldExchange, newExchange);
         } catch (Throwable e) {
+            // must catch any exception from aggregation
             throw new CamelExchangeException("Error occurred during aggregation", newExchange, e);
         }
         if (answer == null) {
@@ -420,8 +449,6 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
             complete = isCompleted(key, answer);
         }
 
-        List<Exchange> list = new ArrayList<Exchange>();
-
         if (complete == null) {
             // only need to update aggregation repository if we are not complete
             doAggregationRepositoryAdd(newExchange.getContext(), key, originalExchange, answer);
@@ -568,6 +595,13 @@ public class AggregateProcessor extends ServiceSupport implements AsyncProcessor
         return aggregationStrategy.aggregate(oldExchange, newExchange);
     }
 
+    protected boolean onPreCompletionAggregation(Exchange oldExchange, Exchange newExchange) {
+        if (aggregationStrategy instanceof PreCompletionAwareAggregationStrategy) {
+            return ((PreCompletionAwareAggregationStrategy) aggregationStrategy).preComplete(oldExchange, newExchange);
+        }
+        return false;
+    }
+
     protected Exchange onCompletion(final String key, final Exchange original, final Exchange aggregated, boolean fromTimeout) {
         // store the correlation key as property before we remove so the repository has that information
         if (original != null) {

http://git-wip-us.apache.org/repos/asf/camel/blob/0c7b6d22/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteAggregatingStrategy.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteAggregatingStrategy.java b/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteAggregatingStrategy.java
new file mode 100644
index 0000000..11a87c8
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteAggregatingStrategy.java
@@ -0,0 +1,40 @@
+/**
+ * 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.Exchange;
+import org.apache.camel.processor.aggregate.PreCompletionAwareAggregationStrategy;
+
+public class BodyInPreCompleteAggregatingStrategy implements PreCompletionAwareAggregationStrategy {
+
+    public Exchange aggregate(Exchange oldExchange, Exchange newExchange) {
+        if (oldExchange == null) {
+            return newExchange;
+        }
+
+        String oldBody = oldExchange.getIn().getBody(String.class);
+        String newBody = newExchange.getIn().getBody(String.class);
+        oldExchange.getIn().setBody(oldBody + "+" + newBody);
+        return oldExchange;
+    }
+
+    public boolean preComplete(Exchange oldExchange, Exchange newExchange) {
+        // pre complete when new body has an X
+        String newBody = newExchange.getIn().getBody(String.class);
+        return newBody.contains("X");
+    }
+}

http://git-wip-us.apache.org/repos/asf/camel/blob/0c7b6d22/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePredicateAwareStrategyTest.java
----------------------------------------------------------------------
diff --git a/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePredicateAwareStrategyTest.java b/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePredicateAwareStrategyTest.java
new file mode 100644
index 0000000..74fe19b
--- /dev/null
+++ b/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePredicateAwareStrategyTest.java
@@ -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.aggregator;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.processor.BodyInPreCompleteAggregatingStrategy;
+
+/**
+ * @version 
+ */
+public class AggregatePredicateAwareStrategyTest extends ContextTestSupport {
+
+    public void testAggregatePreComplete() throws Exception {
+        getMockEndpoint("mock:aggregated").expectedBodiesReceived("A+B+C", "X+D+E");
+
+        template.sendBodyAndHeader("direct:start", "A", "id", 123);
+        template.sendBodyAndHeader("direct:start", "B", "id", 123);
+        template.sendBodyAndHeader("direct:start", "C", "id", 123);
+        template.sendBodyAndHeader("direct:start", "X", "id", 123);
+        template.sendBodyAndHeader("direct:start", "D", "id", 123);
+        template.sendBodyAndHeader("direct:start", "E", "id", 123);
+        template.sendBodyAndHeader("direct:start", "X", "id", 123);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new RouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                    .aggregate(header("id"), new BodyInPreCompleteAggregatingStrategy()).completionSize(5)
+                        .to("mock:aggregated");
+            }
+        };
+    }
+}
\ No newline at end of file