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 2022/12/04 09:06:33 UTC

[camel] branch main updated: AdviceWith - make it easier to use for non lambda style.

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 b16733a73ea AdviceWith - make it easier to use for non lambda style.
b16733a73ea is described below

commit b16733a73ea79de4e38c347e0cd1fcacf74a8ddf
Author: Claus Ibsen <cl...@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");
+            }
+        };
+    }
+
+}