This is an automated email from the ASF dual-hosted git repository. davsclaus pushed a commit to branch reload in repository https://gitbox.apache.org/repos/asf/camel.git
commit 49f1f25ab66fa6d87b512beddcd52f313ffe0879 Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Thu Sep 1 15:27:33 2022 +0200 CAMEL-18267: ContextReloadStrategy to reload all routes after external changes such as properties/vault updated. --- .../apache/camel/spi/ContextReloadStrategy.java | 5 +-- .../impl/engine/DefaultContextReloadStrategy.java | 1 - ...ntextReloadStrategyPropertiesFunctionTest.java} | 46 ++++++++++------------ .../camel/impl/CamelContextReloadStrategyTest.java | 9 ++++- 4 files changed, 30 insertions(+), 31 deletions(-) diff --git a/core/camel-api/src/main/java/org/apache/camel/spi/ContextReloadStrategy.java b/core/camel-api/src/main/java/org/apache/camel/spi/ContextReloadStrategy.java index b240f26dc04..d09f0d2660b 100644 --- a/core/camel-api/src/main/java/org/apache/camel/spi/ContextReloadStrategy.java +++ b/core/camel-api/src/main/java/org/apache/camel/spi/ContextReloadStrategy.java @@ -23,9 +23,8 @@ import org.apache.camel.StaticService; /** * SPI strategy for reloading {@link CamelContext}. * - * The reloading is limited to: - * - all routes (reload only changes from routes that has been loaded as a {@link Resource}. - * - all {@link LoadablePropertiesSource} properties. + * The reloading is limited to: - all routes (reload only changes from routes that has been loaded as a + * {@link Resource}. - all {@link LoadablePropertiesSource} properties. * * General services in the {@link CamelContext} is not reloaded. * diff --git a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextReloadStrategy.java b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextReloadStrategy.java index 5d3700dae71..3b524bda07b 100644 --- a/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextReloadStrategy.java +++ b/core/camel-base-engine/src/main/java/org/apache/camel/impl/engine/DefaultContextReloadStrategy.java @@ -17,7 +17,6 @@ package org.apache.camel.impl.engine; import org.apache.camel.CamelContext; -import org.apache.camel.ExtendedCamelContext; import org.apache.camel.api.management.ManagedAttribute; import org.apache.camel.api.management.ManagedOperation; import org.apache.camel.spi.ContextReloadStrategy; diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyPropertiesFunctionTest.java similarity index 72% copy from core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java copy to core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyPropertiesFunctionTest.java index 56d3abf00ba..f6c45292848 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyPropertiesFunctionTest.java @@ -23,35 +23,36 @@ import org.apache.camel.component.mock.MockEndpoint; import org.apache.camel.impl.engine.DefaultContextReloadStrategy; import org.apache.camel.spi.ContextReloadStrategy; import org.apache.camel.spi.PropertiesComponent; -import org.apache.camel.spi.PropertiesSource; -import org.apache.camel.support.service.ServiceHelper; -import org.apache.camel.support.service.ServiceSupport; +import org.apache.camel.spi.PropertiesFunction; import org.junit.jupiter.api.Assertions; import org.junit.jupiter.api.Test; -public class CamelContextReloadStrategyTest extends ContextTestSupport { +public class CamelContextReloadStrategyPropertiesFunctionTest extends ContextTestSupport { + + private MyFunction my = new MyFunction(); @Test public void testContextReload() throws Exception { - Assertions.assertEquals("Hello 1", context.resolvePropertyPlaceholders("{{hello}}")); - MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedBodiesReceived("Hello 1"); + mock.expectedBodiesReceived("Bye 1", "Bye 1"); template.sendBody("direct:start", "Camel"); + template.sendBody("direct:start", "Dog"); mock.assertIsSatisfied(); + // simulate that an external system forces reload where we update the counter + // and reload the context + my.incCounter(); ContextReloadStrategy crs = context.hasService(ContextReloadStrategy.class); Assertions.assertNotNull(crs); crs.onReload("CamelContextReloadStrategyTest"); - Assertions.assertEquals("Hello 2", context.resolvePropertyPlaceholders("{{hello}}")); - // need to re-get endpoint after reload mock = getMockEndpoint("mock:result"); mock.reset(); - mock.expectedBodiesReceived("Hello 2"); + mock.expectedBodiesReceived("Bye 2", "Bye 2"); template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Moon"); mock.assertIsSatisfied(); } @@ -60,9 +61,7 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { CamelContext context = super.createCamelContext(); PropertiesComponent pc = context.getPropertiesComponent(); - MySource my = new MySource(); - ServiceHelper.startService(my); - pc.addPropertiesSource(my); + pc.addPropertiesFunction(my); ContextReloadStrategy crs = new DefaultContextReloadStrategy(); context.addService(crs); @@ -76,15 +75,19 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { @Override public void configure() throws Exception { from("direct:start") - .setBody(constant("{{hello}}")) + .setBody(constant("{{my:Bye}}")) .to("mock:result"); } }; } - private class MySource extends ServiceSupport implements PropertiesSource { + private class MyFunction implements PropertiesFunction { - private int counter; + private int counter = 1; + + public void incCounter() { + counter++; + } @Override public String getName() { @@ -92,16 +95,9 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { } @Override - public String getProperty(String name) { - if ("hello".equals(name)) { - return "Hello " + counter; - } - return null; + public String apply(String remainder) { + return remainder + " " + counter; } - @Override - protected void doStart() throws Exception { - counter++; - } } } diff --git a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java index 56d3abf00ba..5a8cd4683ed 100644 --- a/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/impl/CamelContextReloadStrategyTest.java @@ -36,10 +36,13 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { Assertions.assertEquals("Hello 1", context.resolvePropertyPlaceholders("{{hello}}")); MockEndpoint mock = getMockEndpoint("mock:result"); - mock.expectedBodiesReceived("Hello 1"); + mock.expectedBodiesReceived("Hello 1", "Hello 1"); template.sendBody("direct:start", "Camel"); + template.sendBody("direct:start", "Dog"); 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"); @@ -50,8 +53,9 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { mock = getMockEndpoint("mock:result"); mock.reset(); - mock.expectedBodiesReceived("Hello 2"); + mock.expectedBodiesReceived("Hello 2", "Hello 2"); template.sendBody("direct:start", "World"); + template.sendBody("direct:start", "Moon"); mock.assertIsSatisfied(); } @@ -101,6 +105,7 @@ public class CamelContextReloadStrategyTest extends ContextTestSupport { @Override protected void doStart() throws Exception { + // the properties source will be restarted counter++; } }