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 05aeabe0124 CAMEL-21775: camel-core: Split or Multicast in onException route cannot be started using Supervised route controller 05aeabe0124 is described below commit 05aeabe0124c6667c1897030c70223a68ba825c5 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sat Feb 22 09:18:01 2025 +0100 CAMEL-21775: camel-core: Split or Multicast in onException route cannot be started using Supervised route controller --- .../apache/camel/language/bean/BeanExpression.java | 4 +- .../org/apache/camel/impl/engine/DefaultRoute.java | 4 +- ...rvisingRouteControllerSplitOnExceptionTest.java | 87 ++++++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) diff --git a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java index 6b96c07a966..aa502a2b47b 100644 --- a/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java +++ b/components/camel-bean/src/main/java/org/apache/camel/language/bean/BeanExpression.java @@ -349,7 +349,9 @@ public class BeanExpression implements Expression, Predicate { private static Object invokeBean(BeanHolder beanHolder, String beanName, String methodName, Exchange exchange) { Object result; - try (BeanExpressionProcessor processor = new BeanExpressionProcessor(beanHolder)) { + try { + // do not close BeanExpressionProcessor as beanHolder should not be closed + BeanExpressionProcessor processor = new BeanExpressionProcessor(beanHolder); if (methodName != null) { processor.setMethod(methodName); diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java index fc076c25d4b..4cc2fc9c335 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultRoute.java @@ -692,12 +692,12 @@ public class DefaultRoute extends ServiceSupport implements Route { services.add(service); } for (Processor p : onCompletions.values()) { - if (processor instanceof Service service) { + if (p instanceof Service service) { services.add(service); } } for (Processor p : onExceptions.values()) { - if (processor instanceof Service service) { + if (p instanceof Service service) { services.add(service); } } diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/SupervisingRouteControllerSplitOnExceptionTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/SupervisingRouteControllerSplitOnExceptionTest.java new file mode 100644 index 00000000000..710f56f00d4 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/processor/SupervisingRouteControllerSplitOnExceptionTest.java @@ -0,0 +1,87 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; + +import org.apache.camel.Body; +import org.apache.camel.CamelContext; +import org.apache.camel.ContextTestSupport; +import org.apache.camel.Message; +import org.apache.camel.RoutesBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.apache.camel.spi.SupervisingRouteController; +import org.junit.jupiter.api.Test; + +public class SupervisingRouteControllerSplitOnExceptionTest extends ContextTestSupport { + + @Override + protected CamelContext createCamelContext() throws Exception { + CamelContext context = super.createCamelContext(); + + SupervisingRouteController src = context.getRouteController().supervising(); + src.setBackOffDelay(25); + src.setBackOffMaxAttempts(3); + src.setInitialDelay(100); + src.setThreadPoolSize(1); + + return context; + } + + @Test + public void testSupervising() throws Exception { + getMockEndpoint("mock:error").expectedMessageCount(1); + getMockEndpoint("mock:uk").expectedMessageCount(0); + getMockEndpoint("mock:other").expectedMessageCount(0); + + template.sendBody("direct:start", "<hello>World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RoutesBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + onException().handled(true).split().method(SupervisingRouteControllerSplitOnExceptionTest.class, "mySplit") + .streaming().log("Exception occurred").to("mock:error"); + + from("direct:start") + .choice() + .when(xpath("/person/city = 'London'")) + .log("UK message") + .to("mock:uk") + .otherwise() + .log("Other message") + .to("mock:other"); + } + }; + } + + public static List<Message> mySplit(@Body Message inputMessage) { + List<Message> outputMessages = new ArrayList<>(); + + Message outputMessage = inputMessage.copy(); + outputMessage.setBody(inputMessage.getBody()); + outputMessages.add(outputMessage); + + return outputMessages; + } + +}