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 1345eb2f03d3 CAMEL-27023: camel-core-model - each route created from a
template gets its own copy of an aggregate or onException option given as an
expression (#30175)
1345eb2f03d3 is described below
commit 1345eb2f03d358f86eb419924be520da4d590086
Author: Federico Mariani <[email protected]>
AuthorDate: Mon Nov 16 16:09:19 2026 +0100
CAMEL-27023: camel-core-model - each route created from a template gets its
own copy of an aggregate or onException option given as an expression (#30175)
- In the Java DSL, `completionSize`, `completionTimeout` and
`completionPredicate` of the Aggregate EIP, and `handled`, `continued` and
`retryWhile` of `onException`, wrapped the expression builder
(`simple("{{size}}")`) in a plain `ExpressionDefinition`. Copying the route
kept the same builder, so every route created from a route template shared one
expression, built with the parameters of the first route.
- `ExpressionSubElementDefinition(Expression)` and `(Predicate)` now unwrap
the builder with `ExpressionNodeHelper.toExpressionDefinition`, as
`ExpressionNode` (filter, setBody, when ...) already does. Each templated route
gets its own copy of the expression.
- Side effect: route dumps (XML/YAML) and the aggregator JMX attributes now
show the language and text of these options. Before, the dump was an empty
`<expressionDefinition/>`.
_Claude Code on behalf of Federico Mariani_
Co-Authored-By: Claude <[email protected]>
---
.../model/ExpressionSubElementDefinition.java | 6 +-
.../RouteTemplateExpressionOptionPerRouteTest.java | 254 +++++++++++++++++++++
.../ROOT/pages/camel-4x-upgrade-guide-4_24.adoc | 10 +
3 files changed, 268 insertions(+), 2 deletions(-)
diff --git
a/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionSubElementDefinition.java
b/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionSubElementDefinition.java
index e3baece6f778..83e4e5abbfd5 100644
---
a/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionSubElementDefinition.java
+++
b/core/camel-core-model/src/main/java/org/apache/camel/model/ExpressionSubElementDefinition.java
@@ -50,11 +50,13 @@ public class ExpressionSubElementDefinition implements
HasExpressionType {
}
public ExpressionSubElementDefinition(Expression expression) {
- this.expressionType = new ExpressionDefinition(expression);
+ // unwrap a builder such as simple("...") to its expression
definition, so a copy (each route created from a
+ // route template) gets its own expression rather than sharing one
that is created only once
+ this.expressionType =
ExpressionNodeHelper.toExpressionDefinition(expression);
}
public ExpressionSubElementDefinition(Predicate predicate) {
- this.expressionType = new ExpressionDefinition(predicate);
+ this.expressionType =
ExpressionNodeHelper.toExpressionDefinition(predicate);
}
public ExpressionSubElementDefinition copyDefinition() {
diff --git
a/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateExpressionOptionPerRouteTest.java
b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateExpressionOptionPerRouteTest.java
new file mode 100644
index 000000000000..4ffa4e2ad0e6
--- /dev/null
+++
b/core/camel-core/src/test/java/org/apache/camel/builder/RouteTemplateExpressionOptionPerRouteTest.java
@@ -0,0 +1,254 @@
+/*
+ * 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.builder;
+
+import java.util.List;
+import java.util.Map;
+import java.util.concurrent.ConcurrentHashMap;
+import java.util.concurrent.atomic.AtomicInteger;
+
+import org.apache.camel.ContextTestSupport;
+import org.apache.camel.Exchange;
+import org.apache.camel.Predicate;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.model.AggregateDefinition;
+import org.apache.camel.model.RouteTemplateDefinition;
+import org.apache.camel.processor.aggregate.GroupedBodyAggregationStrategy;
+import org.junit.jupiter.api.Test;
+
+import static org.junit.jupiter.api.Assertions.assertEquals;
+import static org.junit.jupiter.api.Assertions.assertFalse;
+import static org.junit.jupiter.api.Assertions.assertTrue;
+
+/**
+ * The options of an EIP that take an expression or predicate in the Java DSL
(aggregate completionSize,
+ * completionTimeout and completionPredicate, onException handled and
retryWhile) are given to each route created from a
+ * route template, so each route uses its own template parameters.
+ */
+public class RouteTemplateExpressionOptionPerRouteTest extends
ContextTestSupport {
+
+ private final Map<String, AtomicInteger> attempts = new
ConcurrentHashMap<>();
+
+ @Override
+ public boolean isUseRouteBuilder() {
+ return false;
+ }
+
+ private void addRoutes(RouteBuilder builder) throws Exception {
+ context.addRoutes(builder);
+ context.addTemplatedRoutes(builder);
+ context.start();
+ }
+
+ private void sendSix() {
+ for (int i = 1; i <= 6; i++) {
+ template.sendBody("direct:a", "a" + i);
+ }
+ for (int i = 1; i <= 6; i++) {
+ template.sendBody("direct:b", "b" + i);
+ }
+ }
+
+ private static void assertGroupSize(MockEndpoint mock, int size) {
+ for (Exchange exchange : mock.getReceivedExchanges()) {
+ assertEquals(size, exchange.getIn().getBody(List.class).size(),
"Group size on " + mock.getEndpointUri());
+ }
+ }
+
+ private void assertSizeTwoOnAAndThreeOnB() throws Exception {
+ MockEndpoint a = getMockEndpoint("mock:a");
+ MockEndpoint b = getMockEndpoint("mock:b");
+ a.expectedMessageCount(3);
+ b.expectedMessageCount(2);
+
+ sendSix();
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertGroupSize(a, 2);
+ assertGroupSize(b, 3);
+ }
+
+ @Test
+ public void testCompletionSizeExpression() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("size")
+ .from("direct:{{name}}")
+ .aggregate(constant(true), new
GroupedBodyAggregationStrategy())
+ .completionSize(simple("{{size}}"))
+ .to("mock:{{name}}");
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("size", "2");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("size", "3");
+ }
+ });
+
+ assertSizeTwoOnAAndThreeOnB();
+ }
+
+ @Test
+ public void testCompletionPredicate() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("size")
+ .from("direct:{{name}}")
+ .aggregate(constant(true), new
GroupedBodyAggregationStrategy())
+
.completionPredicate(simple("${exchangeProperty.CamelAggregatedSize} ==
{{size}}"))
+ .to("mock:{{name}}");
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("size", "2");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("size", "3");
+ }
+ });
+
+ assertSizeTwoOnAAndThreeOnB();
+ }
+
+ @Test
+ public void testCompletionSizeString() throws Exception {
+ // the String option was already resolved for each route
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("size")
+ .from("direct:{{name}}")
+ .aggregate(constant(true), new
GroupedBodyAggregationStrategy())
+ .completionSize("{{size}}")
+ .to("mock:{{name}}");
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("size", "2");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("size", "3");
+ }
+ });
+
+ assertSizeTwoOnAAndThreeOnB();
+ }
+
+ @Test
+ public void testCompletionTimeoutExpression() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("timeout")
+ .from("direct:{{name}}")
+ .aggregate(constant(true), new
GroupedBodyAggregationStrategy())
+
.completionTimeout(simple("{{timeout}}")).completionTimeoutCheckerInterval(10)
+ .to("mock:{{name}}");
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("timeout", "600000");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("timeout", "100");
+ }
+ });
+
+ // route b times out its group after 100 millis, not after the 10
minutes of route a
+ MockEndpoint b = getMockEndpoint("mock:b");
+ b.expectedMessageCount(1);
+
+ template.sendBody("direct:a", "a1");
+ template.sendBody("direct:b", "b1");
+ template.sendBody("direct:b", "b2");
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertGroupSize(b, 2);
+ assertEquals(0, getMockEndpoint("mock:a").getReceivedCounter());
+ }
+
+ @Test
+ public void testOnExceptionRetryWhile() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("retries")
+ .from("direct:{{name}}")
+ .onException(IllegalStateException.class)
+
.retryWhile(simple("${header.CamelRedeliveryCounter} <=
{{retries}}")).redeliveryDelay(0)
+ .handled(true)
+ .to("mock:{{name}}")
+ .end()
+ .process(exchange -> {
+
attempts.computeIfAbsent(exchange.getFromRouteId(), k -> new
AtomicInteger()).incrementAndGet();
+ throw new IllegalStateException("Forced");
+ });
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("retries", "1");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("retries", "3");
+ }
+ });
+
+ getMockEndpoint("mock:a").expectedMessageCount(1);
+ getMockEndpoint("mock:b").expectedMessageCount(1);
+
+ template.sendBody("direct:a", "a");
+ template.sendBody("direct:b", "b");
+
+ MockEndpoint.assertIsSatisfied(context);
+ // the first attempt and then the retries
+ assertEquals(2, attempts.get("a").get());
+ assertEquals(4, attempts.get("b").get());
+ }
+
+ @Test
+ public void testOnExceptionHandled() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("handled")
+ .from("direct:{{name}}")
+ .onException(IllegalStateException.class)
+ .handled((Predicate) simple("{{handled}}"))
+ .to("mock:{{name}}")
+ .end()
+ .throwException(IllegalStateException.class, "Forced");
+
+ templatedRoute("t").routeId("a").parameter("name",
"a").parameter("handled", "true");
+ templatedRoute("t").routeId("b").parameter("name",
"b").parameter("handled", "false");
+ }
+ });
+
+ getMockEndpoint("mock:a").expectedMessageCount(1);
+ getMockEndpoint("mock:b").expectedMessageCount(1);
+
+ Exchange a = template.send("direct:a", e ->
e.getMessage().setBody("a"));
+ Exchange b = template.send("direct:b", e ->
e.getMessage().setBody("b"));
+
+ MockEndpoint.assertIsSatisfied(context);
+ assertFalse(a.isFailed(), "Route a handles the exception");
+ assertTrue(b.isFailed(), "Route b does not handle the exception");
+ }
+
+ @Test
+ public void testTemplateKeepsTheExpressionModel() throws Exception {
+ addRoutes(new RouteBuilder() {
+ @Override
+ public void configure() {
+
routeTemplate("t").templateParameter("name").templateParameter("size")
+ .from("direct:{{name}}")
+ .aggregate(constant(true), new
GroupedBodyAggregationStrategy())
+ .completionSize(simple("{{size}}"))
+ .to("mock:{{name}}");
+ }
+ });
+
+ RouteTemplateDefinition template =
context.getRouteTemplateDefinition("t");
+ AggregateDefinition aggregate = (AggregateDefinition)
template.getRoute().getOutputs().get(0);
+ // the expression is the simple language with the text as given, so a
copy of the template has its own
+ assertEquals("simple",
aggregate.getCompletionSizeExpression().getExpressionType().getLanguage());
+ assertEquals("{{size}}",
aggregate.getCompletionSizeExpression().getExpressionType().getExpression());
+ }
+}
diff --git
a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
index 2577b3f8f95b..cdb0e12d565e 100644
--- a/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
+++ b/docs/user-manual/modules/ROOT/pages/camel-4x-upgrade-guide-4_24.adoc
@@ -85,6 +85,16 @@ keys. Code that walks `getOutputs()` (route dumps, tooling)
now sees them, and `
the outputs, for example with advice-with, is no longer used, and changing the
list returned by `getCatchClauses()`
no longer changes the doTry. Use `setCatchClauses` and `setFinallyClause`, or
the outputs, instead.
+=== camel-core-model - EIP options given an expression in the Java DSL keep
its language
+
+In the Java DSL, `completionSize`, `completionTimeout` and
`completionPredicate` of the Aggregate EIP, and `handled`,
+`continued` and `retryWhile` of `onException`, keep an expression made with
`simple(...)`, `header(...)` or another
+expression builder as its language and text, as the EIPs that take an
expression directly do. Each route created from
+a route template now has its own copy of the expression, so a template
parameter in it, such as
+`+completionSize(simple("{{size}}"))+`, takes the value of that route: before,
every route used the value of the first
+route created from the template. Route dumps and the JMX attributes of the
aggregator (`CompletionSizeExpression`,
+`CompletionSizeExpressionLanguage` and the others) now show the language and
the expression.
+
=== camel-ehcache - shared CacheManager and changed defaults
The following user-visible changes take effect in 4.24: