Search in sources :

Example 1 with DataChangedEvent

use of io.shardingjdbc.orchestration.reg.listener.DataChangedEvent in project sharding-jdbc by shardingjdbc.

the class ConfigMapListenerManager method start.

@Override
public void start(final MasterSlaveDataSource masterSlaveDataSource) {
    String cachePath = configNode.getFullPath(ConfigurationNode.MASTER_SLAVE_CONFIG_MAP_NODE_PATH);
    regCenter.watch(cachePath, new EventListener() {

        @Override
        public void onChange(final DataChangedEvent event) {
            if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                ConfigMapContext.getInstance().getMasterSlaveConfig().clear();
                ConfigMapContext.getInstance().getMasterSlaveConfig().putAll(configService.loadMasterSlaveConfigMap());
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) EventListener(io.shardingjdbc.orchestration.reg.listener.EventListener)

Example 2 with DataChangedEvent

use of io.shardingjdbc.orchestration.reg.listener.DataChangedEvent in project sharding-jdbc by shardingjdbc.

the class ConfigMapListenerManager method start.

@Override
public void start(final ShardingDataSource shardingDataSource) {
    String cachePath = configNode.getFullPath(ConfigurationNode.SHARDING_CONFIG_MAP_NODE_PATH);
    regCenter.watch(cachePath, new EventListener() {

        @Override
        public void onChange(final DataChangedEvent event) {
            if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                ConfigMapContext.getInstance().getShardingConfig().clear();
                ConfigMapContext.getInstance().getShardingConfig().putAll(configService.loadShardingConfigMap());
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) EventListener(io.shardingjdbc.orchestration.reg.listener.EventListener)

Example 3 with DataChangedEvent

use of io.shardingjdbc.orchestration.reg.listener.DataChangedEvent in project sharding-jdbc by shardingjdbc.

the class ConfigurationListenerManager method start.

private void start(final String node, final MasterSlaveDataSource masterSlaveDataSource) {
    String cachePath = configNode.getFullPath(node);
    regCenter.watch(cachePath, new EventListener() {

        @Override
        public void onChange(final DataChangedEvent event) {
            if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                masterSlaveDataSource.renew(dataSourceService.getAvailableDataSources(), dataSourceService.getAvailableMasterSlaveRuleConfiguration());
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) EventListener(io.shardingjdbc.orchestration.reg.listener.EventListener)

Example 4 with DataChangedEvent

use of io.shardingjdbc.orchestration.reg.listener.DataChangedEvent in project sharding-jdbc by shardingjdbc.

the class ConfigurationListenerManager method start.

private void start(final String node, final ShardingDataSource shardingDataSource) {
    String cachePath = configNode.getFullPath(node);
    regCenter.watch(cachePath, new EventListener() {

        @Override
        public void onChange(final DataChangedEvent event) {
            if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
                Map<String, DataSource> dataSourceMap = dataSourceService.getAvailableDataSources();
                shardingDataSource.renew(dataSourceMap, new ShardingRule(dataSourceService.getAvailableShardingRuleConfiguration(), dataSourceMap.keySet()), configService.loadShardingProperties());
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) ShardingRule(io.shardingjdbc.core.rule.ShardingRule) EventListener(io.shardingjdbc.orchestration.reg.listener.EventListener) Map(java.util.Map)

Example 5 with DataChangedEvent

use of io.shardingjdbc.orchestration.reg.listener.DataChangedEvent in project sharding-jdbc by shardingjdbc.

the class ZookeeperRegistryCenter method watch.

@Override
public void watch(final String key, final EventListener eventListener) {
    final String path = key + "/";
    if (!caches.containsKey(path)) {
        addCacheData(key);
    }
    TreeCache cache = caches.get(path);
    cache.getListenable().addListener(new TreeCacheListener() {

        @Override
        public void childEvent(final CuratorFramework client, final TreeCacheEvent event) throws Exception {
            ChildData data = event.getData();
            if (null == data || null == data.getPath()) {
                return;
            }
            eventListener.onChange(new DataChangedEvent(getEventType(event), data.getPath(), null == data.getData() ? null : new String(data.getData(), "UTF-8")));
        }

        private DataChangedEvent.Type getEventType(final TreeCacheEvent event) {
            switch(event.getType()) {
                case NODE_UPDATED:
                    return DataChangedEvent.Type.UPDATED;
                case NODE_REMOVED:
                    return DataChangedEvent.Type.DELETED;
                default:
                    return DataChangedEvent.Type.IGNORED;
            }
        }
    });
}
Also used : DataChangedEvent(io.shardingjdbc.orchestration.reg.listener.DataChangedEvent) TreeCache(org.apache.curator.framework.recipes.cache.TreeCache) CuratorFramework(org.apache.curator.framework.CuratorFramework) TreeCacheListener(org.apache.curator.framework.recipes.cache.TreeCacheListener) ChildData(org.apache.curator.framework.recipes.cache.ChildData) TreeCacheEvent(org.apache.curator.framework.recipes.cache.TreeCacheEvent) OperationTimeoutException(org.apache.zookeeper.KeeperException.OperationTimeoutException)

Aggregations

DataChangedEvent (io.shardingjdbc.orchestration.reg.listener.DataChangedEvent)5 EventListener (io.shardingjdbc.orchestration.reg.listener.EventListener)4 ShardingRule (io.shardingjdbc.core.rule.ShardingRule)1 Map (java.util.Map)1 CuratorFramework (org.apache.curator.framework.CuratorFramework)1 ChildData (org.apache.curator.framework.recipes.cache.ChildData)1 TreeCache (org.apache.curator.framework.recipes.cache.TreeCache)1 TreeCacheEvent (org.apache.curator.framework.recipes.cache.TreeCacheEvent)1 TreeCacheListener (org.apache.curator.framework.recipes.cache.TreeCacheListener)1 OperationTimeoutException (org.apache.zookeeper.KeeperException.OperationTimeoutException)1