use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloAnnotationProcessor method processMethod.
@Override
protected void processMethod(final Object bean, String beanName, 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();
ConfigChangeListener configChangeListener = new ConfigChangeListener() {
@Override
public void onChange(ConfigChangeEvent changeEvent) {
ReflectionUtils.invokeMethod(method, bean, changeEvent);
}
};
for (String namespace : namespaces) {
Config config = ConfigService.getConfig(namespace);
config.addChangeListener(configChangeListener);
}
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class CachedCompositePropertySourceTest method testGetPropertyNames.
@Test
public void testGetPropertyNames() {
String[] propertyNames = Arrays.array("propertyName");
String[] anotherPropertyNames = Arrays.array("propertyName", "anotherPropertyName");
when(configPropertySource.getPropertyNames()).thenReturn(propertyNames, anotherPropertyNames);
String[] returnedPropertyNames = compositeSource.getPropertyNames();
assertArrayEquals(propertyNames, returnedPropertyNames);
assertSame(returnedPropertyNames, compositeSource.getPropertyNames());
listeners.get(0).onChange(new ConfigChangeEvent(null, null));
returnedPropertyNames = compositeSource.getPropertyNames();
assertArrayEquals(anotherPropertyNames, returnedPropertyNames);
assertSame(returnedPropertyNames, compositeSource.getPropertyNames());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloMockServerSpringIntegrationTest method shouldNotifyOnInterestedPatterns.
@Test
@DirtiesContext
public void shouldNotifyOnInterestedPatterns() throws Exception {
embeddedApollo.addOrModifyProperty(otherNamespace, "server.port", "8080");
embeddedApollo.addOrModifyProperty(otherNamespace, "server.path", "/apollo");
embeddedApollo.addOrModifyProperty(otherNamespace, "spring.application.name", "whatever");
ConfigChangeEvent changeEvent = testInterestedKeyPrefixesBean.futureData.get(5000, TimeUnit.MILLISECONDS);
assertEquals(otherNamespace, changeEvent.getNamespace());
assertEquals("8080", changeEvent.getChange("server.port").getNewValue());
assertEquals("/apollo", changeEvent.getChange("server.path").getNewValue());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent in project apollo by ctripcorp.
the class ApolloMockServerSpringIntegrationTest method testListenerTriggeredByDel.
@Test
@DirtiesContext
public void testListenerTriggeredByDel() throws InterruptedException, ExecutionException, TimeoutException {
embeddedApollo.deleteProperty(otherNamespace, "key1");
ConfigChangeEvent changeEvent = testBean.futureData.get(5000, TimeUnit.MILLISECONDS);
assertEquals(otherNamespace, changeEvent.getNamespace());
assertEquals(PropertyChangeType.DELETED, changeEvent.getChange("key1").getChangeType());
}
use of com.ctrip.framework.apollo.model.ConfigChangeEvent 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());
}
Aggregations