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/11/07 10:37:54 UTC

[camel] branch main updated: CAMEL-18689: camel-endpointdsl - Reloading routes should re-evaluate property placeholders

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 11c3754dfa4 CAMEL-18689: camel-endpointdsl - Reloading routes should re-evaluate property placeholders
11c3754dfa4 is described below

commit 11c3754dfa44fdd34034d7ec0ae16be48732a456
Author: Claus Ibsen <cl...@gmail.com>
AuthorDate: Mon Nov 7 11:37:32 2022 +0100

    CAMEL-18689: camel-endpointdsl - Reloading routes should re-evaluate property placeholders
---
 .../builder/endpoint/AbstractEndpointBuilder.java  |  21 ++--
 .../endpoint/CamelContextReloadStrategyTest.java   | 119 +++++++++++++++++++++
 2 files changed, 130 insertions(+), 10 deletions(-)

diff --git a/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java b/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
index 5cdec3e6bfc..00613d00cec 100644
--- a/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
+++ b/dsl/camel-endpointdsl/src/main/java/org/apache/camel/builder/endpoint/AbstractEndpointBuilder.java
@@ -42,8 +42,8 @@ public class AbstractEndpointBuilder {
     protected final String scheme;
     protected final String path;
     protected final Map<String, Object> properties = new LinkedHashMap<>();
-    protected final Map<String, Map<String, Object>> multivalues = new HashMap<>();
-    private volatile Endpoint resolvedEndpoint;
+    protected final Map<String, Map<String, Object>> multiValues = new HashMap<>();
+    private volatile Map<String, Object> originalProperties;
     private volatile Language simple;
 
     public AbstractEndpointBuilder(String scheme, String path) {
@@ -52,11 +52,13 @@ public class AbstractEndpointBuilder {
     }
 
     public Endpoint resolve(CamelContext context) throws NoSuchEndpointException {
-        if (resolvedEndpoint != null) {
-            return resolvedEndpoint;
-        }
-
         // properties may contain property placeholder which should be resolved
+        if (originalProperties == null) {
+            originalProperties = new LinkedHashMap<>(properties);
+        } else {
+            // reload from original properties before resolving placeholder in case its updated
+            properties.putAll(originalProperties);
+        }
         resolvePropertyPlaceholders(context, properties);
 
         Map<String, Object> remaining = new LinkedHashMap<>();
@@ -68,7 +70,6 @@ public class AbstractEndpointBuilder {
             throw new NoSuchEndpointException(uri.getUri());
         }
 
-        resolvedEndpoint = endpoint;
         return endpoint;
     }
 
@@ -113,9 +114,9 @@ public class AbstractEndpointBuilder {
 
         // sort parameters so it can be regarded as normalized
         Map<String, Object> params = new TreeMap<>();
-        // compute from properties and multivalues
+        // compute from properties and multi values
         computeProperties(remaining, camelContext, bindToRegistry, params, properties);
-        for (Map<String, Object> map : multivalues.values()) {
+        for (Map<String, Object> map : multiValues.values()) {
             computeProperties(remaining, camelContext, bindToRegistry, params, map);
         }
         if (!remaining.isEmpty()) {
@@ -179,7 +180,7 @@ public class AbstractEndpointBuilder {
     }
 
     public void doSetMultiValueProperty(String name, String key, Object value) {
-        Map<String, Object> map = multivalues.computeIfAbsent(name, k -> new LinkedHashMap<>());
+        Map<String, Object> map = multiValues.computeIfAbsent(name, k -> new LinkedHashMap<>());
         map.put(key, value);
     }
 
diff --git a/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/CamelContextReloadStrategyTest.java b/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/CamelContextReloadStrategyTest.java
new file mode 100644
index 00000000000..4c3aa412e1d
--- /dev/null
+++ b/dsl/camel-endpointdsl/src/test/java/org/apache/camel/builder/endpoint/CamelContextReloadStrategyTest.java
@@ -0,0 +1,119 @@
+/*
+ * 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.builder.endpoint;
+
+import org.apache.camel.CamelContext;
+import org.apache.camel.builder.RouteBuilder;
+import org.apache.camel.component.mock.MockEndpoint;
+import org.apache.camel.component.seda.SedaConsumerNotAvailableException;
+import org.apache.camel.spi.ContextReloadStrategy;
+import org.apache.camel.spi.PropertiesComponent;
+import org.apache.camel.spi.PropertiesSource;
+import org.apache.camel.support.DefaultContextReloadStrategy;
+import org.apache.camel.support.service.ServiceHelper;
+import org.apache.camel.support.service.ServiceSupport;
+import org.apache.camel.test.junit5.CamelTestSupport;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import static org.assertj.core.api.Assertions.fail;
+
+public class CamelContextReloadStrategyTest extends CamelTestSupport {
+
+    @Test
+    public void testContextReload() throws Exception {
+        MockEndpoint mock = getMockEndpoint("mock:result");
+        mock.expectedBodiesReceived("Hello World");
+        template.sendBody("direct:start", "Hello World");
+        mock.assertIsSatisfied();
+
+        // simulate that an external system forces reload
+        // where we reload the context
+        ContextReloadStrategy crs = context.hasService(ContextReloadStrategy.class);
+        Assertions.assertNotNull(crs);
+        crs.onReload("CamelContextReloadStrategyTest");
+
+        // need to re-get endpoint after reload
+        mock = getMockEndpoint("mock:result");
+
+        mock.reset();
+        mock.expectedMessageCount(0);
+        try {
+            template.sendBody("direct:start", "Bye World");
+            fail("Should throw exception");
+        } catch (Exception e) {
+            Assertions.assertInstanceOf(SedaConsumerNotAvailableException.class, e.getCause());
+        }
+        mock.assertIsSatisfied();
+    }
+
+    @Override
+    protected CamelContext createCamelContext() throws Exception {
+        CamelContext context = super.createCamelContext();
+
+        PropertiesComponent pc = context.getPropertiesComponent();
+        MySource my = new MySource();
+        ServiceHelper.startService(my);
+        pc.addPropertiesSource(my);
+
+        ContextReloadStrategy crs = new DefaultContextReloadStrategy();
+        context.addService(crs);
+
+        return context;
+    }
+
+    @Override
+    protected RouteBuilder createRouteBuilder() throws Exception {
+        return new EndpointRouteBuilder() {
+            @Override
+            public void configure() throws Exception {
+                from("direct:start")
+                        .to(seda("{{cheese}}").failIfNoConsumers(property("myfail")));
+
+                from(seda("foo1")).to("mock:result");
+            }
+        };
+    }
+
+    private class MySource extends ServiceSupport implements PropertiesSource {
+
+        private int counter;
+
+        @Override
+        public String getName() {
+            return "my";
+        }
+
+        @Override
+        public String getProperty(String name) {
+            if ("cheese".equals(name)) {
+                return "foo" + counter;
+            }
+            if ("myfail".equals(name)) {
+                return counter == 1 ? "false" : "true";
+            }
+            return null;
+        }
+
+        @Override
+        protected void doStart() throws Exception {
+            // the properties source will be restarted
+            counter++;
+        }
+    }
+
+}