Search in sources :

Example 1 with ConfigChangeType

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

the class FileSystemDynamicConfiguration method processWatchEvents.

/**
 * Process the {@link WatchEvent WatchEvents} loop in async execution
 *
 * @param watchService {@link WatchService}
 */
private void processWatchEvents(WatchService watchService) {
    getWatchEventsLoopThreadPool().execute(() -> {
        // WatchEvents Loop
        while (true) {
            WatchKey watchKey = null;
            try {
                watchKey = watchService.take();
                if (watchKey.isValid()) {
                    for (WatchEvent event : watchKey.pollEvents()) {
                        WatchEvent.Kind kind = event.kind();
                        // configChangeType's key to match WatchEvent's Kind
                        ConfigChangeType configChangeType = CONFIG_CHANGE_TYPES_MAP.get(kind.name());
                        if (configChangeType != null) {
                            Path configDirectoryPath = (Path) watchKey.watchable();
                            Path currentPath = (Path) event.context();
                            Path configFilePath = configDirectoryPath.resolve(currentPath);
                            File configDirectory = configDirectoryPath.toFile();
                            executeMutually(configDirectory, () -> {
                                fireConfigChangeEvent(configDirectory, configFilePath.toFile(), configChangeType);
                                signalConfigDirectory(configDirectory);
                                return null;
                            });
                        }
                    }
                }
            } catch (Exception e) {
                return;
            } finally {
                if (watchKey != null) {
                    // reset
                    watchKey.reset();
                }
            }
        }
    });
}
Also used : Path(java.nio.file.Path) WatchKey(java.nio.file.WatchKey) WatchEvent(java.nio.file.WatchEvent) ConfigChangeType(org.apache.dubbo.common.config.configcenter.ConfigChangeType) File(java.io.File) IOException(java.io.IOException)

Example 2 with ConfigChangeType

use of org.apache.dubbo.common.config.configcenter.ConfigChangeType 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)

Aggregations

ConfigChangeType (org.apache.dubbo.common.config.configcenter.ConfigChangeType)2 File (java.io.File)1 IOException (java.io.IOException)1 Path (java.nio.file.Path)1 WatchEvent (java.nio.file.WatchEvent)1 WatchKey (java.nio.file.WatchKey)1 ConfigChangedEvent (org.apache.dubbo.common.config.configcenter.ConfigChangedEvent)1 ConfigurationListener (org.apache.dubbo.common.config.configcenter.ConfigurationListener)1