/* * Copyright 2016-2018 shardingsphere.io. *

* 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. *

*/ package io.shardingsphere.jdbc.orchestration.internal.state.datasource; import io.shardingsphere.core.api.config.MasterSlaveRuleConfiguration; import io.shardingsphere.core.exception.ShardingException; import io.shardingsphere.core.jdbc.core.datasource.MasterSlaveDataSource; import io.shardingsphere.core.jdbc.core.datasource.ShardingDataSource; import io.shardingsphere.core.rule.ShardingRule; import io.shardingsphere.jdbc.orchestration.internal.config.ConfigurationService; import io.shardingsphere.jdbc.orchestration.internal.listener.ListenerManager; import io.shardingsphere.jdbc.orchestration.internal.state.StateNode; import io.shardingsphere.jdbc.orchestration.reg.api.RegistryCenter; import io.shardingsphere.jdbc.orchestration.reg.listener.DataChangedEvent; import io.shardingsphere.jdbc.orchestration.reg.listener.EventListener; import javax.sql.DataSource; import java.util.Map; /** * Data source listener manager. * * @author caohao */ public final class DataSourceListenerManager implements ListenerManager { private final StateNode stateNode; private final RegistryCenter regCenter; private final ConfigurationService configService; private final DataSourceService dataSourceService; public DataSourceListenerManager(final String name, final RegistryCenter regCenter) { stateNode = new StateNode(name); this.regCenter = regCenter; configService = new ConfigurationService(name, regCenter); dataSourceService = new DataSourceService(name, regCenter); } @Override public void start(final ShardingDataSource shardingDataSource) { regCenter.watch(stateNode.getDataSourcesNodeFullPath(), new EventListener() { @Override public void onChange(final DataChangedEvent event) { if (DataChangedEvent.Type.UPDATED == event.getEventType() || DataChangedEvent.Type.DELETED == event.getEventType()) { Map dataSourceMap = dataSourceService.getAvailableDataSources(); shardingDataSource.renew( dataSourceMap, new ShardingRule(dataSourceService.getAvailableShardingRuleConfiguration(), dataSourceMap.keySet()), configService.loadShardingProperties()); } } }); } @Override public void start(final MasterSlaveDataSource masterSlaveDataSource) { regCenter.watch(stateNode.getDataSourcesNodeFullPath(), new EventListener() { @Override public void onChange(final DataChangedEvent event) { if (DataChangedEvent.Type.UPDATED == event.getEventType() || DataChangedEvent.Type.DELETED == event.getEventType()) { MasterSlaveRuleConfiguration masterSlaveRuleConfiguration = dataSourceService.getAvailableMasterSlaveRuleConfiguration(); if (masterSlaveRuleConfiguration.getSlaveDataSourceNames().isEmpty()) { throw new ShardingException("No available slave datasource, can't apply the configuration!"); } masterSlaveDataSource.renew(dataSourceService.getAvailableDataSources(), masterSlaveRuleConfiguration); } } }); } }