Search in sources :

Example 26 with ConfigChangeEvent

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

Example 27 with ConfigChangeEvent

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));
}
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)

Example 28 with ConfigChangeEvent

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());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ApolloConfigChangeListener(com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener) 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) InvocationOnMock(org.mockito.invocation.InvocationOnMock) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Aggregations

ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)28 Test (org.junit.Test)25 ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)23 Config (com.ctrip.framework.apollo.Config)16 ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)9 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)5 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)4 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)4 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)4 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)4 HashMap (java.util.HashMap)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3 Properties (java.util.Properties)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 InvocationOnMock (org.mockito.invocation.InvocationOnMock)3 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)3 DirtiesContext (org.springframework.test.annotation.DirtiesContext)3 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)2