From f9b674e16ba2efc6f338e2f39ea38acae3320957 Mon Sep 17 00:00:00 2001 From: Claus Ibsen Date: Tue, 26 May 2026 11:55:22 +0200 Subject: [PATCH] CAMEL-23614: Add getFromRouteGroup() to Exchange API Co-Authored-By: Claude Opus 4.6 --- .../main/java/org/apache/camel/Exchange.java | 7 +++ .../seda/SedaFromRouteGroupTest.java | 56 +++++++++++++++++++ .../camel/support/AbstractExchange.java | 13 +++++ 3 files changed, 76 insertions(+) create mode 100644 core/camel-core/src/test/java/org/apache/camel/component/seda/SedaFromRouteGroupTest.java diff --git a/core/camel-api/src/main/java/org/apache/camel/Exchange.java b/core/camel-api/src/main/java/org/apache/camel/Exchange.java index 9240f08444c1a..595cecdfd8e49 100644 --- a/core/camel-api/src/main/java/org/apache/camel/Exchange.java +++ b/core/camel-api/src/main/java/org/apache/camel/Exchange.java @@ -772,6 +772,13 @@ public interface Exchange extends VariableAware { @Nullable String getFromRouteId(); + /** + * Returns the route group of the route that originated this message exchange, or null if the route has no + * group or if this exchange was not created by a route consumer. + */ + @Nullable + String getFromRouteGroup(); + /** * Returns the unit of work that this exchange belongs to; which may map to zero, one or more physical transactions */ diff --git a/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaFromRouteGroupTest.java b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaFromRouteGroupTest.java new file mode 100644 index 0000000000000..5d4cd070a6cbf --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/component/seda/SedaFromRouteGroupTest.java @@ -0,0 +1,56 @@ +/* + * 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.component.seda; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.component.mock.MockEndpoint; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertNull; + +public class SedaFromRouteGroupTest extends ContextTestSupport { + + @Test + public void testFromRouteGroup() throws Exception { + MockEndpoint foo = getMockEndpoint("mock:foo"); + foo.expectedMessageCount(1); + + MockEndpoint bar = getMockEndpoint("mock:bar"); + bar.expectedMessageCount(1); + + template.sendBody("seda:foo", "Hello World"); + + assertMockEndpointsSatisfied(); + + assertEquals("myGroup", foo.getReceivedExchanges().get(0).getFromRouteGroup()); + assertNull(bar.getReceivedExchanges().get(0).getFromRouteGroup()); + } + + @Override + protected RouteBuilder createRouteBuilder() { + return new RouteBuilder() { + @Override + public void configure() { + from("seda:foo").routeId("foo").routeGroup("myGroup").to("mock:foo").to("seda:bar"); + + from("seda:bar").routeId("bar").to("mock:bar"); + } + }; + } +} diff --git a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java index 1e9141709309f..a9e72993777c8 100644 --- a/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java +++ b/core/camel-support/src/main/java/org/apache/camel/support/AbstractExchange.java @@ -33,6 +33,7 @@ import org.apache.camel.ExchangePropertyKey; import org.apache.camel.Message; import org.apache.camel.MessageHistory; +import org.apache.camel.Route; import org.apache.camel.SafeCopyProperty; import org.apache.camel.spi.UnitOfWork; import org.apache.camel.spi.VariableRepository; @@ -612,6 +613,18 @@ public String getFromRouteId() { return privateExtension.getFromRouteId(); } + @Override + public String getFromRouteGroup() { + String routeId = getFromRouteId(); + if (routeId != null) { + Route route = getContext().getRoute(routeId); + if (route != null) { + return route.getGroup(); + } + } + return null; + } + @Override public String getExchangeId() { if (exchangeId == null) {