Search in sources :

Example 11 with ConfigChange

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();
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ImmutableMap(com.google.common.collect.ImmutableMap)

Example 12 with ConfigChange

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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 13 with ConfigChange

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;
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange)

Example 14 with ConfigChange

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);
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) Properties(java.util.Properties)

Example 15 with ConfigChange

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));
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) EnableApolloConfig(com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig) ApolloConfig(com.ctrip.framework.apollo.spring.annotation.ApolloConfig) SimpleConfig(com.ctrip.framework.apollo.internals.SimpleConfig) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Matchers.anyString(org.mockito.Matchers.anyString) YamlConfigFile(com.ctrip.framework.apollo.internals.YamlConfigFile) Test(org.junit.Test)

Aggregations

ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)15 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)9 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)8 Test (org.junit.Test)8 HashMap (java.util.HashMap)4 Properties (java.util.Properties)4 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)3 Config (com.ctrip.framework.apollo.Config)2 ImmutableMap (com.google.common.collect.ImmutableMap)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)1 YamlConfigFile (com.ctrip.framework.apollo.internals.YamlConfigFile)1 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)1 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)1 OrderedProperties (com.ctrip.framework.apollo.util.OrderedProperties)1 File (java.io.File)1 Matchers.anyString (org.mockito.Matchers.anyString)1