Search in sources :

Example 1 with ConfigurationListener

use of org.apache.dubbo.common.config.configcenter.ConfigurationListener in project dubbo by alibaba.

the class ApolloDynamicConfigurationTest method testAddListener.

/**
 * Test add listener.
 *
 * @throws Exception the exception
 */
@Test
@Order(3)
public void testAddListener() throws Exception {
    String mockKey = "mockKey3";
    String mockValue = String.valueOf(new Random().nextInt());
    final SettableFuture<org.apache.dubbo.common.config.configcenter.ConfigChangedEvent> future = SettableFuture.create();
    apolloDynamicConfiguration = new ApolloDynamicConfiguration(url);
    apolloDynamicConfiguration.addListener(mockKey, DEFAULT_NAMESPACE, new ConfigurationListener() {

        @Override
        public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent event) {
            future.set(event);
        }
    });
    putData(mockKey, mockValue);
    org.apache.dubbo.common.config.configcenter.ConfigChangedEvent result = future.get(3000, TimeUnit.MILLISECONDS);
    assertEquals(mockValue, result.getContent());
    assertEquals(mockKey, result.getKey());
    assertEquals(ConfigChangeType.MODIFIED, result.getChangeType());
}
Also used : ConfigurationListener(org.apache.dubbo.common.config.configcenter.ConfigurationListener) Random(java.util.Random) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Example 2 with ConfigurationListener

use of org.apache.dubbo.common.config.configcenter.ConfigurationListener in project dubbo by alibaba.

the class CacheListener method dataChanged.

@Override
public void dataChanged(String path, Object value, EventType eventType) {
    ConfigChangeType changeType;
    if (EventType.NodeCreated.equals(eventType)) {
        changeType = ConfigChangeType.ADDED;
    } else if (value == null) {
        changeType = ConfigChangeType.DELETED;
    } else {
        changeType = ConfigChangeType.MODIFIED;
    }
    String key = pathToKey(path);
    ConfigChangedEvent configChangeEvent = new ConfigChangedEvent(key, getGroup(path), (String) value, changeType);
    Set<ConfigurationListener> listeners = keyListeners.get(path);
    if (CollectionUtils.isNotEmpty(listeners)) {
        listeners.forEach(listener -> listener.process(configChangeEvent));
    }
}
Also used : ConfigurationListener(org.apache.dubbo.common.config.configcenter.ConfigurationListener) ConfigChangeType(org.apache.dubbo.common.config.configcenter.ConfigChangeType) ConfigChangedEvent(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent)

Example 3 with ConfigurationListener

use of org.apache.dubbo.common.config.configcenter.ConfigurationListener in project dubbo by alibaba.

the class FileSystemDynamicConfiguration method doInListener.

private void doInListener(String configFilePath, BiConsumer<File, List<ConfigurationListener>> consumer) {
    watchService.ifPresent(watchService -> {
        File configFile = new File(configFilePath);
        executeMutually(configFile.getParentFile(), () -> {
            // process the WatchEvents if not start
            if (!isProcessingWatchEvents()) {
                processWatchEvents(watchService);
            }
            List<ConfigurationListener> listeners = getListeners(configFile);
            consumer.accept(configFile, listeners);
            // Nothing to return
            return null;
        });
    });
}
Also used : ConfigurationListener(org.apache.dubbo.common.config.configcenter.ConfigurationListener) File(java.io.File)

Example 4 with ConfigurationListener

use of org.apache.dubbo.common.config.configcenter.ConfigurationListener in project dubbo by alibaba.

the class ApolloDynamicConfigurationTest method testRemoveListener.

/**
 * Test remove listener.
 *
 * @throws Exception the exception
 */
@Test
@Order(4)
public void testRemoveListener() throws Exception {
    String mockKey = "mockKey4";
    String mockValue = String.valueOf(new Random().nextInt());
    final SettableFuture<org.apache.dubbo.common.config.configcenter.ConfigChangedEvent> future = SettableFuture.create();
    apolloDynamicConfiguration = new ApolloDynamicConfiguration(url);
    apolloDynamicConfiguration.addListener(mockKey, DEFAULT_NAMESPACE, new ConfigurationListener() {

        @Override
        public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent event) {
            future.set(event);
        }
    });
    putData(mockKey, mockValue);
    future.get(3000, TimeUnit.MILLISECONDS);
    apolloDynamicConfiguration.removeListener(mockKey, DEFAULT_NAMESPACE, new ConfigurationListener() {

        @Override
        public void process(org.apache.dubbo.common.config.configcenter.ConfigChangedEvent event) {
            future.set(event);
        }
    });
    deleteData(mockKey);
    org.apache.dubbo.common.config.configcenter.ConfigChangedEvent result = future.get(3000, TimeUnit.MILLISECONDS);
    assertEquals(mockValue, result.getContent());
    assertEquals(mockKey, result.getKey());
    assertEquals(ConfigChangeType.MODIFIED, result.getChangeType());
}
Also used : ConfigurationListener(org.apache.dubbo.common.config.configcenter.ConfigurationListener) Random(java.util.Random) TestMethodOrder(org.junit.jupiter.api.TestMethodOrder) Order(org.junit.jupiter.api.Order) Test(org.junit.jupiter.api.Test)

Aggregations

ConfigurationListener (org.apache.dubbo.common.config.configcenter.ConfigurationListener)4 Random (java.util.Random)2 Order (org.junit.jupiter.api.Order)2 Test (org.junit.jupiter.api.Test)2 TestMethodOrder (org.junit.jupiter.api.TestMethodOrder)2 File (java.io.File)1 ConfigChangeType (org.apache.dubbo.common.config.configcenter.ConfigChangeType)1 ConfigChangedEvent (org.apache.dubbo.common.config.configcenter.ConfigChangedEvent)1