ShardingTableRuleActualDataNodesRefreshJob.java 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. //package com.xin.shardingspherejdbcdemo.config.sharding;
  2. //
  3. //import com.google.common.collect.Maps;
  4. //import lombok.extern.slf4j.Slf4j;
  5. //import org.apache.commons.compress.utils.Sets;
  6. //import org.apache.ibatis.javassist.Modifier;
  7. //import org.apache.shardingsphere.core.rule.DataNode;
  8. //import org.apache.shardingsphere.core.rule.TableRule;
  9. //import org.springframework.beans.factory.annotation.Autowired;
  10. //import org.springframework.scheduling.annotation.Scheduled;
  11. //import org.springframework.stereotype.Component;
  12. //
  13. //import javax.annotation.PostConstruct;
  14. //import javax.annotation.Resource;
  15. //import javax.sql.DataSource;
  16. //import java.lang.reflect.Field;
  17. //import java.util.Collection;
  18. //import java.util.List;
  19. //import java.util.Map;
  20. //import java.util.Set;
  21. //import java.util.concurrent.atomic.AtomicInteger;
  22. //
  23. ///**
  24. // * 基于范围分表的 ActualDataNodes 动态刷新JOB
  25. // *
  26. // * @author qimok
  27. // * @since 2020-09-07
  28. // */
  29. //@Slf4j
  30. //@Component
  31. //public class ShardingTableRuleActualDataNodesRefreshJob {
  32. // @Resource(name = "shardingDataSource")
  33. // private DataSource dataSource;
  34. //
  35. // @Autowired
  36. // private DynamicTablesProperties dynamicTables;
  37. //
  38. // /**
  39. // * 6 个小时执行一次
  40. // */
  41. // @PostConstruct
  42. // @Scheduled(fixedRate = 1000 * 60 * 60 * 12)
  43. // public void refreshActualDataNodes() throws NoSuchFieldException, IllegalAccessException {
  44. //
  45. // }
  46. //
  47. // /**
  48. // * 动态刷新数据源
  49. // */
  50. // private void dynamicRefreshDatasource(String dataSourceName, TableRule tableRule, List<DataNode> newDataNodes)
  51. // throws NoSuchFieldException, IllegalAccessException {
  52. // Set<String> actualTables = Sets.newHashSet();
  53. // Map<DataNode, Integer> dataNodeIndexMap = Maps.newHashMap();
  54. // AtomicInteger index = new AtomicInteger(0);
  55. // newDataNodes.forEach(dataNode -> {
  56. // actualTables.add(dataNode.getTableName());
  57. // if (index.intValue() == 0) {
  58. // dataNodeIndexMap.put(dataNode, 0);
  59. // } else {
  60. // dataNodeIndexMap.put(dataNode, index.intValue());
  61. // }
  62. // index.incrementAndGet();
  63. // });
  64. // // 动态刷新:actualDataNodesField
  65. // Field actualDataNodesField = TableRule.class.getDeclaredField("actualDataNodes");
  66. // Field modifiersField = Field.class.getDeclaredField("modifiers");
  67. // modifiersField.setAccessible(true);
  68. // modifiersField.setInt(actualDataNodesField, actualDataNodesField.getModifiers() & ~Modifier.FINAL);
  69. // actualDataNodesField.setAccessible(true);
  70. // actualDataNodesField.set(tableRule, newDataNodes);
  71. //
  72. // }
  73. //}