This is an automated email from the ASF dual-hosted git repository.
liubao pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/servicecomb-java-chassis.git
The following commit(s) were added to refs/heads/master by this push:
new 7f18ee589 [SCB-2813]DynamicProperties: when key removed should get
default value (#3978)
7f18ee589 is described below
commit 7f18ee589b1e972b6f88fc6c02e56fb6cacaf5ea
Author: liubao68 <[email protected]>
AuthorDate: Thu Oct 19 15:17:15 2023 +0800
[SCB-2813]DynamicProperties: when key removed should get default value
(#3978)
---
.../ConfigCenterConfigurationChangedEvent.java | 23 +++---
.../config/center/client/ConfigCenterManager.java | 10 ++-
.../common/TestConfigurationChangedEvent.java | 47 -----------
.../config/kie/client/KieConfigManager.java | 11 +--
.../kie/client/KieConfigurationChangedEvent.java} | 49 +++++------
.../cc/ConfigCenterDynamicPropertiesSource.java | 8 +-
.../config/kie/KieDynamicPropertiesSource.java | 12 ++-
.../config/ConfigurationChangedEvent.java | 5 ++
.../servicecomb/config/DynamicPropertiesImpl.java | 94 +++++++++-------------
.../servicecomb/config/DynamicPropertiesTest.java | 27 ++++---
10 files changed, 125 insertions(+), 161 deletions(-)
diff --git
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java
similarity index 72%
copy from
foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
copy to
clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java
index 9e58a5f97..448c51a3d 100644
---
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
+++
b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterConfigurationChangedEvent.java
@@ -15,7 +15,7 @@
* limitations under the License.
*/
-package org.apache.servicecomb.config;
+package org.apache.servicecomb.config.center.client;
import java.util.HashMap;
import java.util.HashSet;
@@ -23,7 +23,10 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
-public class ConfigurationChangedEvent {
+/**
+ * This event is fired when configuration changed of config center.
+ */
+public class ConfigCenterConfigurationChangedEvent {
private final Map<String, Object> added;
private final Map<String, Object> deleted;
@@ -32,7 +35,7 @@ public class ConfigurationChangedEvent {
private Set<String> changed;
- private ConfigurationChangedEvent(Map<String, Object> added, Map<String,
Object> updated,
+ private ConfigCenterConfigurationChangedEvent(Map<String, Object> added,
Map<String, Object> updated,
Map<String, Object> deleted) {
this.added = added;
this.deleted = deleted;
@@ -43,7 +46,8 @@ public class ConfigurationChangedEvent {
this.changed.addAll(deleted.keySet());
}
- public static ConfigurationChangedEvent createIncremental(Map<String,
Object> latest, Map<String, Object> last) {
+ public static ConfigCenterConfigurationChangedEvent
createIncremental(Map<String, Object> latest,
+ Map<String, Object> last) {
Map<String, Object> itemsCreated = new HashMap<>();
Map<String, Object> itemsDeleted = new HashMap<>();
Map<String, Object> itemsModified = new HashMap<>();
@@ -61,18 +65,19 @@ public class ConfigurationChangedEvent {
itemsDeleted.put(itemKey, null);
}
}
- ConfigurationChangedEvent event = ConfigurationChangedEvent
+ ConfigCenterConfigurationChangedEvent event =
ConfigCenterConfigurationChangedEvent
.createIncremental(itemsCreated, itemsModified, itemsDeleted);
return event;
}
- public static ConfigurationChangedEvent createIncremental(Map<String,
Object> added, Map<String, Object> updated,
+ public static ConfigCenterConfigurationChangedEvent
createIncremental(Map<String, Object> added,
+ Map<String, Object> updated,
Map<String, Object> deleted) {
- return new ConfigurationChangedEvent(added, updated, deleted);
+ return new ConfigCenterConfigurationChangedEvent(added, updated, deleted);
}
- public static ConfigurationChangedEvent createIncremental(Map<String,
Object> updated) {
- return new ConfigurationChangedEvent(new HashMap<>(), updated, new
HashMap<>());
+ public static ConfigCenterConfigurationChangedEvent
createIncremental(Map<String, Object> updated) {
+ return new ConfigCenterConfigurationChangedEvent(new HashMap<>(), updated,
new HashMap<>());
}
public final Map<String, Object> getAdded() {
diff --git
a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
index 9a89b92a6..bdccc3543 100644
---
a/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
+++
b/clients/config-center-client/src/main/java/org/apache/servicecomb/config/center/client/ConfigCenterManager.java
@@ -23,7 +23,6 @@ import
org.apache.servicecomb.config.center.client.model.ConfigCenterConfigurati
import
org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest;
import
org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse;
import org.apache.servicecomb.config.common.ConfigConverter;
-import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
import org.apache.servicecomb.http.client.task.AbstractTask;
import org.apache.servicecomb.http.client.task.Task;
import org.slf4j.Logger;
@@ -76,11 +75,14 @@ public class ConfigCenterManager extends AbstractTask {
if (response.isChanged()) {
queryConfigurationsRequest.setRevision(response.getRevision());
Map<String, Object> lastData =
configConverter.updateData(response.getConfigurations());
- ConfigurationChangedEvent event = ConfigurationChangedEvent
+ ConfigCenterConfigurationChangedEvent event =
ConfigCenterConfigurationChangedEvent
.createIncremental(configConverter.getCurrentData(), lastData);
- eventBus.post(event);
+ if (!event.getChanged().isEmpty()) {
+ eventBus.post(event);
+ }
}
- startTask(new
BackOffSleepTask(configCenterConfiguration.getRefreshIntervalInMillis(), new
PollConfigurationTask(0)));
+ startTask(
+ new
BackOffSleepTask(configCenterConfiguration.getRefreshIntervalInMillis(), new
PollConfigurationTask(0)));
} catch (Exception e) {
LOGGER.error("get configurations from ConfigCenter failed, and will
try again.", e);
startTask(new BackOffSleepTask(failCount + 1, new
PollConfigurationTask(failCount + 1)));
diff --git
a/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
b/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
deleted file mode 100644
index 85c84e020..000000000
---
a/clients/config-common/src/test/java/org/apache/servicecomb/config/common/TestConfigurationChangedEvent.java
+++ /dev/null
@@ -1,47 +0,0 @@
-/*
- * Licensed to the Apache Software Foundation (ASF) under one or more
- * contributor license agreements. See the NOTICE file distributed with
- * this work for additional information regarding copyright ownership.
- * The ASF licenses this file to You under the Apache License, Version 2.0
- * (the "License"); you may not use this file except in compliance with
- * the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-
-package org.apache.servicecomb.config.common;
-
-import org.junit.jupiter.api.Assertions;
-import org.junit.jupiter.api.Test;
-
-import java.util.HashMap;
-import java.util.Map;
-
-public class TestConfigurationChangedEvent {
- @Test
- public void testConfigurationChangedEvent() {
- Map<String, Object> before = new HashMap<>();
- Map<String, Object> after = new HashMap<>();
- before.put("updated", "1");
- before.put("deleted", "1");
- before.put("notChanged", null);
-
- after.put("added", 1);
- after.put("updated", 2);
- after.put("addedNull", null);
- after.put("notChanged", null);
-
- ConfigurationChangedEvent event =
ConfigurationChangedEvent.createIncremental(after, before);
- Assertions.assertEquals(2, event.getAdded().size());
- Assertions.assertEquals(1, event.getDeleted().size());
- Assertions.assertEquals(1, event.getUpdated().size());
- Assertions.assertEquals(4, event.getComplete().size());
- Assertions.assertTrue(event.isChanged());
- }
-}
diff --git
a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
index 7c1eaf923..eaa7b93d6 100644
---
a/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
+++
b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigManager.java
@@ -23,7 +23,6 @@ import java.util.Map;
import java.util.concurrent.Executors;
import org.apache.servicecomb.config.common.ConfigConverter;
-import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
import org.apache.servicecomb.config.kie.client.model.ConfigurationsRequest;
import
org.apache.servicecomb.config.kie.client.model.ConfigurationsRequestFactory;
import org.apache.servicecomb.config.kie.client.model.ConfigurationsResponse;
@@ -90,9 +89,9 @@ public class KieConfigManager extends AbstractTask {
this.configurationsRequests.forEach(r ->
latestData.putAll(r.getLastRawData()));
Map<String, Object> lastData = configConverter.updateData(latestData);
- ConfigurationChangedEvent event = ConfigurationChangedEvent
+ KieConfigurationChangedEvent event = KieConfigurationChangedEvent
.createIncremental(configConverter.getCurrentData(), lastData);
- if (event.isChanged()) {
+ if (!event.getChanged().isEmpty()) {
eventBus.post(event);
}
}
@@ -128,9 +127,11 @@ public class KieConfigManager extends AbstractTask {
onDataChanged();
}
if (KieConfigManager.this.kieConfiguration.isEnableLongPolling()) {
- startTask(new BackOffSleepTask(LONG_POLLING_INTERVAL, new
PollConfigurationTask(0, this.configurationsRequest)));
+ startTask(
+ new BackOffSleepTask(LONG_POLLING_INTERVAL, new
PollConfigurationTask(0, this.configurationsRequest)));
} else {
- startTask(new
BackOffSleepTask(kieConfiguration.getRefreshIntervalInMillis(), new
PollConfigurationTask(0, this.configurationsRequest)));
+ startTask(new
BackOffSleepTask(kieConfiguration.getRefreshIntervalInMillis(),
+ new PollConfigurationTask(0, this.configurationsRequest)));
}
} catch (Exception e) {
LOGGER.error("get configurations from KieConfigCenter failed, and will
try again.", e);
diff --git
a/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java
b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java
similarity index 63%
rename from
clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java
rename to
clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java
index e24d061a6..0e22c17e8 100644
---
a/clients/config-common/src/main/java/org/apache/servicecomb/config/common/ConfigurationChangedEvent.java
+++
b/clients/config-kie-client/src/main/java/org/apache/servicecomb/config/kie/client/KieConfigurationChangedEvent.java
@@ -15,62 +15,67 @@
* limitations under the License.
*/
-package org.apache.servicecomb.config.common;
+package org.apache.servicecomb.config.kie.client;
import java.util.HashMap;
+import java.util.HashSet;
import java.util.Map;
import java.util.Objects;
+import java.util.Set;
-public class ConfigurationChangedEvent {
+/**
+ * This event is fired when configuration changed of kie.
+ */
+public class KieConfigurationChangedEvent {
private final Map<String, Object> added;
private final Map<String, Object> deleted;
private final Map<String, Object> updated;
- private final boolean changed;
-
- private Map<String, Object> complete;
+ private Set<String> changed;
- private ConfigurationChangedEvent(Map<String, Object> added, Map<String,
Object> updated,
- Map<String, Object> deleted, boolean changed) {
+ private KieConfigurationChangedEvent(Map<String, Object> added, Map<String,
Object> updated,
+ Map<String, Object> deleted) {
this.added = added;
this.deleted = deleted;
this.updated = updated;
- this.changed = changed;
+ this.changed = new HashSet<>();
+ this.changed.addAll(added.keySet());
+ this.changed.addAll(updated.keySet());
+ this.changed.addAll(deleted.keySet());
}
- public static ConfigurationChangedEvent createIncremental(Map<String,
Object> latest, Map<String, Object> last) {
+ public static KieConfigurationChangedEvent createIncremental(Map<String,
Object> latest, Map<String, Object> last) {
Map<String, Object> itemsCreated = new HashMap<>();
Map<String, Object> itemsDeleted = new HashMap<>();
Map<String, Object> itemsModified = new HashMap<>();
- boolean changed = false;
for (Map.Entry<String, Object> entry : latest.entrySet()) {
String itemKey = entry.getKey();
if (!last.containsKey(itemKey)) {
itemsCreated.put(itemKey, entry.getValue());
- changed = true;
} else if (!Objects.equals(last.get(itemKey), latest.get(itemKey))) {
itemsModified.put(itemKey, entry.getValue());
- changed = true;
}
}
for (String itemKey : last.keySet()) {
if (!latest.containsKey(itemKey)) {
itemsDeleted.put(itemKey, null);
- changed = true;
}
}
- ConfigurationChangedEvent event = ConfigurationChangedEvent
- .createIncremental(itemsCreated, itemsModified, itemsDeleted, changed);
- event.complete = latest;
+ KieConfigurationChangedEvent event = KieConfigurationChangedEvent
+ .createIncremental(itemsCreated, itemsModified, itemsDeleted);
return event;
}
- private static ConfigurationChangedEvent createIncremental(Map<String,
Object> added, Map<String, Object> updated,
- Map<String, Object> deleted, boolean changed) {
- return new ConfigurationChangedEvent(added, updated, deleted, changed);
+ public static KieConfigurationChangedEvent createIncremental(Map<String,
Object> added, Map<String, Object> updated,
+ Map<String, Object> deleted) {
+ return new KieConfigurationChangedEvent(added, updated, deleted);
+ }
+
+ public static KieConfigurationChangedEvent createIncremental(Map<String,
Object> updated) {
+ return new KieConfigurationChangedEvent(new HashMap<>(), updated, new
HashMap<>());
}
public final Map<String, Object> getAdded() {
@@ -87,11 +92,7 @@ public class ConfigurationChangedEvent {
return deleted;
}
- public final Map<String, Object> getComplete() {
- return complete;
- }
-
- public final boolean isChanged() {
+ public final Set<String> getChanged() {
return changed;
}
}
diff --git
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java
index 52f116727..614c44d39 100644
---
a/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java
+++
b/dynamic-config/config-cc/src/main/java/org/apache/servicecomb/config/cc/ConfigCenterDynamicPropertiesSource.java
@@ -28,15 +28,16 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.servicecomb.config.BootStrapProperties;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
import org.apache.servicecomb.config.DynamicPropertiesSource;
import org.apache.servicecomb.config.center.client.ConfigCenterAddressManager;
import org.apache.servicecomb.config.center.client.ConfigCenterClient;
+import
org.apache.servicecomb.config.center.client.ConfigCenterConfigurationChangedEvent;
import org.apache.servicecomb.config.center.client.ConfigCenterManager;
import
org.apache.servicecomb.config.center.client.model.ConfigCenterConfiguration;
import
org.apache.servicecomb.config.center.client.model.QueryConfigurationsRequest;
import
org.apache.servicecomb.config.center.client.model.QueryConfigurationsResponse;
import org.apache.servicecomb.config.common.ConfigConverter;
-import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
import org.apache.servicecomb.foundation.auth.AuthHeaderProvider;
import org.apache.servicecomb.foundation.common.concurrent.ConcurrentHashMapEx;
import org.apache.servicecomb.foundation.common.event.EventManager;
@@ -108,10 +109,13 @@ public class ConfigCenterDynamicPropertiesSource
implements DynamicPropertiesSou
}
@Subscribe
- public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
+ public void
onConfigurationChangedEvent(ConfigCenterConfigurationChangedEvent event) {
+ LOGGER.info("Dynamic configuration changed: {}", event.getChanged());
data.putAll(event.getAdded());
data.putAll(event.getUpdated());
event.getDeleted().forEach((k, v) -> data.remove(k));
+
EventManager.post(ConfigurationChangedEvent.createIncremental(event.getAdded(),
+ event.getUpdated(), event.getDeleted()));
}
private QueryConfigurationsRequest
createQueryConfigurationsRequest(Environment environment) {
diff --git
a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java
b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java
index b06ae1130..13a25be6f 100644
---
a/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java
+++
b/dynamic-config/config-kie/src/main/java/org/apache/servicecomb/config/kie/KieDynamicPropertiesSource.java
@@ -29,11 +29,12 @@ import org.apache.http.client.config.RequestConfig;
import org.apache.http.impl.client.BasicCredentialsProvider;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.servicecomb.config.BootStrapProperties;
+import org.apache.servicecomb.config.ConfigurationChangedEvent;
import org.apache.servicecomb.config.DynamicPropertiesSource;
import org.apache.servicecomb.config.common.ConfigConverter;
-import org.apache.servicecomb.config.common.ConfigurationChangedEvent;
import org.apache.servicecomb.config.kie.client.KieClient;
import org.apache.servicecomb.config.kie.client.KieConfigManager;
+import org.apache.servicecomb.config.kie.client.KieConfigurationChangedEvent;
import org.apache.servicecomb.config.kie.client.model.KieAddressManager;
import org.apache.servicecomb.config.kie.client.model.KieConfiguration;
import org.apache.servicecomb.foundation.auth.AuthHeaderProvider;
@@ -43,12 +44,16 @@ import
org.apache.servicecomb.foundation.common.utils.SPIServiceUtils;
import org.apache.servicecomb.http.client.auth.RequestAuthHeaderProvider;
import org.apache.servicecomb.http.client.common.HttpTransport;
import org.apache.servicecomb.http.client.common.HttpTransportFactory;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.core.env.MapPropertySource;
import com.google.common.eventbus.Subscribe;
public class KieDynamicPropertiesSource implements DynamicPropertiesSource {
+ private static final Logger LOGGER =
LoggerFactory.getLogger(KieDynamicPropertiesSource.class);
+
public static final String SOURCE_NAME = "kie";
private final Map<String, Object> data = new ConcurrentHashMapEx<>();
@@ -84,10 +89,13 @@ public class KieDynamicPropertiesSource implements
DynamicPropertiesSource {
}
@Subscribe
- public void onConfigurationChangedEvent(ConfigurationChangedEvent event) {
+ public void onConfigurationChangedEvent(KieConfigurationChangedEvent event) {
+ LOGGER.info("Dynamic configuration changed: {}", event.getChanged());
data.putAll(event.getAdded());
data.putAll(event.getUpdated());
event.getDeleted().forEach((k, v) -> data.remove(k));
+
EventManager.post(ConfigurationChangedEvent.createIncremental(event.getAdded(),
+ event.getUpdated(), event.getDeleted()));
}
private KieConfiguration createKieConfiguration(KieConfig kieConfig,
Environment environment) {
diff --git
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
index 9e58a5f97..2c0532a65 100644
---
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
+++
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/ConfigurationChangedEvent.java
@@ -23,6 +23,11 @@ import java.util.Map;
import java.util.Objects;
import java.util.Set;
+/**
+ * This event is fired when configuration changed. And the change is already
applied to Environment.
+ *
+ * Listeners can use Environment to get the latest value.
+ */
public class ConfigurationChangedEvent {
private final Map<String, Object> added;
diff --git
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java
index 63cf7679d..84f623d74 100644
---
a/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java
+++
b/foundations/foundation-config/src/main/java/org/apache/servicecomb/config/DynamicPropertiesImpl.java
@@ -33,17 +33,28 @@ import org.springframework.core.env.Environment;
import com.google.common.eventbus.Subscribe;
public class DynamicPropertiesImpl implements DynamicProperties {
- private final Map<String, Set<Consumer<String>>> stringCallbacks = new
HashMap<>();
+ private static class Holder<C, D> {
+ C callback;
- private final Map<String, Set<IntConsumer>> intCallbacks = new HashMap<>();
+ D defaultValue;
- private final Map<String, Set<LongConsumer>> longCallbacks = new HashMap<>();
+ Holder(C callback, D defaultValue) {
+ this.callback = callback;
+ this.defaultValue = defaultValue;
+ }
+ }
+
+ private final Map<String, Set<Holder<Consumer<String>, String>>>
stringCallbacks = new HashMap<>();
+
+ private final Map<String, Set<Holder<IntConsumer, Integer>>> intCallbacks =
new HashMap<>();
- private final Map<String, Set<DoubleConsumer>> floatCallbacks = new
HashMap<>();
+ private final Map<String, Set<Holder<LongConsumer, Long>>> longCallbacks =
new HashMap<>();
- private final Map<String, Set<DoubleConsumer>> doubleCallbacks = new
HashMap<>();
+ private final Map<String, Set<Holder<DoubleConsumer, Float>>> floatCallbacks
= new HashMap<>();
- private final Map<String, Set<Consumer<Boolean>>> booleanCallbacks = new
HashMap<>();
+ private final Map<String, Set<Holder<DoubleConsumer, Double>>>
doubleCallbacks = new HashMap<>();
+
+ private final Map<String, Set<Holder<Consumer<Boolean>, Boolean>>>
booleanCallbacks = new HashMap<>();
private final Environment environment;
@@ -63,79 +74,46 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
}
for (Entry<String, Object> entry : event.getDeleted().entrySet()) {
- updateDefault(entry);
- }
- }
-
- private void updateDefault(Entry<String, Object> entry) {
- if (stringCallbacks.containsKey(entry.getKey())) {
- for (Consumer<String> callbacks : stringCallbacks.get(entry.getKey())) {
- callbacks.accept(null);
- }
- }
- if (intCallbacks.containsKey(entry.getKey())) {
- for (IntConsumer callbacks : intCallbacks.get(entry.getKey())) {
- callbacks.accept(0);
- }
- }
- if (longCallbacks.containsKey(entry.getKey())) {
- for (LongConsumer callbacks : longCallbacks.get(entry.getKey())) {
- callbacks.accept(0L);
- }
- }
- if (floatCallbacks.containsKey(entry.getKey())) {
- for (DoubleConsumer callbacks : floatCallbacks.get(entry.getKey())) {
- callbacks.accept(0F);
- }
- }
- if (doubleCallbacks.containsKey(entry.getKey())) {
- for (DoubleConsumer callbacks : doubleCallbacks.get(entry.getKey())) {
- callbacks.accept(0D);
- }
- }
- if (booleanCallbacks.containsKey(entry.getKey())) {
- for (Consumer<Boolean> callbacks : booleanCallbacks.get(entry.getKey()))
{
- callbacks.accept(false);
- }
+ updateValue(entry);
}
}
private void updateValue(Entry<String, Object> entry) {
if (stringCallbacks.containsKey(entry.getKey())) {
- for (Consumer<String> callbacks : stringCallbacks.get(entry.getKey())) {
- callbacks.accept((String) entry.getValue());
+ for (Holder<Consumer<String>, String> callbacks :
stringCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
callbacks.defaultValue));
}
}
if (intCallbacks.containsKey(entry.getKey())) {
- for (IntConsumer callbacks : intCallbacks.get(entry.getKey())) {
- callbacks.accept((int) entry.getValue());
+ for (Holder<IntConsumer, Integer> callbacks :
intCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
Integer.class, callbacks.defaultValue));
}
}
if (longCallbacks.containsKey(entry.getKey())) {
- for (LongConsumer callbacks : longCallbacks.get(entry.getKey())) {
- callbacks.accept((long) entry.getValue());
+ for (Holder<LongConsumer, Long> callbacks :
longCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
Long.class, callbacks.defaultValue));
}
}
if (floatCallbacks.containsKey(entry.getKey())) {
- for (DoubleConsumer callbacks : floatCallbacks.get(entry.getKey())) {
- callbacks.accept((float) entry.getValue());
+ for (Holder<DoubleConsumer, Float> callbacks :
floatCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
Float.class, callbacks.defaultValue));
}
}
if (doubleCallbacks.containsKey(entry.getKey())) {
- for (DoubleConsumer callbacks : doubleCallbacks.get(entry.getKey())) {
- callbacks.accept((double) entry.getValue());
+ for (Holder<DoubleConsumer, Double> callbacks :
doubleCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
Double.class, callbacks.defaultValue));
}
}
if (booleanCallbacks.containsKey(entry.getKey())) {
- for (Consumer<Boolean> callbacks : booleanCallbacks.get(entry.getKey()))
{
- callbacks.accept((Boolean) entry.getValue());
+ for (Holder<Consumer<Boolean>, Boolean> callbacks :
booleanCallbacks.get(entry.getKey())) {
+ callbacks.callback.accept(environment.getProperty(entry.getKey(),
Boolean.class, callbacks.defaultValue));
}
}
}
@Override
public String getStringProperty(String propertyName, Consumer<String>
consumer, String defaultValue) {
- stringCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ stringCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(new Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, defaultValue);
}
@@ -146,7 +124,7 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
@Override
public int getIntProperty(String propertyName, IntConsumer consumer, int
defaultValue) {
- intCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ intCallbacks.computeIfAbsent(propertyName, key -> new HashSet<>()).add(new
Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, int.class, defaultValue);
}
@@ -157,7 +135,7 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
@Override
public long getLongProperty(String propertyName, LongConsumer consumer, long
defaultValue) {
- longCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ longCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(new Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, long.class, defaultValue);
}
@@ -168,7 +146,7 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
@Override
public float getFloatProperty(String propertyName, DoubleConsumer consumer,
float defaultValue) {
- floatCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ floatCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(new Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, float.class, defaultValue);
}
@@ -179,7 +157,7 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
@Override
public double getDoubleProperty(String propertyName, DoubleConsumer
consumer, double defaultValue) {
- doubleCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ doubleCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(new Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, double.class, defaultValue);
}
@@ -190,7 +168,7 @@ public class DynamicPropertiesImpl implements
DynamicProperties {
@Override
public boolean getBooleanProperty(String propertyName, Consumer<Boolean>
consumer, boolean defaultValue) {
- booleanCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(consumer);
+ booleanCallbacks.computeIfAbsent(propertyName, key -> new
HashSet<>()).add(new Holder<>(consumer, defaultValue));
return environment.getProperty(propertyName, boolean.class, defaultValue);
}
diff --git
a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java
b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java
index ecf7bf779..3f72af9b2 100644
---
a/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java
+++
b/foundations/foundation-config/src/test/java/org/apache/servicecomb/config/DynamicPropertiesTest.java
@@ -114,7 +114,7 @@ public class DynamicPropertiesTest {
String newValue = uniquify("newValue");
- Mockito.when(environment.getProperty(stringPropertyName,
stringOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(stringPropertyName, (String)
null)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(stringPropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));
@@ -132,7 +132,7 @@ public class DynamicPropertiesTest {
int newValue = Randomness.nextInt();
- Mockito.when(environment.getProperty(intPropertyName, int.class,
intOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(intPropertyName, Integer.class,
0)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(intPropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));
@@ -142,20 +142,27 @@ public class DynamicPropertiesTest {
@Test
public void observesSpecifiedLongProperty() throws Exception {
+ Mockito.when(environment.getProperty(longPropertyName, long.class,
3L)).thenReturn(longOldValue);
+
long property = dynamicProperties.getLongProperty(longPropertyName, 0);
assertThat(property, is(longOldValue));
- property = dynamicProperties.getLongProperty(longPropertyName, value ->
longPropertyValue = value, 0);
+ property = dynamicProperties.getLongProperty(longPropertyName, value ->
longPropertyValue = value, 3L);
assertThat(property, is(longOldValue));
long newValue = Randomness.nextLong();
- Mockito.when(environment.getProperty(longPropertyName, long.class,
longOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(longPropertyName, Long.class,
3L)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(longPropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));
- poller.assertEventually(() -> longPropertyValue == newValue);
+ Mockito.when(environment.getProperty(longPropertyName, Long.class,
3L)).thenReturn(3L);
+ HashMap<String, Object> deleted = new HashMap<>();
+ deleted.put(longPropertyName, newValue);
+ EventManager.post(ConfigurationChangedEvent.createIncremental(new
HashMap<>(), new HashMap<>(), deleted));
+
+ poller.assertEventually(() -> longPropertyValue == 3L);
}
@Test
@@ -163,12 +170,12 @@ public class DynamicPropertiesTest {
double property = dynamicProperties.getFloatProperty(floatPropertyName, 0);
assertThat(property, closeTo(floatOldValue, ERROR));
- property = dynamicProperties.getFloatProperty(floatPropertyName, value ->
floatPropertyValue = value, 0);
+ property = dynamicProperties.getFloatProperty(floatPropertyName, value ->
floatPropertyValue = value, 0f);
assertThat(property, closeTo(floatOldValue, ERROR));
float newValue = Double.valueOf(Randomness.nextDouble()).floatValue();
- Mockito.when(environment.getProperty(floatPropertyName, float.class,
floatOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(floatPropertyName, Float.class,
0f)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(floatPropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));
@@ -181,12 +188,12 @@ public class DynamicPropertiesTest {
double property = dynamicProperties.getDoubleProperty(doublePropertyName,
0);
assertThat(property, closeTo(doubleOldValue, ERROR));
- property = dynamicProperties.getDoubleProperty(doublePropertyName, value
-> doublePropertyValue = value, 0);
+ property = dynamicProperties.getDoubleProperty(doublePropertyName, value
-> doublePropertyValue = value, 0d);
assertThat(property, closeTo(doubleOldValue, ERROR));
double newValue = Randomness.nextDouble();
- Mockito.when(environment.getProperty(doublePropertyName, double.class,
doubleOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(doublePropertyName, Double.class,
0d)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(doublePropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));
@@ -207,7 +214,7 @@ public class DynamicPropertiesTest {
boolean newValue = !booleanOldValue;
- Mockito.when(environment.getProperty(booleanPropertyName, boolean.class,
booleanOldValue)).thenReturn(newValue);
+ Mockito.when(environment.getProperty(booleanPropertyName, Boolean.class,
booleanOldValue)).thenReturn(newValue);
HashMap<String, Object> updated = new HashMap<>();
updated.put(booleanPropertyName, newValue);
EventManager.post(ConfigurationChangedEvent.createIncremental(updated));