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 2020/03/20 08:47:03 UTC

[camel] branch master updated: CAMEL-14599: Fixed advicewith lambda setting log as xml on|off.

This is an automated email from the ASF dual-hosted git repository.

davsclaus pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/camel.git


The following commit(s) were added to refs/heads/master by this push:
     new 8b28f70  CAMEL-14599: Fixed advicewith lambda setting log as xml on|off.
8b28f70 is described below

commit 8b28f70a2d79da3d5e64eeae03ba29501d12f757
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Fri Mar 20 09:46:40 2020 +0100

    CAMEL-14599: Fixed advicewith lambda setting log as xml on|off.
---
 .../camel/builder/AdviceWithRouteBuilder.java      | 49 ++++++++++++++++++++--
 .../org/apache/camel/reifier/RouteReifier.java     | 11 +++--
 .../interceptor/AdviceWithLambdaTest.java          | 16 +++++++
 3 files changed, 70 insertions(+), 6 deletions(-)

diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java b/core/camel-core-engine/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
index 074e796..8c3bed3 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/builder/AdviceWithRouteBuilder.java
@@ -93,9 +93,52 @@ public abstract class AdviceWithRouteBuilder extends RouteBuilder {
         return RouteReifier.adviceWith(rd, camelContext, new AdviceWithRouteBuilder() {
             @Override
             public void configure() throws Exception {
-                if (builder instanceof AdviceWithRouteBuilder) {
-                    setLogRouteAsXml(((AdviceWithRouteBuilder) builder).isLogRouteAsXml());
-                }
+                builder.accept(this);
+            }
+        });
+    }
+
+    /**
+     * Advices this route with the route builder using a lambda expression. It
+     * can be used as following:
+     *
+     * <pre>
+     * AdviceWithRouteBuilder.adviceWith(context, "myRoute", false, a ->
+     *     a.weaveAddLast().to("mock:result");
+     * </pre>
+     * <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/>
+     * 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 camelContext the camel context
+     * @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 logXml whether to log the before and after advices routes as XML to the log (this can be turned off to perform faster)
+     * @param builder the advice with route builder
+     * @return a new route which is this route merged with the route builder
+     * @throws Exception can be thrown from the route builder
+     */
+    public static RouteDefinition adviceWith(CamelContext camelContext, Object routeId, boolean logXml, ThrowingConsumer<AdviceWithRouteBuilder, Exception> builder) throws Exception {
+        RouteDefinition rd = findRouteDefinition(camelContext, routeId);
+
+        return RouteReifier.adviceWith(rd, camelContext, new AdviceWithRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                setLogRouteAsXml(logXml);
                 builder.accept(this);
             }
         });
diff --git a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/RouteReifier.java b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/RouteReifier.java
index 43f18f9..f670c24 100644
--- a/core/camel-core-engine/src/main/java/org/apache/camel/reifier/RouteReifier.java
+++ b/core/camel-core-engine/src/main/java/org/apache/camel/reifier/RouteReifier.java
@@ -166,21 +166,26 @@ public class RouteReifier extends ProcessorReifier<RouteDefinition> {
 
         // inject this route into the advice route builder so it can access this route
         // and offer features to manipulate the route directly
-        boolean logRoutesAsXml = true;
         if (builder instanceof AdviceWithRouteBuilder) {
             AdviceWithRouteBuilder arb = (AdviceWithRouteBuilder)builder;
             arb.setOriginalRoute(definition);
-            logRoutesAsXml = arb.isLogRouteAsXml();
         }
 
         // configure and prepare the routes from the builder
         RoutesDefinition routes = builder.configureRoutes(camelContext);
 
+        // was logging enabled or disabled
+        boolean logRoutesAsXml = true;
+        if (builder instanceof AdviceWithRouteBuilder) {
+            AdviceWithRouteBuilder arb = (AdviceWithRouteBuilder)builder;
+            logRoutesAsXml = arb.isLogRouteAsXml();
+        }
+
         log.debug("AdviceWith routes: {}", routes);
 
         // we can only advice with a route builder without any routes
         if (!builder.getRouteCollection().getRoutes().isEmpty()) {
-            throw new IllegalArgumentException("You can only advice from a RouteBuilder which has no existing routes." + " Remove all routes from the route builder.");
+            throw new IllegalArgumentException("You can only advice from a RouteBuilder which has no existing routes. Remove all routes from the route builder.");
         }
         // we can not advice with error handlers (if you added a new error
         // handler in the route builder)
diff --git a/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithLambdaTest.java b/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithLambdaTest.java
index 72c8707..8f99a5b 100644
--- a/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithLambdaTest.java
+++ b/core/camel-core/src/test/java/org/apache/camel/processor/interceptor/AdviceWithLambdaTest.java
@@ -52,6 +52,22 @@ public class AdviceWithLambdaTest extends ContextTestSupport {
     // END SNIPPET: e1
 
     @Test
+    public void testAdvisedNoLog() throws Exception {
+        AdviceWithRouteBuilder.adviceWith(context, null, false, a -> {
+            a.weaveByToUri("mock:result").remove();
+            a.weaveAddLast().transform().constant("Bye World");
+        });
+
+        getMockEndpoint("mock:foo").expectedMessageCount(1);
+        getMockEndpoint("mock:result").expectedMessageCount(0);
+
+        Object out = template.requestBody("direct:start", "Hello World");
+        assertEquals("Bye World", out);
+
+        assertMockEndpointsSatisfied();
+    }
+
+    @Test
     public void testAdvisedNoNewRoutesAllowed() throws Exception {
         try {
             AdviceWithRouteBuilder.adviceWith(context, 0, a -> {