//package com.xin.shardingspherejdbcdemo.config.sharding; // //import com.google.common.collect.Maps; //import lombok.extern.slf4j.Slf4j; //import org.apache.commons.compress.utils.Sets; //import org.apache.ibatis.javassist.Modifier; //import org.apache.shardingsphere.core.rule.DataNode; //import org.apache.shardingsphere.core.rule.TableRule; //import org.springframework.beans.factory.annotation.Autowired; //import org.springframework.scheduling.annotation.Scheduled; //import org.springframework.stereotype.Component; // //import javax.annotation.PostConstruct; //import javax.annotation.Resource; //import javax.sql.DataSource; //import java.lang.reflect.Field; //import java.util.Collection; //import java.util.List; //import java.util.Map; //import java.util.Set; //import java.util.concurrent.atomic.AtomicInteger; // ///** // * 基于范围分表的 ActualDataNodes 动态刷新JOB // * // * @author qimok // * @since 2020-09-07 // */ //@Slf4j //@Component //public class ShardingTableRuleActualDataNodesRefreshJob { // @Resource(name = "shardingDataSource") // private DataSource dataSource; // // @Autowired // private DynamicTablesProperties dynamicTables; // // /** // * 6 个小时执行一次 // */ // @PostConstruct // @Scheduled(fixedRate = 1000 * 60 * 60 * 12) // public void refreshActualDataNodes() throws NoSuchFieldException, IllegalAccessException { // // } // // /** // * 动态刷新数据源 // */ // private void dynamicRefreshDatasource(String dataSourceName, TableRule tableRule, List newDataNodes) // throws NoSuchFieldException, IllegalAccessException { // Set actualTables = Sets.newHashSet(); // Map dataNodeIndexMap = Maps.newHashMap(); // AtomicInteger index = new AtomicInteger(0); // newDataNodes.forEach(dataNode -> { // actualTables.add(dataNode.getTableName()); // if (index.intValue() == 0) { // dataNodeIndexMap.put(dataNode, 0); // } else { // dataNodeIndexMap.put(dataNode, index.intValue()); // } // index.incrementAndGet(); // }); // // 动态刷新:actualDataNodesField // Field actualDataNodesField = TableRule.class.getDeclaredField("actualDataNodes"); // Field modifiersField = Field.class.getDeclaredField("modifiers"); // modifiersField.setAccessible(true); // modifiersField.setInt(actualDataNodesField, actualDataNodesField.getModifiers() & ~Modifier.FINAL); // actualDataNodesField.setAccessible(true); // actualDataNodesField.set(tableRule, newDataNodes); // // } //}