ConfigurationListenerManager.java 3.7 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

import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
22
import io.shardingjdbc.orchestration.internal.listener.ListenerManager;
23
import io.shardingjdbc.orchestration.internal.state.datasource.DataSourceService;
24
import io.shardingjdbc.orchestration.reg.api.RegistryCenter;
25 26
import io.shardingjdbc.orchestration.reg.listener.DataChangedEvent;
import io.shardingjdbc.orchestration.reg.listener.EventListener;
27

28 29
import javax.sql.DataSource;
import java.util.Map;
T
terrymanu 已提交
30

31 32 33 34 35
/**
 * Configuration listener manager.
 *
 * @author caohao
 */
36
public final class ConfigurationListenerManager implements ListenerManager {
37
    
T
terrymanu 已提交
38
    private final ConfigurationNode configNode;
39
    
40
    private final RegistryCenter regCenter;
41
    
42
    private final ConfigurationService configService;
43
    
T
terrymanu 已提交
44
    private final DataSourceService dataSourceService;
45
    
46 47
    public ConfigurationListenerManager(final String name, final RegistryCenter regCenter) {
        configNode = new ConfigurationNode(name);
48
        this.regCenter = regCenter;
49 50
        configService = new ConfigurationService(name, regCenter);
        dataSourceService = new DataSourceService(name, regCenter);
51 52
    }
    
53
    @Override
54 55
    public void start(final ShardingDataSource shardingDataSource) {
        start(ConfigurationNode.DATA_SOURCE_NODE_PATH, shardingDataSource);
H
haocao 已提交
56 57
        start(ConfigurationNode.SHARDING_RULE_NODE_PATH, shardingDataSource);
        start(ConfigurationNode.SHARDING_PROPS_NODE_PATH, shardingDataSource);
58 59
    }
    
60
    private void start(final String node, final ShardingDataSource shardingDataSource) {
61
        String cachePath = configNode.getFullPath(node);
T
terrymanu 已提交
62
        regCenter.watch(cachePath, new EventListener() {
T
terrymanu 已提交
63
            
64
            @Override
T
terrymanu 已提交
65
            public void onChange(final DataChangedEvent event) {
T
terrymanu 已提交
66
                if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
T
terrymanu 已提交
67 68
                    Map<String, DataSource> dataSourceMap = dataSourceService.getAvailableDataSources();
                    shardingDataSource.renew(dataSourceMap, dataSourceService.getAvailableShardingRuleConfiguration().build(dataSourceMap.keySet()), configService.loadShardingProperties());
69 70 71 72 73
                }
            }
        });
    }
    
74
    @Override
75 76
    public void start(final MasterSlaveDataSource masterSlaveDataSource) {
        start(ConfigurationNode.DATA_SOURCE_NODE_PATH, masterSlaveDataSource);
H
haocao 已提交
77
        start(ConfigurationNode.MASTER_SLAVE_RULE_NODE_PATH, masterSlaveDataSource);
78 79
    }
    
80
    private void start(final String node, final MasterSlaveDataSource masterSlaveDataSource) {
81
        String cachePath = configNode.getFullPath(node);
T
terrymanu 已提交
82
        regCenter.watch(cachePath, new EventListener() {
T
terrymanu 已提交
83
            
84
            @Override
T
terrymanu 已提交
85
            public void onChange(final DataChangedEvent event) {
T
terrymanu 已提交
86
                if (DataChangedEvent.Type.UPDATED == event.getEventType()) {
87
                    masterSlaveDataSource.renew(dataSourceService.getAvailableDataSources(), dataSourceService.getAvailableMasterSlaveRuleConfiguration());
88 89 90 91 92
                }
            }
        });
    }
}