use of com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource in project LinkAgent by shulieTech.
the class DataSourceGetConnectionCutoffInterceptor method wrapAtomikosDataSourceBean.
private CutOffResult wrapAtomikosDataSourceBean(AtomikosDataSourceBean dataSourceBean) {
DataSourceMeta<AtomikosDataSourceBean> dataSourceMeta = new DataSourceMeta<AtomikosDataSourceBean>(getUrl(dataSourceBean), getUsername(dataSourceBean), dataSourceBean);
AtomikosDataSourceBeanWrapUtil.init(dataSourceMeta);
Connection connection = null;
/**
* 所有的流量均切换到此逻辑上,防止业务有连接缓存后无法进入
* 如果未找到配置情况下则当前流量为压测流量时返回null,非压测流量则执行业务连接池正常逻辑,此种情况可能由于数据源未配置的情况
* 如果获取连接出错时如果流量为压测流量则返回null,非压测流量则执行业务连接池正常逻辑
*/
if (AtomikosDataSourceBeanWrapUtil.pressureDataSources.containsKey(dataSourceMeta)) {
AtomikosDataSourceBeanMediaDataSource mediatorDataSource = AtomikosDataSourceBeanWrapUtil.pressureDataSources.get(dataSourceMeta);
if (mediatorDataSource != null) {
try {
connection = mediatorDataSource.getConnection();
} catch (SQLException e) {
throw new PressureMeasureError(e);
}
} else {
if (!Pradar.isClusterTest()) {
return CutOffResult.passed();
}
}
return CutOffResult.cutoff(connection);
} else {
if (!Pradar.isClusterTest()) {
return CutOffResult.passed();
}
return CutOffResult.cutoff(null);
}
}
use of com.pamirs.attach.plugin.atomikos.utils.AtomikosDataSourceBeanMediaDataSource in project LinkAgent by shulieTech.
the class DataSourceGetConnectionCutoffInterceptor method addListener.
private void addListener() {
EventRouter.router().addListener(new PradarEventListener() {
@Override
public EventResult onEvent(IEvent event) {
if (!(event instanceof ClusterTestSwitchOffEvent)) {
return EventResult.IGNORE;
}
// 关闭压测数据源
AtomikosDataSourceBeanWrapUtil.destroy();
AtomikosNonXADataSourceBeanWrapUtil.destroy();
return EventResult.success("tomcat-jdbc-plugin");
}
@Override
public int order() {
return 8;
}
}).addListener(new PradarEventListener() {
@Override
public EventResult onEvent(IEvent event) {
if (!(event instanceof ShadowDataSourceConfigModifyEvent)) {
return EventResult.IGNORE;
}
ShadowDataSourceConfigModifyEvent shadowDataSourceConfigModifyEvent = (ShadowDataSourceConfigModifyEvent) event;
Set<ShadowDatabaseConfig> target = shadowDataSourceConfigModifyEvent.getTarget();
if (null == target || target.size() == 0) {
return EventResult.IGNORE;
}
for (ShadowDatabaseConfig config : target) {
Iterator<Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource>> it = AtomikosDataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
while (it.hasNext()) {
Map.Entry<DataSourceMeta, AtomikosDataSourceBeanMediaDataSource> entry = it.next();
if (StringUtils.equalsIgnoreCase(DbUrlUtils.getKey(config.getUrl(), config.getUsername()), DbUrlUtils.getKey(entry.getKey().getUrl(), entry.getKey().getUsername()))) {
AtomikosDataSourceBeanMediaDataSource value = entry.getValue();
it.remove();
try {
value.close();
if (logger.isInfoEnabled()) {
logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
}
} catch (Throwable e) {
logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
}
break;
}
}
Iterator<Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource>> nonIt = AtomikosNonXADataSourceBeanWrapUtil.pressureDataSources.entrySet().iterator();
while (nonIt.hasNext()) {
Map.Entry<DataSourceMeta, AtomikosNonXADataSourceBeanMediaDataSource> entry = nonIt.next();
if (StringUtils.equalsIgnoreCase(config.getUrl(), entry.getKey().getUrl()) && StringUtils.equalsIgnoreCase(config.getUsername(), entry.getKey().getUsername())) {
AtomikosNonXADataSourceBeanMediaDataSource value = entry.getValue();
nonIt.remove();
try {
value.close();
if (logger.isInfoEnabled()) {
logger.info("module-atomikos-datasource: destroyed shadow table datasource success. url:{} ,username:{}", entry.getKey().getUrl(), entry.getKey().getUsername());
}
} catch (Throwable e) {
logger.error("module-atomikos-datasource: closed datasource err! target:{}, url:{} username:{}", entry.getKey().getDataSource().hashCode(), entry.getKey().getUrl(), entry.getKey().getUsername(), e);
}
break;
}
}
}
return EventResult.success("module-atomikos-datasource: destroyed shadow table datasource success.");
}
@Override
public int order() {
return 2;
}
});
}
Aggregations