use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloAnnotationProcessor method processApolloConfigChangeListener.
private void processApolloConfigChangeListener(final Object bean, final Method method) {
ApolloConfigChangeListener annotation = AnnotationUtils.findAnnotation(method, ApolloConfigChangeListener.class);
if (annotation == null) {
return;
}
Class<?>[] parameterTypes = method.getParameterTypes();
Preconditions.checkArgument(parameterTypes.length == 1, "Invalid number of parameters: %s for method: %s, should be 1", parameterTypes.length, method);
Preconditions.checkArgument(ConfigChangeEvent.class.isAssignableFrom(parameterTypes[0]), "Invalid parameter type: %s for method: %s, should be ConfigChangeEvent", parameterTypes[0], method);
ReflectionUtils.makeAccessible(method);
String[] namespaces = annotation.value();
String[] annotatedInterestedKeys = annotation.interestedKeys();
String[] annotatedInterestedKeyPrefixes = annotation.interestedKeyPrefixes();
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
ReflectionUtils.invokeMethod(method, bean, changeEvent);
}
};
Set<String> interestedKeys = annotatedInterestedKeys.length > 0 ? Sets.newHashSet(annotatedInterestedKeys) : null;
Set<String> interestedKeyPrefixes = annotatedInterestedKeyPrefixes.length > 0 ? Sets.newHashSet(annotatedInterestedKeyPrefixes) : null;
for (String namespace : namespaces) {
final String resolvedNamespace = this.environment.resolveRequiredPlaceholders(namespace);
Config config = ConfigService.getConfig(resolvedNamespace);
if (interestedKeys == null && interestedKeyPrefixes == null) {
config.addChangeListener(configChangeListener);
} else {
config.addChangeListener(configChangeListener, interestedKeys, interestedKeyPrefixes);
}
}
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent 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));
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class JavaConfigAnnotationTest 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<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);
TestApolloConfigChangeListenerBean1 bean = getBean(TestApolloConfigChangeListenerBean1.class, AppConfig3.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());
}
Aggregations