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
commit d74b298e0a092ac9cdd8a8ef17731fe06b53369b Author: Claus Ibsen <claus.ib...@gmail.com> AuthorDate: Mon Jul 1 12:49:46 2019 +0200 CAMEL-13709: Properties component - Optimise to not call loadProperties to frequently --- .../camel/component/properties/PropertiesComponent.java | 13 +++++++++++-- .../properties/PropertiesComponentEIPChoiceSimpleTest.java | 9 +++++---- 2 files changed, 16 insertions(+), 6 deletions(-) diff --git a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java index eeafbfb..ab39061 100644 --- a/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java +++ b/components/camel-properties/src/main/java/org/apache/camel/component/properties/PropertiesComponent.java @@ -101,6 +101,7 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. @SuppressWarnings("unchecked") private final Map<CacheKey, Properties> cacheMap = LRUCacheFactory.newLRUSoftCache(1000); + private transient Properties cachedProperties; private final Map<String, PropertiesFunction> functions = new LinkedHashMap<>(); private PropertiesResolver propertiesResolver = new DefaultPropertiesResolver(this); private PropertiesParser propertiesParser = new DefaultPropertiesParser(this); @@ -184,7 +185,11 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. } public String parseUri(String uri) { - return parseUri(uri, locations); + // optimise to only load properties once as we use the configured locations + if (cachedProperties == null) { + cachedProperties = doLoadProperties(locations); + } + return parseUri(uri, cachedProperties); } public String parseUri(String uri, String... locations) { @@ -254,7 +259,10 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. protected String parseUri(String uri, List<PropertiesLocation> paths) { Properties prop = doLoadProperties(paths); + return parseUri(uri, prop); + } + protected String parseUri(String uri, Properties prop) { // enclose tokens if missing if (!uri.contains(prefixToken) && !uri.startsWith(prefixToken)) { uri = prefixToken + uri; @@ -660,8 +668,9 @@ public class PropertiesComponent extends DefaultComponent implements org.apache. @Override protected void doStop() throws Exception { cacheMap.clear(); - super.doStop(); + cachedProperties = null; ServiceHelper.stopAndShutdownService(sources); + super.doStop(); } private List<PropertiesLocation> parseLocations(List<PropertiesLocation> locations) { diff --git a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEIPChoiceSimpleTest.java b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEIPChoiceSimpleTest.java index 04dedcf..9e82f21 100644 --- a/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEIPChoiceSimpleTest.java +++ b/core/camel-core/src/test/java/org/apache/camel/component/properties/PropertiesComponentEIPChoiceSimpleTest.java @@ -25,11 +25,12 @@ public class PropertiesComponentEIPChoiceSimpleTest extends ContextTestSupport { @Test public void testChoice() throws Exception { - getMockEndpoint("mock:camel").expectedBodiesReceived("Hello Camel"); - getMockEndpoint("mock:other").expectedBodiesReceived("Hello World"); + getMockEndpoint("mock:camel").expectedBodiesReceived("Hello Camel", "Hi Camel"); + getMockEndpoint("mock:other").expectedBodiesReceived("Bye World"); - template.sendBody("direct:start", "Hello Camel"); - template.sendBody("direct:start", "Hello World"); + template.sendBody("direct:start", context.resolvePropertyPlaceholders("Hello {{cool.name}}")); + template.sendBody("direct:start", context.resolvePropertyPlaceholders("Hi {{cool.name}}")); + template.sendBody("direct:start", "Bye World"); assertMockEndpointsSatisfied(); }