refactor the codes related to DataSourceSelector

This commit is contained in:
DigitalSonic
2019-02-02 17:23:13 +08:00
parent dfcc16107a
commit bd87a038be
7 changed files with 70 additions and 20 deletions
@@ -167,11 +167,9 @@ public class HighAvailableDataSource extends WrapperAdapter implements DataSourc
}
public void setSelector(String name) {
if (name != null && this.selector != null && this.selector.isSame(name)) {
return;
}
DataSourceSelector selector = DataSourceSelectorFactory.getSelector(name, this);
if (selector != null) {
selector.init();
setDataSourceSelector(selector);
}
}
@@ -27,7 +27,6 @@ import java.util.Map;
public interface DataSourceSelector {
DataSource get();
void setTarget(String name);
boolean isSame(String name);
String getName();
void init();
}
@@ -0,0 +1,53 @@
package com.alibaba.druid.pool.ha.selector;
import com.alibaba.druid.pool.ha.HighAvailableDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
/**
* An enum holding the names and classes of DataSourceSelector.
*
* @author DigitalSonic
*/
public enum DataSourceSelectorEnum {
BY_NAME("byName", NamedDataSourceSelector.class),
RANDOM("random", RandomDataSourceSelector.class),
STICKY_RANDOM("stickyRandom", StickyRandomDataSourceSelector.class);
private final static Log LOG = LogFactory.getLog(DataSourceSelectorEnum.class);
private String name;
private Class<? extends DataSourceSelector> clazz;
DataSourceSelectorEnum(String name, Class<? extends DataSourceSelector> clazz) {
this.name = name;
this.clazz = clazz;
}
/**
* Create a new instance of the DataSourceSelector represented by this enum.
*
* @return null if dataSource is not given or exception occurred while creating new instance
*/
public DataSourceSelector newInstance(HighAvailableDataSource dataSource) {
if (dataSource == null) {
LOG.warn("You should provide an instance of HighAvailableDataSource!");
return null;
}
DataSourceSelector selector = null;
try {
selector = clazz.getDeclaredConstructor(HighAvailableDataSource.class).newInstance(dataSource);
} catch (Exception e) {
LOG.error("Can not create new instance of " + clazz.getName(), e);
}
return selector;
}
public String getName() {
return name;
}
public Class<? extends DataSourceSelector> getClazz() {
return clazz;
}
}
@@ -23,11 +23,16 @@ import com.alibaba.druid.pool.ha.HighAvailableDataSource;
* @author DigitalSonic
*/
public class DataSourceSelectorFactory {
/**
* Get a new instance of the given selector name.
*
* @return null if the given name do not represent a DataSourceSelector
*/
public static DataSourceSelector getSelector(String name, HighAvailableDataSource highAvailableDataSource) {
if ("random".equalsIgnoreCase(name)) {
return new RandomDataSourceSelector(highAvailableDataSource);
} else if ("byName".equalsIgnoreCase(name)) {
return new NamedDataSourceSelector(highAvailableDataSource);
for (DataSourceSelectorEnum e : DataSourceSelectorEnum.values()) {
if (e.getName().equalsIgnoreCase(name)) {
return e.newInstance(highAvailableDataSource);
}
}
return null;
}
@@ -39,14 +39,9 @@ public class NamedDataSourceSelector implements DataSourceSelector {
public void init() {
}
@Override
public boolean isSame(String name) {
return getName().equalsIgnoreCase(name);
}
@Override
public String getName() {
return "byName";
return DataSourceSelectorEnum.BY_NAME.getName();
}
@Override
@@ -74,14 +74,9 @@ public class RandomDataSourceSelector implements DataSourceSelector {
}
}
@Override
public boolean isSame(String name) {
return getName().equalsIgnoreCase(name);
}
@Override
public String getName() {
return "random";
return DataSourceSelectorEnum.RANDOM.getName();
}
@Override
@@ -25,6 +25,11 @@ public class StickyRandomDataSourceSelector extends RandomDataSourceSelector {
super(highAvailableDataSource);
}
@Override
public String getName() {
return DataSourceSelectorEnum.STICKY_RANDOM.getName();
}
@Override
public DataSource get() {
StickyDataSourceHolder holder = holders.get();