Search in sources :

Example 21 with ConfigChangeEvent

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

Example 22 with ConfigChangeEvent

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

Example 23 with ConfigChangeEvent

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());
}
Also used : ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 24 with ConfigChangeEvent

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());
}
Also used : ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test) SpringBootTest(org.springframework.boot.test.context.SpringBootTest) DirtiesContext(org.springframework.test.annotation.DirtiesContext)

Example 25 with ConfigChangeEvent

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

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