Search in sources :

Example 1 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.

the class AbstractConfig method fireConfigChange.

protected void fireConfigChange(final ConfigChangeEvent changeEvent) {
    for (final ConfigChangeListener listener : m_listeners) {
        m_executorService.submit(new Runnable() {

            @Override
            public void run() {
                String listenerName = listener.getClass().getName();
                Transaction transaction = Tracer.newTransaction("Apollo.ConfigChangeListener", listenerName);
                try {
                    listener.onChange(changeEvent);
                    transaction.setStatus(Transaction.SUCCESS);
                } catch (Throwable ex) {
                    transaction.setStatus(ex);
                    Tracer.logError(ex);
                    logger.error("Failed to invoke config change listener {}", listenerName, ex);
                } finally {
                    transaction.complete();
                }
            }
        });
    }
}
Also used : Transaction(com.ctrip.framework.apollo.tracer.spi.Transaction) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener)

Example 2 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.

the class AbstractConfigTest method testFireConfigChange_event_notify_once.

@Test
public void testFireConfigChange_event_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));
    ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
    abstractConfig.fireConfigChange(configChangeEvent);
    assertEquals(configChangeEvent, future1.get(500, TimeUnit.MILLISECONDS));
    assertEquals(configChangeEvent, future2.get(500, TimeUnit.MILLISECONDS));
    assertEquals(2, invokeCount.get());
    verify(configChangeListener1, times(1)).onChange(Matchers.eq(configChangeEvent));
    verify(configChangeListener2, times(1)).onChange(Matchers.eq(configChangeEvent));
}
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 3 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.

the class AbstractConfigTest method testFireConfigChange_cannot_notify.

/**
 * @see AbstractConfig#fireConfigChange(ConfigChangeEvent)
 */
@Test
public void testFireConfigChange_cannot_notify() throws InterruptedException {
    AbstractConfig abstractConfig = spy(new ErrorConfig());
    final String namespace = "app-namespace-0";
    ConfigChangeListener configChangeListener = spy(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
        }
    });
    abstractConfig.addChangeListener(configChangeListener, Collections.singleton("cannot-be-match-key"));
    Map<String, ConfigChange> changes = new HashMap<>();
    changes.put("key1", new ConfigChange(namespace, "key1", null, "new-value", PropertyChangeType.ADDED));
    ConfigChangeEvent configChangeEvent = new ConfigChangeEvent(namespace, changes);
    abstractConfig.fireConfigChange(configChangeEvent);
    abstractConfig.fireConfigChange(namespace, changes);
    // wait a minute for invoking
    Thread.sleep(100);
    verify(configChangeListener, times(0)).onChange(Matchers.<ConfigChangeEvent>any());
}
Also used : ConfigChange(com.ctrip.framework.apollo.model.ConfigChange) ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) HashMap(java.util.HashMap) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 4 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.

the class ApolloMockServerApiTest method testUpdateProperties.

@Test
public void testUpdateProperties() throws Exception {
    String someNewValue = "someNewValue";
    Config otherConfig = ConfigService.getConfig(anotherNamespace);
    final SettableFuture<ConfigChangeEvent> future = SettableFuture.create();
    otherConfig.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            future.set(changeEvent);
        }
    });
    assertEquals("otherValue1", otherConfig.getProperty("key1", null));
    assertEquals("otherValue2", otherConfig.getProperty("key2", null));
    embeddedApollo.addOrModifyProperty(anotherNamespace, "key1", someNewValue);
    ConfigChangeEvent changeEvent = future.get(5, TimeUnit.SECONDS);
    assertEquals(someNewValue, otherConfig.getProperty("key1", null));
    assertEquals("otherValue2", otherConfig.getProperty("key2", null));
    assertTrue(changeEvent.isChanged("key1"));
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Test(org.junit.Test)

Example 5 with ConfigChangeListener

use of com.ctrip.framework.apollo.ConfigChangeListener in project apollo by ctripcorp.

the class ApolloMockServerApiTest method testDeleteSamePropertyTwice.

@Test
public void testDeleteSamePropertyTwice() throws Exception {
    Config otherConfig = ConfigService.getConfig(anotherNamespace);
    final Semaphore changes = new Semaphore(0);
    otherConfig.addChangeListener(new ConfigChangeListener() {

        @Override
        public void onChange(ConfigChangeEvent changeEvent) {
            changes.release();
        }
    });
    assertEquals("otherValue6", otherConfig.getProperty("key6", null));
    embeddedApollo.deleteProperty(anotherNamespace, "key6");
    embeddedApollo.deleteProperty(anotherNamespace, "key6");
    assertTrue(changes.tryAcquire(5, TimeUnit.SECONDS));
    assertNull(otherConfig.getProperty("key6", null));
    assertEquals(0, changes.availablePermits());
}
Also used : ConfigChangeListener(com.ctrip.framework.apollo.ConfigChangeListener) Config(com.ctrip.framework.apollo.Config) ConfigChangeEvent(com.ctrip.framework.apollo.model.ConfigChangeEvent) Semaphore(java.util.concurrent.Semaphore) Test(org.junit.Test)

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