ConfigurationListenerManager.java 4.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
/*
 * Copyright 1999-2015 dangdang.com.
 * <p>
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * </p>
 */

T
terrymanu 已提交
18
package io.shardingjdbc.orchestration.internal.config;
19 20 21 22

import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
23
import io.shardingjdbc.orchestration.internal.listener.ListenerManager;
24
import io.shardingjdbc.orchestration.internal.state.datasource.DataSourceService;
J
junxiong 已提交
25 26
import io.shardingjdbc.orchestration.reg.base.ChangeEvent;
import io.shardingjdbc.orchestration.reg.base.ChangeListener;
27
import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter;
J
junxiong 已提交
28
import javafx.scene.control.TextFormatter;
29 30 31 32 33 34 35 36 37 38 39
import org.apache.curator.framework.CuratorFramework;
import org.apache.curator.framework.recipes.cache.ChildData;
import org.apache.curator.framework.recipes.cache.TreeCache;
import org.apache.curator.framework.recipes.cache.TreeCacheEvent;
import org.apache.curator.framework.recipes.cache.TreeCacheListener;

/**
 * Configuration listener manager.
 *
 * @author caohao
 */
40
public final class ConfigurationListenerManager implements ListenerManager {
41
    
T
terrymanu 已提交
42
    private final ConfigurationNode configNode;
43
    
T
terrymanu 已提交
44
    private final CoordinatorRegistryCenter regCenter;
45
    
T
terrymanu 已提交
46
    private final ConfigurationService configurationService;
47
    
T
terrymanu 已提交
48
    private final DataSourceService dataSourceService;
49 50 51
    
    public ConfigurationListenerManager(final OrchestrationConfiguration config) {
        configNode = new ConfigurationNode(config.getName());
T
terrymanu 已提交
52
        regCenter = config.getRegistryCenter();
53 54 55 56
        configurationService = new ConfigurationService(config);
        dataSourceService = new DataSourceService(config);
    }
    
57
    @Override
58 59
    public void start(final ShardingDataSource shardingDataSource) {
        start(ConfigurationNode.DATA_SOURCE_NODE_PATH, shardingDataSource);
H
haocao 已提交
60 61
        start(ConfigurationNode.SHARDING_RULE_NODE_PATH, shardingDataSource);
        start(ConfigurationNode.SHARDING_PROPS_NODE_PATH, shardingDataSource);
62 63
    }
    
64
    private void start(final String node, final ShardingDataSource shardingDataSource) {
65
        String cachePath = configNode.getFullPath(node);
J
junxiong 已提交
66
        regCenter.watch(cachePath, new ChangeListener() {
67
            @Override
J
junxiong 已提交
68 69 70 71
            public void onChange(ChangeEvent event) throws Exception {
                // only handle updated event
                if (ChangeEvent.ChangeType.UPDATED == event.getChangeType() && event.getChangeData().isPresent()) {
                    shardingDataSource.renew(dataSourceService.getAvailableShardingRule(), configurationService.loadShardingProperties());
72 73 74 75 76
                }
            }
        });
    }
    
77
    @Override
78 79
    public void start(final MasterSlaveDataSource masterSlaveDataSource) {
        start(ConfigurationNode.DATA_SOURCE_NODE_PATH, masterSlaveDataSource);
H
haocao 已提交
80
        start(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH, masterSlaveDataSource);
81 82
    }
    
83
    private void start(final String node, final MasterSlaveDataSource masterSlaveDataSource) {
84
        String cachePath = configNode.getFullPath(node);
J
junxiong 已提交
85
        regCenter.watch(cachePath, new ChangeListener() {
86
            @Override
J
junxiong 已提交
87 88 89 90
            public void onChange(ChangeEvent event) throws Exception {
                // only handle updated event
                if (ChangeEvent.ChangeType.UPDATED == event.getChangeType() && event.getChangeData().isPresent()) {
                    masterSlaveDataSource.renew(dataSourceService.getAvailableMasterSlaveRule());
91 92 93 94 95
                }
            }
        });
    }
}