use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class CachedCompositePropertySourceTest method setUp.
@Before
public void setUp() throws Exception {
compositeSource = new CachedCompositePropertySource("testCompositeSource");
listeners = new LinkedList<>();
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
ConfigChangeListener listener = invocation.getArgument(0, ConfigChangeListener.class);
listeners.add(listener);
return Void.class;
}
}).when(configPropertySource).addChangeListener(any(ConfigChangeListener.class));
compositeSource.addPropertySource(configPropertySource);
}
use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.
the class CachedCompositePropertySourceTest method testAddFirstPropertySource.
@Test
public void testAddFirstPropertySource() {
ConfigPropertySource anotherSource = mock(ConfigPropertySource.class);
final List<ConfigChangeListener> anotherListenerList = new LinkedList<>();
Mockito.doAnswer(new Answer() {
@Override
public Object answer(InvocationOnMock invocation) throws Throwable {
ConfigChangeListener listener = invocation.getArgument(0, ConfigChangeListener.class);
anotherListenerList.add(listener);
return Void.class;
}
}).when(anotherSource).addChangeListener(any(ConfigChangeListener.class));
compositeSource.addFirstPropertySource(anotherSource);
Collection<PropertySource<?>> propertySources = compositeSource.getPropertySources();
Iterator<PropertySource<?>> it = propertySources.iterator();
assertEquals(2, propertySources.size());
assertEquals(1, anotherListenerList.size());
assertSame(anotherSource, it.next());
assertSame(configPropertySource, it.next());
}
use of com.ctrip.framework.apollo.ConfigChangeListener 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.ConfigChangeListener 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.ConfigChangeListener in project apollo by ctripcorp.
the class AbstractConfig method fireConfigChange.
/**
* @param changes map's key is config property's key
*/
protected void fireConfigChange(String namespace, Map<String, ConfigChange> changes) {
final Set<String> changedKeys = changes.keySet();
final List<ConfigChangeListener> listeners = this.findMatchedConfigChangeListeners(changedKeys);
// notify those listeners
for (ConfigChangeListener listener : listeners) {
Set<String> interestedChangedKeys = resolveInterestedChangedKeys(listener, changedKeys);
InterestedConfigChangeEvent interestedConfigChangeEvent = new InterestedConfigChangeEvent(namespace, changes, interestedChangedKeys);
this.notifyAsync(listener, interestedConfigChangeEvent);
}
}
Aggregations