ConfigurationListenerManager.java 3.6 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;
T
terrymanu 已提交
25
import io.shardingjdbc.orchestration.reg.base.DataChangedEvent;
J
junxiong 已提交
26
import io.shardingjdbc.orchestration.reg.base.ChangeListener;
27 28 29 30 31 32 33
import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter;

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