From 3b387162b36f2e02594479e49ee984a15a356f5d Mon Sep 17 00:00:00 2001 From: Artem Vysochyn Date: Mon, 28 Sep 2020 16:41:27 +0300 Subject: [PATCH] Added .noReload() --- .../scalecube/config/ConfigRegistryImpl.java | 24 +++++---- .../config/ConfigRegistrySettings.java | 52 ++++++++++--------- 2 files changed, 41 insertions(+), 35 deletions(-) diff --git a/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java b/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java index 37d7a3be..e72c452d 100644 --- a/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java +++ b/config/src/main/java/io/scalecube/config/ConfigRegistryImpl.java @@ -90,17 +90,19 @@ protected boolean removeEldestEntry(Map.Entry 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(); diff --git a/config/src/main/java/io/scalecube/config/ConfigRegistrySettings.java b/config/src/main/java/io/scalecube/config/ConfigRegistrySettings.java index 8c6c3740..de68ad22 100644 --- a/config/src/main/java/io/scalecube/config/ConfigRegistrySettings.java +++ b/config/src/main/java/io/scalecube/config/ConfigRegistrySettings.java @@ -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. @@ -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 sources; + private final String host; private final int reloadIntervalSec; private final int recentConfigEventsNum; private final Map listeners; - private final Map 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 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; } @@ -59,6 +60,10 @@ public int getReloadIntervalSec() { return reloadIntervalSec; } + public boolean isReloadEnabled() { + return reloadIntervalSec != Integer.MAX_VALUE; + } + public int getRecentConfigEventsNum() { return recentConfigEventsNum; } @@ -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 sourceOrder = new LinkedList<>(); + private final Map sources = new HashMap<>(); + private final String host = null; private int reloadIntervalSec = DEFAULT_RELOAD_PERIOD_SEC; private int recentConfigEventsNum = DEFAULT_RECENT_EVENTS_NUM; - private Map listeners = new HashMap<>(); - private LinkedList sourceOrder = new LinkedList<>(); - private Map sources = new HashMap<>(); - private String host = null; + private final Map 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;