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 2021/11/05 12:09:06 UTC
[camel] branch main updated: CAMEL-17170: Aggregate EIP - pre
completion should have access to correlation key
This is an automated email from the ASF dual-hosted git repository.
davsclaus pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/main by this push:
new 51efc1c CAMEL-17170: Aggregate EIP - pre completion should have access to correlation key
51efc1c is described below
commit 51efc1cf25304b75cfb1818c8e48c07a85e204fa
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Nov 5 13:08:21 2021 +0100
CAMEL-17170: Aggregate EIP - pre completion should have access to correlation key
---
.../docs/modules/eips/pages/aggregate-eip.adoc | 10 ++++
.../processor/aggregate/AggregateProcessor.java | 4 ++
.../BodyInPreCompleteSizeAggregatingStrategy.java | 54 ++++++++++++++++++++++
.../aggregator/AggregatePreCompleteSizeTest.java | 53 +++++++++++++++++++++
4 files changed, 121 insertions(+)
diff --git a/core/camel-core-engine/src/main/docs/modules/eips/pages/aggregate-eip.adoc b/core/camel-core-engine/src/main/docs/modules/eips/pages/aggregate-eip.adoc
index 77c3c02..3920481 100644
--- a/core/camel-core-engine/src/main/docs/modules/eips/pages/aggregate-eip.adoc
+++ b/core/camel-core-engine/src/main/docs/modules/eips/pages/aggregate-eip.adoc
@@ -271,6 +271,16 @@ Then the `newExchange` is used to start the correlation group from scratch,
so the group would contain only that new incoming exchange. This is
known as pre-completion mode.
+The `newExchange` contains the following exchange properties, which can be used
+to determine whether to pre complete.
+
+[width="100%",cols="3,1m,6",options="header"]
+|=======================================================================
+| Property | Type | Description
+| `CamelAggregatedSize` | `int` | The total number of messages aggregated.
+| `CamelAggregatedCorrelationKey` | `String` | The correlation identifier as a `String`.
+|=======================================================================
+
When the aggregation is in _pre-completion_ mode, then only the following completions are in use:
* _completionTimeout_ or _completionInterval_ can also be used as fallback
diff --git a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
index 77b0f78..9b44c6f 100644
--- a/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
+++ b/core/camel-core-processor/src/main/java/org/apache/camel/processor/aggregate/AggregateProcessor.java
@@ -503,6 +503,7 @@ public class AggregateProcessor extends AsyncProcessorSupport
try {
// put the current aggregated size on the exchange so its avail during completion check
newExchange.setProperty(ExchangePropertyKey.AGGREGATED_SIZE, size);
+ newExchange.setProperty(ExchangePropertyKey.AGGREGATED_CORRELATION_KEY, key);
complete = isPreCompleted(key, oldExchange, newExchange);
// make sure to track timeouts if not complete
if (complete == null) {
@@ -510,6 +511,7 @@ public class AggregateProcessor extends AsyncProcessorSupport
}
// remove it afterwards
newExchange.removeProperty(ExchangePropertyKey.AGGREGATED_SIZE);
+ newExchange.removeProperty(ExchangePropertyKey.AGGREGATED_CORRELATION_KEY);
} catch (Throwable e) {
// must catch any exception from aggregation
throw new CamelExchangeException("Error occurred during preComplete", newExchange, e);
@@ -517,6 +519,7 @@ public class AggregateProcessor extends AsyncProcessorSupport
} else if (isEagerCheckCompletion()) {
// put the current aggregated size on the exchange so its avail during completion check
newExchange.setProperty(ExchangePropertyKey.AGGREGATED_SIZE, size);
+ newExchange.setProperty(ExchangePropertyKey.AGGREGATED_CORRELATION_KEY, key);
complete = isCompleted(key, newExchange);
// make sure to track timeouts if not complete
if (complete == null) {
@@ -524,6 +527,7 @@ public class AggregateProcessor extends AsyncProcessorSupport
}
// remove it afterwards
newExchange.removeProperty(ExchangePropertyKey.AGGREGATED_SIZE);
+ newExchange.removeProperty(ExchangePropertyKey.AGGREGATED_CORRELATION_KEY);
}
if (preCompletion && complete != null) {
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteSizeAggregatingStrategy.java b/core/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteSizeAggregatingStrategy.java
new file mode 100644
index 0000000..1cfd469
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/BodyInPreCompleteSizeAggregatingStrategy.java
@@ -0,0 +1,54 @@
+/*
+ * 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.AggregationStrategy;
+import org.apache.camel.Exchange;
+
+public class BodyInPreCompleteSizeAggregatingStrategy implements AggregationStrategy {
+
+ @Override
+ 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;
+ }
+
+ @Override
+ public boolean canPreComplete() {
+ return true;
+ }
+
+ @Override
+ public boolean preComplete(Exchange oldExchange, Exchange newExchange) {
+ String key = newExchange.getProperty(Exchange.AGGREGATED_CORRELATION_KEY, String.class);
+ int size = newExchange.getProperty(Exchange.AGGREGATED_SIZE, int.class);
+
+ if ("123".equals(key)) {
+ return size > 2;
+ } else if ("456".equals(key)) {
+ return size > 3;
+ } else {
+ return true;
+ }
+ }
+}
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePreCompleteSizeTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePreCompleteSizeTest.java
new file mode 100644
index 0000000..7c777bc
--- /dev/null
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/aggregator/AggregatePreCompleteSizeTest.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.BodyInPreCompleteSizeAggregatingStrategy;
+import org.junit.jupiter.api.Test;
+
+public class AggregatePreCompleteSizeTest extends ContextTestSupport {
+
+ @Test
+ public void testAggregatePreComplete() throws Exception {
+ getMockEndpoint("mock:aggregated").expectedBodiesReceived("A+B", "C+D+F", "E+G");
+
+ template.sendBodyAndHeader("direct:start", "A", "id", 123);
+ template.sendBodyAndHeader("direct:start", "B", "id", 123);
+ template.sendBodyAndHeader("direct:start", "C", "id", 456);
+ template.sendBodyAndHeader("direct:start", "D", "id", 456);
+ template.sendBodyAndHeader("direct:start", "E", "id", 123);
+ template.sendBodyAndHeader("direct:start", "F", "id", 456);
+ template.sendBodyAndHeader("direct:start", "G", "id", 123);
+ template.sendBodyAndHeader("direct:start", "H", "id", 456);
+ template.sendBodyAndHeader("direct:start", "I", "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 BodyInPreCompleteSizeAggregatingStrategy())
+ .to("mock:aggregated");
+ }
+ };
+ }
+}