This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch camel-3.18.x in repository https://gitbox.apache.org/repos/asf/camel.git
The following commit(s) were added to refs/heads/camel-3.18.x by this push: new bfe702f99c2 AdviceWith - make it easier to use for non lambda style. bfe702f99c2 is described below commit bfe702f99c2f24abcc9ece8c4859e49c274918da Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Sun Dec 4 10:06:19 2022 +0100 AdviceWith - make it easier to use for non lambda style. --- .../java/org/apache/camel/builder/AdviceWith.java | 33 +++++++++ .../camel/issues/AdviceWithCBRRouteIdTest.java | 86 ++++++++++++++++++++++ 2 files changed, 119 insertions(+) diff --git a/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWith.java b/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWith.java index e85ef704959..110a4159c3b 100644 --- a/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWith.java +++ b/core/camel-core-model/src/main/java/org/apache/camel/builder/AdviceWith.java @@ -122,6 +122,39 @@ public final class AdviceWith { }); } + /** + * Advices this route with the route builder. + * <p/> + * <b>Important:</b> It is recommended to only advice a given route once (you can of course advice multiple routes). + * If you do it multiple times, then it may not work as expected, especially when any kind of error handling is + * involved. The Camel team plan for Camel 3.0 to support this as internal refactorings in the routing engine is + * needed to support this properly. + * <p/> + * You can use a regular {@link RouteBuilder} but the specialized {@link AdviceWithRouteBuilder} has additional + * features when using the advice with feature. We therefore suggest you to use the {@link AdviceWithRouteBuilder}. + * <p/> + * The advice process will add the interceptors, on exceptions, on completions etc. configured from the route + * builder to this route. + * <p/> + * This is mostly used for testing purpose to add interceptors and the likes to an existing route. + * <p/> + * Will stop and remove the old route from camel context and add and start this new advised route. + * + * @param routeId either the route id as a string value, or <tt>null</tt> to chose the 1st route, or you can + * specify a number for the n'th route, or provide the route definition instance directly as + * well. + * @param camelContext the camel context + * @param builder the route builder + * @return a new route which is this route merged with the route builder + * @throws Exception can be thrown from the route builder + * @see AdviceWithRouteBuilder + */ + public static RouteDefinition adviceWith(Object routeId, CamelContext camelContext, RouteBuilder builder) + throws Exception { + RouteDefinition rd = findRouteDefinition(camelContext, routeId); + return adviceWith(rd, camelContext, builder); + } + /** * Advices this route with the route builder. * <p/> diff --git a/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithCBRRouteIdTest.java b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithCBRRouteIdTest.java new file mode 100644 index 00000000000..9055f6492e7 --- /dev/null +++ b/core/camel-core/src/test/java/org/apache/camel/issues/AdviceWithCBRRouteIdTest.java @@ -0,0 +1,86 @@ +/* + * 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.issues; + +import org.apache.camel.ContextTestSupport; +import org.apache.camel.builder.AdviceWith; +import org.apache.camel.builder.AdviceWithRouteBuilder; +import org.apache.camel.builder.RouteBuilder; +import org.junit.jupiter.api.Test; + +public class AdviceWithCBRRouteIdTest extends ContextTestSupport { + + @Test + public void testAdviceCBR() throws Exception { + AdviceWith.adviceWith("myRoute", context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + weaveById("foo").after().to("mock:foo2"); + weaveById("bar").after().to("mock:bar2"); + } + }); + + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:foo2").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:bar2").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:baz").expectedBodiesReceived("Hi World"); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); + template.sendBodyAndHeader("direct:start", "Bye World", "bar", "123"); + template.sendBody("direct:start", "Hi World"); + + assertMockEndpointsSatisfied(); + } + + @Test + public void testAdviceToStringCBR() throws Exception { + // pick first route from index 0 + AdviceWith.adviceWith(0, context, new AdviceWithRouteBuilder() { + @Override + public void configure() throws Exception { + weaveByToString("To[mock:foo]").after().to("mock:foo2"); + weaveByToString("To[mock:bar]").after().to("mock:bar2"); + } + }); + + getMockEndpoint("mock:foo").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:foo2").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:bar").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:bar2").expectedBodiesReceived("Bye World"); + getMockEndpoint("mock:baz").expectedBodiesReceived("Hi World"); + + template.sendBodyAndHeader("direct:start", "Hello World", "foo", "123"); + template.sendBodyAndHeader("direct:start", "Bye World", "bar", "123"); + template.sendBody("direct:start", "Hi World"); + + assertMockEndpointsSatisfied(); + } + + @Override + protected RouteBuilder createRouteBuilder() throws Exception { + return new RouteBuilder() { + @Override + public void configure() throws Exception { + from("direct:start").routeId("myRoute") + .choice().when(header("foo")).to("mock:foo").id("foo").when(header("bar")).to("mock:bar") + .id("bar").otherwise().to("mock:baz").id("baz"); + } + }; + } + +}