Search in sources :

Example 1 with ConfigFileChangeListener

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

the class AbstractConfigFile method fireConfigChange.

private void fireConfigChange(final ConfigFileChangeEvent changeEvent) {
    for (final ConfigFileChangeListener listener : m_listeners) {
        m_executorService.submit(new Runnable() {

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

Example 2 with ConfigFileChangeListener

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

the class PropertiesConfigFileTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String someKey = "someKey";
    String someValue = "someValue";
    String anotherValue = "anotherValue";
    someProperties.setProperty(someKey, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    PropertiesConfigFile configFile = new PropertiesConfigFile(someNamespace, configRepository);
    assertTrue(configFile.getContent().contains(String.format("%s=%s", someKey, someValue)));
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(someKey, anotherValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertFalse(configFile.getContent().contains(String.format("%s=%s", someKey, someValue)));
    assertTrue(configFile.getContent().contains(String.format("%s=%s", someKey, anotherValue)));
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertTrue(changeEvent.getOldValue().contains(String.format("%s=%s", someKey, someValue)));
    assertTrue(changeEvent.getNewValue().contains(String.format("%s=%s", someKey, anotherValue)));
    assertEquals(PropertyChangeType.MODIFIED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 3 with ConfigFileChangeListener

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

the class XmlConfigFileTest method testOnRepositoryChangeWithContentAdded.

@Test
public void testOnRepositoryChangeWithContentAdded() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(null, configFile.getContent());
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(key, someValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(someValue, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(null, changeEvent.getOldValue());
    assertEquals(someValue, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.ADDED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 4 with ConfigFileChangeListener

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

the class XmlConfigFileTest method testOnRepositoryChangeWithContentDeleted.

@Test
public void testOnRepositoryChangeWithContentDeleted() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    someProperties.setProperty(key, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(someValue, configFile.getContent());
    Properties anotherProperties = new Properties();
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(null, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(someValue, changeEvent.getOldValue());
    assertEquals(null, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.DELETED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Example 5 with ConfigFileChangeListener

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

the class XmlConfigFileTest method testOnRepositoryChange.

@Test
public void testOnRepositoryChange() throws Exception {
    Properties someProperties = new Properties();
    String key = ConfigConsts.CONFIG_FILE_CONTENT_KEY;
    String someValue = "someValue";
    String anotherValue = "anotherValue";
    someProperties.setProperty(key, someValue);
    when(configRepository.getConfig()).thenReturn(someProperties);
    XmlConfigFile configFile = new XmlConfigFile(someNamespace, configRepository);
    assertEquals(someValue, configFile.getContent());
    Properties anotherProperties = new Properties();
    anotherProperties.setProperty(key, anotherValue);
    final SettableFuture<ConfigFileChangeEvent> configFileChangeFuture = SettableFuture.create();
    ConfigFileChangeListener someListener = new ConfigFileChangeListener() {

        @Override
        public void onChange(ConfigFileChangeEvent changeEvent) {
            configFileChangeFuture.set(changeEvent);
        }
    };
    configFile.addChangeListener(someListener);
    configFile.onRepositoryChange(someNamespace, anotherProperties);
    ConfigFileChangeEvent changeEvent = configFileChangeFuture.get(500, TimeUnit.MILLISECONDS);
    assertEquals(anotherValue, configFile.getContent());
    assertEquals(someNamespace, changeEvent.getNamespace());
    assertEquals(someValue, changeEvent.getOldValue());
    assertEquals(anotherValue, changeEvent.getNewValue());
    assertEquals(PropertyChangeType.MODIFIED, changeEvent.getChangeType());
}
Also used : ConfigFileChangeEvent(com.ctrip.framework.apollo.model.ConfigFileChangeEvent) ConfigFileChangeListener(com.ctrip.framework.apollo.ConfigFileChangeListener) Properties(java.util.Properties) Test(org.junit.Test)

Aggregations

ConfigFileChangeListener (com.ctrip.framework.apollo.ConfigFileChangeListener)5 ConfigFileChangeEvent (com.ctrip.framework.apollo.model.ConfigFileChangeEvent)4 Properties (java.util.Properties)4 Test (org.junit.Test)4 Transaction (com.ctrip.framework.apollo.tracer.spi.Transaction)1