DELTASPIKE-1277 actively update the Config ConfigSources can now actively tell the Config that they got new values. Might get used to eagerly push changes to the clients.
Project: http://git-wip-us.apache.org/repos/asf/deltaspike/repo Commit: http://git-wip-us.apache.org/repos/asf/deltaspike/commit/73cb1ca7 Tree: http://git-wip-us.apache.org/repos/asf/deltaspike/tree/73cb1ca7 Diff: http://git-wip-us.apache.org/repos/asf/deltaspike/diff/73cb1ca7 Branch: refs/heads/master Commit: 73cb1ca7ab185a09279cf8cd1d1331b436fca467 Parents: 91e3a17 Author: Mark Struberg <strub...@apache.org> Authored: Tue Mar 6 15:05:54 2018 +0100 Committer: Mark Struberg <strub...@apache.org> Committed: Tue Mar 6 15:05:54 2018 +0100 ---------------------------------------------------------------------- .../core/spi/config/ConfigSource.java | 21 +++++++++++++++++++- .../deltaspike/core/impl/config/ConfigImpl.java | 18 +++++++++++++++++ 2 files changed, 38 insertions(+), 1 deletion(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/deltaspike/blob/73cb1ca7/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java index 4c71b90..10fe39d 100644 --- a/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java +++ b/deltaspike/core/api/src/main/java/org/apache/deltaspike/core/spi/config/ConfigSource.java @@ -19,6 +19,8 @@ package org.apache.deltaspike.core.spi.config; import java.util.Map; +import java.util.Set; +import java.util.function.Consumer; /** * <p>Implement this interfaces to provide a ConfigSource. @@ -102,5 +104,22 @@ public interface ConfigSource * @return true if this ConfigSource should be scanned for its list of properties, * false if it should not be scanned. */ - boolean isScannable(); + boolean isScannable(); + + /** + * This callback should get invoked if an attribute change got detected inside the ConfigSource. + * + * An example would be a database backed ConfigSource which scans the DB every second in a background task. + * And once it detects a change in values, it will notify the Config about the changed attributes + * by invoking {@code reportAttributeChange.accept(changedKeys);} + * + * @param reportAttributeChange will be set by the {@link org.apache.deltaspike.core.api.config.Config} after this + * {@code ConfigSource} got created and before any configured values + * get served. + */ + default void setOnAttributeChange(Consumer<Set<String>> reportAttributeChange) + { + // do nothing by default. Just for compat with older ConfigSources. + } + } http://git-wip-us.apache.org/repos/asf/deltaspike/blob/73cb1ca7/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java ---------------------------------------------------------------------- diff --git a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java index 6f60dda..af0e414 100644 --- a/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java +++ b/deltaspike/core/impl/src/main/java/org/apache/deltaspike/core/impl/config/ConfigImpl.java @@ -29,6 +29,7 @@ import java.util.ArrayList; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Set; import java.util.concurrent.CopyOnWriteArrayList; import java.util.logging.Level; import java.util.logging.Logger; @@ -45,6 +46,9 @@ public class ConfigImpl implements Config private ConfigSource[] configSources; private List<ConfigFilter> configFilters; + // volatile to a.) make the read/write behave atomic and b.) guarantee multi-thread safety + private volatile long lastChanged = 0; + public ConfigImpl(ClassLoader classLoader) { this.classLoader = classLoader; @@ -97,6 +101,7 @@ public class ConfigImpl implements Config { for (ConfigSource configSource : this.configSources) { + configSource.setOnAttributeChange(this::onAttributeChange); allConfigSources.add(configSource); } } @@ -161,4 +166,17 @@ public class ConfigImpl implements Config return configSources.toArray(new ConfigSource[configSources.size()]); } + public void onAttributeChange(Set<String> attributesChanged) + { + lastChanged = System.nanoTime(); + } + + /** + * @return the nanoTime when the last change got reported by a ConfigSource + */ + public long getLastChanged() + { + return lastChanged; + } + }