use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithInterestedKeyPrefixes_fire.
@Test
public void testApolloConfigChangeListenerWithInterestedKeyPrefixes_fire() throws InterruptedException {
// default mock, useless here
// just for speed up test without waiting
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, mock(Config.class));
SimpleConfig simpleConfig = spy(this.prepareConfig(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, new Properties()));
mockConfig(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, simpleConfig);
TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1 bean = getBean(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.class, AppConfig11.class);
verify(simpleConfig, atLeastOnce()).addChangeListener(any(ConfigChangeListener.class), Mockito.nullable(Set.class), anySetOf(String.class));
Properties properties = new Properties();
properties.put("logging.level.com", "debug");
properties.put("logging.level.root", "warn");
properties.put("number.value", "333");
// publish config change
simpleConfig.onRepositoryChange(TestApolloConfigChangeListenerWithInterestedKeyPrefixesBean1.SPECIAL_NAMESPACE, properties);
// get event from bean
ConfigChangeEvent configChangeEvent = bean.getConfigChangeEvent();
Set<String> interestedChangedKeys = configChangeEvent.interestedChangedKeys();
assertEquals(Sets.newHashSet("logging.level.com", "logging.level.root", "number.value"), interestedChangedKeys);
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class JavaConfigAnnotationTest method testApolloConfigChangeListenerWithInheritance.
@Test
public void testApolloConfigChangeListenerWithInheritance() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);
final List<ConfigChangeListener> applicationListeners = Lists.newArrayList();
final List<ConfigChangeListener> fxApolloListeners = Lists.newArrayList();
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(applicationConfig).addChangeListener(any(ConfigChangeListener.class));
doAnswer(new Answer<Object>() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(fxApolloConfig).addChangeListener(any(ConfigChangeListener.class));
ConfigChangeEvent someEvent = mock(ConfigChangeEvent.class);
ConfigChangeEvent anotherEvent = mock(ConfigChangeEvent.class);
TestApolloChildConfigChangeListener bean = getBean(TestApolloChildConfigChangeListener.class, AppConfig7.class);
// PropertySourcesProcessor add listeners to listen config changed of all namespace
assertEquals(5, applicationListeners.size());
assertEquals(1, fxApolloListeners.size());
for (ConfigChangeListener listener : applicationListeners) {
listener.onChange(someEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(someEvent, bean.getChangeEvent3());
assertEquals(someEvent, bean.getSomeChangeEvent());
for (ConfigChangeListener listener : fxApolloListeners) {
listener.onChange(anotherEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(anotherEvent, bean.getChangeEvent3());
assertEquals(someEvent, bean.getSomeChangeEvent());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class SimpleConfigTest method testOnRepositoryChange.
@Test
public void testOnRepositoryChange() throws Exception {
Properties someProperties = new Properties();
String someKey = "someKey";
String someValue = "someValue";
String anotherKey = "anotherKey";
String anotherValue = "anotherValue";
someProperties.putAll(ImmutableMap.of(someKey, someValue, anotherKey, anotherValue));
Properties anotherProperties = new Properties();
String newKey = "newKey";
String newValue = "newValue";
String someValueNew = "someValueNew";
anotherProperties.putAll(ImmutableMap.of(someKey, someValueNew, newKey, newValue));
someSourceType = ConfigSourceType.LOCAL;
when(configRepository.getConfig()).thenReturn(someProperties);
when(configRepository.getSourceType()).thenReturn(someSourceType);
final SettableFuture<ConfigChangeEvent> configChangeFuture = SettableFuture.create();
ConfigChangeListener someListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
configChangeFuture.set(changeEvent);
}
};
SimpleConfig config = new SimpleConfig(someNamespace, configRepository);
assertEquals(someSourceType, config.getSourceType());
config.addChangeListener(someListener);
ConfigSourceType anotherSourceType = ConfigSourceType.REMOTE;
when(configRepository.getSourceType()).thenReturn(anotherSourceType);
config.onRepositoryChange(someNamespace, anotherProperties);
ConfigChangeEvent changeEvent = configChangeFuture.get(500, TimeUnit.MILLISECONDS);
assertEquals(someNamespace, changeEvent.getNamespace());
assertEquals(3, changeEvent.changedKeys().size());
ConfigChange someKeyChange = changeEvent.getChange(someKey);
assertEquals(someValue, someKeyChange.getOldValue());
assertEquals(someValueNew, someKeyChange.getNewValue());
assertEquals(PropertyChangeType.MODIFIED, someKeyChange.getChangeType());
ConfigChange anotherKeyChange = changeEvent.getChange(anotherKey);
assertEquals(anotherValue, anotherKeyChange.getOldValue());
assertEquals(null, anotherKeyChange.getNewValue());
assertEquals(PropertyChangeType.DELETED, anotherKeyChange.getChangeType());
ConfigChange newKeyChange = changeEvent.getChange(newKey);
assertEquals(null, newKeyChange.getOldValue());
assertEquals(newValue, newKeyChange.getNewValue());
assertEquals(PropertyChangeType.ADDED, newKeyChange.getChangeType());
assertEquals(anotherSourceType, config.getSourceType());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class XMLConfigAnnotationTest method testApolloConfigChangeListener.
@Test
public void testApolloConfigChangeListener() throws Exception {
Config applicationConfig = mock(Config.class);
Config fxApolloConfig = mock(Config.class);
mockConfig(ConfigConsts.NAMESPACE_APPLICATION, applicationConfig);
mockConfig(FX_APOLLO_NAMESPACE, fxApolloConfig);
final List<ConfigChangeListener> applicationListeners = Lists.newArrayList();
final List<ConfigChangeListener> fxApolloListeners = Lists.newArrayList();
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
applicationListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(applicationConfig).addChangeListener(any(ConfigChangeListener.class));
doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
fxApolloListeners.add(invocation.getArgument(0, ConfigChangeListener.class));
return Void.class;
}
}).when(fxApolloConfig).addChangeListener(any(ConfigChangeListener.class));
ConfigChangeEvent someEvent = mock(ConfigChangeEvent.class);
ConfigChangeEvent anotherEvent = mock(ConfigChangeEvent.class);
TestApolloConfigChangeListenerBean1 bean = getBean("spring/XmlConfigAnnotationTest3.xml", TestApolloConfigChangeListenerBean1.class);
// PropertySourcesProcessor add listeners to listen config changed of all namespace
assertEquals(4, applicationListeners.size());
assertEquals(1, fxApolloListeners.size());
for (ConfigChangeListener listener : applicationListeners) {
listener.onChange(someEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(someEvent, bean.getChangeEvent3());
for (ConfigChangeListener listener : fxApolloListeners) {
listener.onChange(anotherEvent);
}
assertEquals(someEvent, bean.getChangeEvent1());
assertEquals(someEvent, bean.getChangeEvent2());
assertEquals(anotherEvent, bean.getChangeEvent3());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project SpringCloudDemo by RickJou.
the class ApolloJavaClientDemo method publicConfig.
public static void publicConfig() {
// 公共配置
String somePublicNamespace = "org1.public-namespace";
// config instance is singleton for each namespace and is never null
Config config = ConfigService.getConfig(somePublicNamespace);
String someKey = "common.setting.value";
String someDefaultValue = "defaultValue";
String value = config.getProperty(someKey, someDefaultValue);
log.info("公共配置" + someKey + ":" + value);
// 开启配置监听,接收实时推送的配置
new Thread(new Runnable() {
public void run() {
config.addChangeListener(new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
log.info(String.format("监听到namespace:%s有修改", changeEvent.getNamespace()));
for (String key : changeEvent.changedKeys()) {
ConfigChange change = changeEvent.getChange(key);
log.info(String.format("Found change - key: %s, oldValue: %s, newValue: %s, changeType: %s", change.getPropertyName(), change.getOldValue(), change.getNewValue(), change.getChangeType()));
}
}
});
}
}).start();
}
Aggregations