Skip to content

Commit

Permalink
Merge pull request #184 from scalecube/shortcut-for-reload-interva
Browse files Browse the repository at this point in the history
Added .noReload()
  • Loading branch information
artem-v authored Sep 28, 2020
2 parents 0ee75d1 + 3b38716 commit 058d1a9
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 35 deletions.
24 changes: 13 additions & 11 deletions config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -90,17 +90,19 @@ protected boolean removeEldestEntry(Map.Entry<ConfigEvent, Object> eldest) {
void init() {
loadAndNotify();

reloadExecutor.scheduleAtFixedRate(
() -> {
try {
loadAndNotify();
} catch (Exception e) {
LOGGER.error("[loadAndNotify] Exception occurred, cause: " + e);
}
},
settings.getReloadIntervalSec(),
settings.getReloadIntervalSec(),
TimeUnit.SECONDS);
if (settings.isReloadEnabled()) {
reloadExecutor.scheduleAtFixedRate(
() -> {
try {
loadAndNotify();
} catch (Exception e) {
LOGGER.error("[loadAndNotify] Exception occurred, cause: " + e);
}
},
settings.getReloadIntervalSec(),
settings.getReloadIntervalSec(),
TimeUnit.SECONDS);
}

if (settings.isJmxEnabled()) {
registerJmxMBean();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import java.util.LinkedHashMap;
import java.util.LinkedList;
import java.util.Map;
import java.util.StringJoiner;

/**
* Represents settings of config registry.
Expand All @@ -21,24 +22,24 @@ public final class ConfigRegistrySettings {
public static final boolean DEFAULT_JMX_ENABLED = true;
public static final String DEFAULT_JMX_MBEAN_NAME = "io.scalecube.config:name=ConfigRegistry";

private final Map<String, ConfigSource> sources;
private final String host;
private final int reloadIntervalSec;
private final int recentConfigEventsNum;
private final Map<String, ConfigEventListener> listeners;
private final Map<String, ConfigSource> sources;
private final String host;
private final boolean jmxEnabled;
private final String jmxMBeanName;

private ConfigRegistrySettings(Builder builder) {
this.reloadIntervalSec = builder.reloadIntervalSec;
this.recentConfigEventsNum = builder.recentConfigEventsNum;
this.listeners = Collections.unmodifiableMap(new HashMap<>(builder.listeners));
Map<String, ConfigSource> sourcesTmp = new LinkedHashMap<>(builder.sources.size());
for (String name : builder.sourceOrder) {
sourcesTmp.put(name, builder.sources.get(name));
}
this.sources = Collections.unmodifiableMap(sourcesTmp);
this.host = builder.host != null ? builder.host : resolveLocalHost();
this.reloadIntervalSec = builder.reloadIntervalSec;
this.recentConfigEventsNum = builder.recentConfigEventsNum;
this.listeners = Collections.unmodifiableMap(new HashMap<>(builder.listeners));
this.jmxEnabled = builder.jmxEnabled;
this.jmxMBeanName = builder.jmxMBeanName;
}
Expand All @@ -59,6 +60,10 @@ public int getReloadIntervalSec() {
return reloadIntervalSec;
}

public boolean isReloadEnabled() {
return reloadIntervalSec != Integer.MAX_VALUE;
}

public int getRecentConfigEventsNum() {
return recentConfigEventsNum;
}
Expand All @@ -85,35 +90,34 @@ public String getJmxMBeanName() {

@Override
public String toString() {
return "{\"reloadIntervalSec\":\" "
+ reloadIntervalSec
+ "\",\"recentConfigEventsNum\":\""
+ recentConfigEventsNum
+ "\",\"host\":\""
+ host
+ "\",\"jmxEnabled\":\""
+ jmxEnabled
+ "\",\"jmxMBeanName\":\""
+ jmxMBeanName
+ "\",\"listeners\":\""
+ listeners
+ "\",\"sources\":\""
+ sources
+ "\"}";
return new StringJoiner(", ", ConfigRegistrySettings.class.getSimpleName() + "[", "]")
.add("sources=" + sources)
.add("host='" + host + "'")
.add("reloadIntervalSec=" + reloadIntervalSec)
.add("recentConfigEventsNum=" + recentConfigEventsNum)
.add("listeners=" + listeners)
.add("jmxEnabled=" + jmxEnabled)
.add("jmxMBeanName='" + jmxMBeanName + "'")
.toString();
}

public static class Builder {
private final LinkedList<String> sourceOrder = new LinkedList<>();
private final Map<String, ConfigSource> sources = new HashMap<>();
private final String host = null;
private int reloadIntervalSec = DEFAULT_RELOAD_PERIOD_SEC;
private int recentConfigEventsNum = DEFAULT_RECENT_EVENTS_NUM;
private Map<String, ConfigEventListener> listeners = new HashMap<>();
private LinkedList<String> sourceOrder = new LinkedList<>();
private Map<String, ConfigSource> sources = new HashMap<>();
private String host = null;
private final Map<String, ConfigEventListener> listeners = new HashMap<>();
private boolean jmxEnabled = DEFAULT_JMX_ENABLED;
private String jmxMBeanName = DEFAULT_JMX_MBEAN_NAME;

private Builder() {}

public Builder noReload() {
this.reloadIntervalSec = Integer.MAX_VALUE;
return this;
}

public Builder reloadIntervalSec(int reloadPeriodSec) {
this.reloadIntervalSec = reloadPeriodSec;
return this;
Expand Down

0 comments on commit 058d1a9

Please sign in to comment.