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

T
terrymanu 已提交
20
import io.shardingjdbc.core.exception.ShardingJdbcException;
21 22 23
import io.shardingjdbc.core.jdbc.core.datasource.MasterSlaveDataSource;
import io.shardingjdbc.core.jdbc.core.datasource.ShardingDataSource;
import io.shardingjdbc.orchestration.api.config.OrchestrationConfiguration;
24
import io.shardingjdbc.orchestration.internal.listener.ListenerManager;
25
import io.shardingjdbc.orchestration.internal.state.datasource.DataSourceService;
T
terrymanu 已提交
26
import io.shardingjdbc.orchestration.reg.base.DataChangedEvent;
T
terrymanu 已提交
27
import io.shardingjdbc.orchestration.reg.base.EventListener;
28 29
import io.shardingjdbc.orchestration.reg.base.CoordinatorRegistryCenter;

T
terrymanu 已提交
30 31
import java.sql.SQLException;

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