Search in sources :

Example 21 with ConfigChangeListener

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);
}
Also used : Answer(org.mockito.stubbing.Answer) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) Before(org.junit.Before)

Example 22 with ConfigChangeListener

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());
}
Also used : Answer(org.mockito.stubbing.Answer) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) InvocationOnMock(org.mockito.invocation.InvocationOnMock) LinkedList(java.util.LinkedList) PropertySource(org.springframework.core.env.PropertySource) Test(org.junit.Test)

Example 23 with ConfigChangeListener

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());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 24 with ConfigChangeListener

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

Example 25 with ConfigChangeListener

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

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