use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.
the class DefaultConfig method updateAndCalcConfigChanges.
private Map<String, ConfigChange> updateAndCalcConfigChanges(Properties newConfigProperties) {
List<ConfigChange> configChanges = calcPropertyChanges(m_namespace, m_configProperties.get(), newConfigProperties);
ImmutableMap.Builder<String, ConfigChange> actualChanges = new ImmutableMap.Builder<>();
// 1. use getProperty to update configChanges's old value
for (ConfigChange change : configChanges) {
change.setOldValue(this.getProperty(change.getPropertyName(), change.getOldValue()));
}
// 2. update m_configProperties
m_configProperties.set(newConfigProperties);
clearConfigCache();
// 3. use getProperty to update configChange's new value and calc the final changes
for (ConfigChange change : configChanges) {
change.setNewValue(this.getProperty(change.getPropertyName(), change.getNewValue()));
switch(change.getChangeType()) {
case ADDED:
if (Objects.equals(change.getOldValue(), change.getNewValue())) {
break;
}
if (change.getOldValue() != null) {
change.setChangeType(PropertyChangeType.MODIFIED);
}
actualChanges.put(change.getPropertyName(), change);
break;
case MODIFIED:
if (!Objects.equals(change.getOldValue(), change.getNewValue())) {
actualChanges.put(change.getPropertyName(), change);
}
break;
case DELETED:
if (Objects.equals(change.getOldValue(), change.getNewValue())) {
break;
}
if (change.getNewValue() != null) {
change.setChangeType(PropertyChangeType.MODIFIED);
}
actualChanges.put(change.getPropertyName(), change);
break;
default:
// do nothing
break;
}
}
return actualChanges.build();
}
use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.
the class AbstractConfigTest method testFireConfigChange_changes_notify_once.
@Test
public void testFireConfigChange_changes_notify_once() throws ExecutionException, InterruptedException, TimeoutException {
AbstractConfig abstractConfig = new ErrorConfig();
final String namespace = "app-namespace-1";
final String key = "great-key";
final SettableFuture<ConfigChangeEvent> future1 = SettableFuture.create();
final SettableFuture<ConfigChangeEvent> future2 = SettableFuture.create();
final AtomicInteger invokeCount = new AtomicInteger();
final ConfigChangeListener configChangeListener1 = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
invokeCount.incrementAndGet();
future1.set(changeEvent);
}
});
final ConfigChangeListener configChangeListener2 = spy(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
invokeCount.incrementAndGet();
future2.set(changeEvent);
}
});
abstractConfig.addChangeListener(configChangeListener1, Collections.singleton(key));
abstractConfig.addChangeListener(configChangeListener2, Collections.singleton(key));
Map<String, ConfigChange> changes = new HashMap<>();
changes.put(key, new ConfigChange(namespace, key, "old-value", "new-value", PropertyChangeType.MODIFIED));
abstractConfig.fireConfigChange(namespace, changes);
assertEquals(Collections.singleton(key), future1.get(500, TimeUnit.MILLISECONDS).changedKeys());
assertEquals(Collections.singleton(key), future2.get(500, TimeUnit.MILLISECONDS).changedKeys());
assertEquals(2, invokeCount.get());
verify(configChangeListener1, times(1)).onChange(Matchers.<ConfigChangeEvent>any());
verify(configChangeListener2, times(1)).onChange(Matchers.<ConfigChangeEvent>any());
}
use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.
the class AbstractConfig method calcPropertyChanges.
List<ConfigChange> calcPropertyChanges(String namespace, Properties previous, Properties current) {
if (previous == null) {
previous = propertiesFactory.getPropertiesInstance();
}
if (current == null) {
current = propertiesFactory.getPropertiesInstance();
}
Set<String> previousKeys = previous.stringPropertyNames();
Set<String> currentKeys = current.stringPropertyNames();
Set<String> commonKeys = Sets.intersection(previousKeys, currentKeys);
Set<String> newKeys = Sets.difference(currentKeys, commonKeys);
Set<String> removedKeys = Sets.difference(previousKeys, commonKeys);
List<ConfigChange> changes = Lists.newArrayList();
for (String newKey : newKeys) {
changes.add(new ConfigChange(namespace, newKey, null, current.getProperty(newKey), PropertyChangeType.ADDED));
}
for (String removedKey : removedKeys) {
changes.add(new ConfigChange(namespace, removedKey, previous.getProperty(removedKey), null, PropertyChangeType.DELETED));
}
for (String commonKey : commonKeys) {
String previousValue = previous.getProperty(commonKey);
String currentValue = current.getProperty(commonKey);
if (Objects.equal(previousValue, currentValue)) {
continue;
}
changes.add(new ConfigChange(namespace, commonKey, previousValue, currentValue, PropertyChangeType.MODIFIED));
}
return changes;
}
use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.
the class DefaultConfig method onRepositoryChange.
@Override
public synchronized void onRepositoryChange(String namespace, Properties newProperties) {
if (newProperties.equals(m_configProperties.get())) {
return;
}
ConfigSourceType sourceType = m_configRepository.getSourceType();
Properties newConfigProperties = propertiesFactory.getPropertiesInstance();
newConfigProperties.putAll(newProperties);
Map<String, ConfigChange> actualChanges = updateAndCalcConfigChanges(newConfigProperties, sourceType);
// check double checked result
if (actualChanges.isEmpty()) {
return;
}
this.fireConfigChange(m_namespace, actualChanges);
Tracer.logEvent("Apollo.Client.ConfigChanges", m_namespace);
}
use of com.ctrip.framework.apollo.model.ConfigChange in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithYamlFile.
@Test
public void testApolloConfigChangeListenerWithYamlFile() throws Exception {
String someKey = "someKey";
String someValue = "someValue";
String anotherValue = "anotherValue";
YamlConfigFile configFile = prepareYamlConfigFile(APPLICATION_YAML_NAMESPACE, readYamlContentAsConfigFileProperties("case9.yml"));
TestApolloConfigChangeListenerWithYamlFile bean = getBean(TestApolloConfigChangeListenerWithYamlFile.class, AppConfig9.class);
Config yamlConfig = bean.getYamlConfig();
SettableFuture<ConfigChangeEvent> future = bean.getConfigChangeEventFuture();
assertEquals(someValue, yamlConfig.getProperty(someKey, null));
assertFalse(future.isDone());
configFile.onRepositoryChange(APPLICATION_YAML_NAMESPACE, readYamlContentAsConfigFileProperties("case9-new.yml"));
ConfigChangeEvent configChangeEvent = future.get(100, TimeUnit.MILLISECONDS);
ConfigChange change = configChangeEvent.getChange(someKey);
assertEquals(someValue, change.getOldValue());
assertEquals(anotherValue, change.getNewValue());
assertEquals(anotherValue, yamlConfig.getProperty(someKey, null));
}
Aggregations