Search in sources :

Example 16 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener 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());
}
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)

Example 17 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener 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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigSourceType(com.ctrip.framework.apollo.enums.ConfigSourceType) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Properties(java.util.Properties) Test(org.junit.Test)

Example 18 with ConfigChangeListener

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

Example 19 with ConfigChangeListener

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

Example 20 with ConfigChangeListener

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

Aggregations

ConfigChangeListener (com.ctrip.framework.apollo.ConfigChangeListener)27 ConfigChangeEvent (com.ctrip.framework.apollo.model.ConfigChangeEvent)22 Test (org.junit.Test)21 Config (com.ctrip.framework.apollo.Config)14 ConfigChange (com.ctrip.framework.apollo.model.ConfigChange)8 InvocationOnMock (org.mockito.invocation.InvocationOnMock)6 BaseIntegrationTest (com.ctrip.framework.apollo.BaseIntegrationTest)4 ApolloConfig (com.ctrip.framework.apollo.core.dto.ApolloConfig)4 HashMap (java.util.HashMap)4 ContextHandler (org.eclipse.jetty.server.handler.ContextHandler)4 Answer (org.mockito.stubbing.Answer)4 ApolloConfigNotification (com.ctrip.framework.apollo.core.dto.ApolloConfigNotification)3 ApolloConfig (com.ctrip.framework.apollo.spring.annotation.ApolloConfig)3 ApolloConfigChangeListener (com.ctrip.framework.apollo.spring.annotation.ApolloConfigChangeListener)3 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)3 ConfigSourceType (com.ctrip.framework.apollo.enums.ConfigSourceType)2 SimpleConfig (com.ctrip.framework.apollo.internals.SimpleConfig)2 EnableApolloConfig (com.ctrip.framework.apollo.spring.annotation.EnableApolloConfig)2 Properties (java.util.Properties)2 Semaphore (java.util.concurrent.Semaphore)2