PoolUpdater can set Schedule Period now

This commit is contained in:
DigitalSonic
2020-01-28 20:14:16 +08:00
parent cff870e80e
commit 8c57759eaa
3 changed files with 47 additions and 19 deletions
@@ -96,6 +96,7 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
private DataSourceSelector selector;
private String dataSourceFile = DEFAULT_DATA_SOURCE_FILE;
private String propertyPrefix = "";
private int poolUpdatePeriodSeconds = PoolUpdater.DEFAULT_PERIOD;
private volatile boolean inited = false;
@@ -110,6 +111,8 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
if (inited) {
return;
}
poolUpdater.setPeriodSeconds(poolUpdatePeriodSeconds);
poolUpdater.init();
if (dataSourceMap == null || dataSourceMap.isEmpty()) {
createNodeMap();
}
@@ -300,12 +303,12 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
// Getters & Setters
public PoolUpdater getPoolUpdater() {
return poolUpdater;
public int getPoolUpdatePeriodSeconds() {
return poolUpdatePeriodSeconds;
}
public void setPoolUpdater(PoolUpdater poolUpdater) {
this.poolUpdater = poolUpdater;
public void setPoolUpdatePeriodSeconds(int poolUpdatePeriodSeconds) {
this.poolUpdatePeriodSeconds = poolUpdatePeriodSeconds;
}
public NodeListener getNodeListener() {
@@ -40,29 +40,17 @@ import java.util.concurrent.locks.ReentrantLock;
* @author DigitalSonic
*/
public class PoolUpdater implements Observer {
public final static int DEFAULT_PERIOD = 60;
private final static Log LOG = LogFactory.getLog(PoolUpdater.class);
private Set<String> nodesToDel = new CopyOnWriteArraySet<String>();
private HighAvailableDataSource highAvailableDataSource;
private Lock lock = new ReentrantLock();
private ScheduledExecutorService executor;
private int periodSeconds = DEFAULT_PERIOD;
private boolean inited = false;
/**
* Create a ScheduledExecutorService to remove unused DataSources.
*/
public PoolUpdater() {
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
removeDataSources();
} catch (Exception e) {
LOG.error("Exception occurred while removing DataSources.", e);
}
}
}, 60, 60, TimeUnit.SECONDS);
}
/**
@@ -73,6 +61,34 @@ public class PoolUpdater implements Observer {
setHighAvailableDataSource(highAvailableDataSource);
}
/**
* Create a ScheduledExecutorService to remove unused DataSources.
*/
public void init() {
if (inited) {
return;
}
synchronized (this) {
if (inited) {
return;
}
if (periodSeconds <= 0) {
periodSeconds = DEFAULT_PERIOD;
}
executor = Executors.newScheduledThreadPool(1);
executor.scheduleAtFixedRate(new Runnable() {
@Override
public void run() {
try {
removeDataSources();
} catch (Exception e) {
LOG.error("Exception occurred while removing DataSources.", e);
}
}
}, periodSeconds, periodSeconds, TimeUnit.SECONDS);
}
}
public void destroy() {
if (executor == null || executor.isShutdown()) {
return;
@@ -229,4 +245,12 @@ public class PoolUpdater implements Observer {
public Set<String> getNodesToDel() {
return nodesToDel;
}
public int getPeriodSeconds() {
return periodSeconds;
}
public void setPeriodSeconds(int periodSeconds) {
this.periodSeconds = periodSeconds;
}
}
@@ -24,6 +24,7 @@ public class PoolUpdaterTest {
haDataSource.setDataSourceMap(map);
updater = new PoolUpdater(haDataSource);
updater.init();
}
@After