mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
refactor DruidDataSourceStatFacade
This commit is contained in:
@@ -63,6 +63,7 @@ import com.alibaba.druid.proxy.jdbc.DataSourceProxyConfig;
|
||||
import com.alibaba.druid.proxy.jdbc.TransactionInfo;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.alibaba.druid.stat.JdbcDataSourceStat;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.IOUtils;
|
||||
@@ -1517,7 +1518,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return x;
|
||||
}
|
||||
|
||||
public Map<String, Object> getStatData() {
|
||||
public Map<String, Object> getStatDataForMBean() {
|
||||
try {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
@@ -1588,4 +1589,83 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
throw new IllegalStateException("getStatData error", ex);
|
||||
}
|
||||
}
|
||||
|
||||
public Map<String, Object> getStatData() {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
|
||||
dataMap.put("Identity", System.identityHashCode(this));
|
||||
dataMap.put("Name", this.getName());
|
||||
dataMap.put("DbType", this.getDbType());
|
||||
dataMap.put("DriverClassName", this.getDriverClassName());
|
||||
|
||||
dataMap.put("URL", this.getUrl());
|
||||
dataMap.put("UserName", this.getUsername());
|
||||
dataMap.put("FilterClassNames", this.getFilterClassNames());
|
||||
|
||||
dataMap.put("WaitThreadCount", this.getWaitThreadCount());
|
||||
dataMap.put("NotEmptyWaitCount", this.getNotEmptyWaitCount());
|
||||
dataMap.put("NotEmptyWaitMillis", this.getNotEmptyWaitMillis());
|
||||
|
||||
dataMap.put("PoolingCount", this.getPoolingCount());
|
||||
dataMap.put("PoolingPeak", this.getPoolingPeak());
|
||||
dataMap.put("PoolingPeakTime",
|
||||
this.getPoolingPeakTime() == null ? null : this.getPoolingPeakTime().toString());
|
||||
|
||||
dataMap.put("ActiveCount", this.getActiveCount());
|
||||
dataMap.put("ActivePeak", this.getActivePeak());
|
||||
dataMap.put("ActivePeakTime",
|
||||
this.getActivePeakTime() == null ? null : this.getActivePeakTime().toString());
|
||||
|
||||
dataMap.put("InitialSize", this.getInitialSize());
|
||||
dataMap.put("MinIdle", this.getMinIdle());
|
||||
dataMap.put("MaxActive", this.getMaxActive());
|
||||
|
||||
dataMap.put("QueryTimeout", this.getQueryTimeout());
|
||||
dataMap.put("TransactionQueryTimeout", this.getTransactionQueryTimeout());
|
||||
dataMap.put("LoginTimeout", this.getLoginTimeout());
|
||||
dataMap.put("ValidConnectionCheckerClassName", this.getValidConnectionCheckerClassName());
|
||||
dataMap.put("ExceptionSorterClassName", this.getExceptionSorterClassName());
|
||||
|
||||
dataMap.put("TestOnBorrow", this.isTestOnBorrow());
|
||||
dataMap.put("TestOnReturn", this.isTestOnReturn());
|
||||
dataMap.put("TestWhileIdle", this.isTestWhileIdle());
|
||||
|
||||
dataMap.put("DefaultAutoCommit", this.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultReadOnly", this.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultTransactionIsolation", this.getDefaultTransactionIsolation());
|
||||
|
||||
dataMap.put("LogicConnectCount", this.getConnectCount());
|
||||
dataMap.put("LogicCloseCount", this.getCloseCount());
|
||||
dataMap.put("LogicConnectErrorCount", this.getConnectErrorCount());
|
||||
|
||||
dataMap.put("PhysicalConnectCount", this.getCreateCount());
|
||||
dataMap.put("PhysicalCloseCount", this.getDestroyCount());
|
||||
dataMap.put("PhysicalConnectErrorCount", this.getCreateErrorCount());
|
||||
|
||||
dataMap.put("ExecuteCount", this.getExecuteCount());
|
||||
dataMap.put("ErrorCount", this.getErrorCount());
|
||||
dataMap.put("CommitCount", this.getCommitCount());
|
||||
dataMap.put("RollbackCount", this.getRollbackCount());
|
||||
|
||||
dataMap.put("PSCacheAccessCount", this.getCachedPreparedStatementAccessCount());
|
||||
dataMap.put("PSCacheHitCount", this.getCachedPreparedStatementHitCount());
|
||||
dataMap.put("PSCacheMissCount", this.getCachedPreparedStatementMissCount());
|
||||
|
||||
dataMap.put("StartTransactionCount", this.getStartTransactionCount());
|
||||
dataMap.put("TransactionHistogram", this.getTransactionHistogramValues());
|
||||
|
||||
dataMap.put("ConnectionHoldTimeHistogram",
|
||||
this.getDataSourceStat().getConnectionHoldHistogram().toArray());
|
||||
dataMap.put("RemoveAbandoned", this.isRemoveAbandoned());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlStat(int sqlId) {
|
||||
return this.getDataSourceStat().getSqlStat(sqlId);
|
||||
}
|
||||
|
||||
public Map<String, JdbcSqlStat> getSqlStatMap() {
|
||||
return this.getDataSourceStat().getSqlStatMap();
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,202 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import javax.management.JMException;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
|
||||
public class DruidDataSourceStatDefaultStrategy implements DruidDataSourceStatStrategy {
|
||||
|
||||
private Set<DruidDataSource> getDruidDataSourceInstances() {
|
||||
return DruidDataSourceStatManager.getDruidDataSourceInstances();
|
||||
}
|
||||
|
||||
public void resetDataSourceStat() {
|
||||
DruidDataSourceStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public void resetSqlStat() {
|
||||
JdbcStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
resetSqlStat();
|
||||
resetDataSourceStat();
|
||||
}
|
||||
|
||||
public JdbcSqlStat getSqlStatById(Integer id) {
|
||||
for (DruidDataSource ds : getDruidDataSourceInstances()) {
|
||||
JdbcSqlStat sqlStat = ds.getDataSourceStat().getSqlStat(id);
|
||||
if (sqlStat != null) return sqlStat;
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
}
|
||||
|
||||
public DruidDataSource getDruidDataSourceById(Integer identity) {
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (DruidDataSource datasource : getDruidDataSourceInstances()) {
|
||||
for (JdbcSqlStat sqlStat : datasource.getDataSourceStat().getSqlStatMap().values()) {
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(getSqlStatData(sqlStat));
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
JdbcSqlStat sqlStat = getSqlStatById(id);
|
||||
return sqlStat == null ? null : getSqlStatData(sqlStat);
|
||||
}
|
||||
|
||||
private Map<String, Object> getSqlStatData(JdbcSqlStat sqlStat) {
|
||||
try {
|
||||
return sqlStat.getData();
|
||||
} catch (JMException e) {
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
List<Object> datasourceList = new ArrayList<Object>();
|
||||
for (DruidDataSource dataSource : getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Version", VERSION.getVersionNumber());
|
||||
dataMap.put("Drivers", getDriversData());
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
private List<String> getDriversData() {
|
||||
List<String> drivers = new ArrayList<String>();
|
||||
for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
|
||||
Driver driver = e.nextElement();
|
||||
drivers.add(driver.getClass().getName());
|
||||
}
|
||||
return drivers;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
if (datasource == null) return null;
|
||||
return datasource.getPoolingConnectionInfo();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
DruidDataSource datasource = getDruidDataSourceById(id);
|
||||
if (datasource == null || !datasource.isRemoveAbandoned()) return null;
|
||||
|
||||
return datasource.getActiveConnectionStackTrace();
|
||||
}
|
||||
|
||||
private Map<String, Object> dataSourceToMapData(DruidDataSource dataSource) {
|
||||
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Identity", System.identityHashCode(dataSource));
|
||||
dataMap.put("Name", dataSource.getName());
|
||||
dataMap.put("DbType", dataSource.getDbType());
|
||||
dataMap.put("DriverClassName", dataSource.getDriverClassName());
|
||||
|
||||
dataMap.put("URL", dataSource.getUrl());
|
||||
dataMap.put("UserName", dataSource.getUsername());
|
||||
dataMap.put("FilterClassNames", dataSource.getFilterClassNames());
|
||||
|
||||
dataMap.put("WaitThreadCount", dataSource.getWaitThreadCount());
|
||||
dataMap.put("NotEmptyWaitCount", dataSource.getNotEmptyWaitCount());
|
||||
dataMap.put("NotEmptyWaitMillis", dataSource.getNotEmptyWaitMillis());
|
||||
|
||||
dataMap.put("PoolingCount", dataSource.getPoolingCount());
|
||||
dataMap.put("PoolingPeak", dataSource.getPoolingPeak());
|
||||
dataMap.put("PoolingPeakTime",
|
||||
dataSource.getPoolingPeakTime() == null ? null : dataSource.getPoolingPeakTime().toString());
|
||||
|
||||
dataMap.put("ActiveCount", dataSource.getActiveCount());
|
||||
dataMap.put("ActivePeak", dataSource.getActivePeak());
|
||||
dataMap.put("ActivePeakTime",
|
||||
dataSource.getActivePeakTime() == null ? null : dataSource.getActivePeakTime().toString());
|
||||
|
||||
dataMap.put("InitialSize", dataSource.getInitialSize());
|
||||
dataMap.put("MinIdle", dataSource.getMinIdle());
|
||||
dataMap.put("MaxActive", dataSource.getMaxActive());
|
||||
|
||||
dataMap.put("QueryTimeout", dataSource.getQueryTimeout());
|
||||
dataMap.put("TransactionQueryTimeout", dataSource.getTransactionQueryTimeout());
|
||||
dataMap.put("LoginTimeout", dataSource.getLoginTimeout());
|
||||
dataMap.put("ValidConnectionCheckerClassName", dataSource.getValidConnectionCheckerClassName());
|
||||
dataMap.put("ExceptionSorterClassName", dataSource.getExceptionSorterClassName());
|
||||
|
||||
dataMap.put("TestOnBorrow", dataSource.isTestOnBorrow());
|
||||
dataMap.put("TestOnReturn", dataSource.isTestOnReturn());
|
||||
dataMap.put("TestWhileIdle", dataSource.isTestWhileIdle());
|
||||
|
||||
dataMap.put("DefaultAutoCommit", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultReadOnly", dataSource.isDefaultAutoCommit());
|
||||
dataMap.put("DefaultTransactionIsolation", dataSource.getDefaultTransactionIsolation());
|
||||
|
||||
dataMap.put("LogicConnectCount", dataSource.getConnectCount());
|
||||
dataMap.put("LogicCloseCount", dataSource.getCloseCount());
|
||||
dataMap.put("LogicConnectErrorCount", dataSource.getConnectErrorCount());
|
||||
|
||||
dataMap.put("PhysicalConnectCount", dataSource.getCreateCount());
|
||||
dataMap.put("PhysicalCloseCount", dataSource.getDestroyCount());
|
||||
dataMap.put("PhysicalConnectErrorCount", dataSource.getCreateErrorCount());
|
||||
|
||||
dataMap.put("ExecuteCount", dataSource.getExecuteCount());
|
||||
dataMap.put("ErrorCount", dataSource.getErrorCount());
|
||||
dataMap.put("CommitCount", dataSource.getCommitCount());
|
||||
dataMap.put("RollbackCount", dataSource.getRollbackCount());
|
||||
|
||||
dataMap.put("PSCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount());
|
||||
dataMap.put("PSCacheHitCount", dataSource.getCachedPreparedStatementHitCount());
|
||||
dataMap.put("PSCacheMissCount", dataSource.getCachedPreparedStatementMissCount());
|
||||
|
||||
dataMap.put("StartTransactionCount", dataSource.getStartTransactionCount());
|
||||
dataMap.put("TransactionHistogram", dataSource.getTransactionHistogramValues());
|
||||
|
||||
dataMap.put("ConnectionHoldTimeHistogram",
|
||||
dataSource.getDataSourceStat().getConnectionHoldHistogram().toArray());
|
||||
dataMap.put("RemoveAbandoned", dataSource.isRemoveAbandoned());
|
||||
|
||||
return dataMap;
|
||||
}
|
||||
}
|
||||
@@ -1,178 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.ArrayList;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
import com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
public class DruidDataSourceStatJNDIStatStrategy implements DruidDataSourceStatStrategy {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSourceStatJNDIStatStrategy.class);
|
||||
|
||||
private final static List<Object> strategyList = new ArrayList<Object>();
|
||||
|
||||
private final static HashMap<Object, HashMap<String, Method>> classMethodHM = new HashMap<Object, HashMap<String, Method>>();
|
||||
|
||||
public DruidDataSourceStatJNDIStatStrategy(){
|
||||
init();
|
||||
}
|
||||
|
||||
private void init() {
|
||||
try {
|
||||
// cache the class
|
||||
String className = "com.alibaba.druid.stat.DruidDataSourceStatDefaultStrategy";
|
||||
Class<?> clazzFromWebContainer = ReflectionUtils.getClassFromWebContainer(className);
|
||||
Class<?> clazzFromCurClassLoader = ReflectionUtils.getClassFromCurrentClassLoader(className);
|
||||
if (clazzFromCurClassLoader != null) {
|
||||
Object clazzFromCurClassLoaderInstance = clazzFromCurClassLoader.newInstance();
|
||||
if (!strategyList.contains(clazzFromCurClassLoaderInstance)) {
|
||||
strategyList.add(clazzFromCurClassLoaderInstance);
|
||||
}
|
||||
}
|
||||
if (clazzFromWebContainer != null) {
|
||||
Object clazzFromWebContainerInstance = clazzFromWebContainer.newInstance();
|
||||
if (!strategyList.contains(clazzFromWebContainerInstance)) {
|
||||
if (clazzFromCurClassLoader != null && clazzFromWebContainer != clazzFromCurClassLoader) {
|
||||
strategyList.add(clazzFromWebContainerInstance);
|
||||
}
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
private Object invoke(String methodName, Integer id) {
|
||||
Object result = null;
|
||||
try {
|
||||
HashMap<String, Method> hm = null;
|
||||
Object invokeRes = null;
|
||||
Object obj = null;
|
||||
Method method = null;
|
||||
for (int i = 0; i < strategyList.size(); i++) {
|
||||
obj = strategyList.get(i);
|
||||
hm = classMethodHM.get(obj);
|
||||
if (hm != null) {
|
||||
if (hm.containsKey(methodName)) {
|
||||
method = hm.get(methodName);
|
||||
if (id == null) {
|
||||
invokeRes = ReflectionUtils.callObjectMethod(obj, method);
|
||||
} else {
|
||||
invokeRes = ReflectionUtils.callObjectMethod(obj, method, id);
|
||||
}
|
||||
result = invokeMapResHandler(result, invokeRes);
|
||||
} else {
|
||||
result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id);
|
||||
}
|
||||
} else {
|
||||
hm = new HashMap<String, Method>();
|
||||
result = cacheMethodAndRtnInvodeRes(hm, obj, methodName, id);
|
||||
}
|
||||
}
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
private Object cacheMethodAndRtnInvodeRes(HashMap<String, Method> methodHM, Object obj, String methodName,
|
||||
Integer id) throws Exception {
|
||||
Method method = null;
|
||||
Object invokeRes = null;
|
||||
if (id == null) {
|
||||
method = ReflectionUtils.getObjectMethod(obj, methodName);
|
||||
invokeRes = ReflectionUtils.callObjectMethod(obj, method);
|
||||
} else {
|
||||
method = ReflectionUtils.getObjectMethod(obj, methodName, id);
|
||||
invokeRes = ReflectionUtils.callObjectMethod(obj, method, id);
|
||||
}
|
||||
methodHM.put(methodName, method);
|
||||
classMethodHM.put(obj, methodHM);
|
||||
return invokeRes;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private Object invokeMapResHandler(Object finalRs, Object invokeRes) {
|
||||
Object res = null;
|
||||
if (finalRs == null) {
|
||||
if (invokeRes instanceof Map) {
|
||||
res = (Map<String, Object>) invokeRes;
|
||||
} else if (invokeRes instanceof List) {
|
||||
res = (List<Map<String, Object>>) invokeRes;
|
||||
} else {
|
||||
res = invokeRes;
|
||||
}
|
||||
} else {
|
||||
if (invokeRes instanceof Map) {
|
||||
Map<String, Object> finalRs_ = (Map<String, Object>) finalRs;
|
||||
Map<String, Object> invokeRes_ = (Map<String, Object>) invokeRes;
|
||||
if (invokeRes != null) finalRs_.putAll(invokeRes_);
|
||||
res = finalRs_;
|
||||
} else if (invokeRes instanceof List) {
|
||||
List<Map<String, Object>> finalRs_ = (List<Map<String, Object>>) finalRs;
|
||||
List<Map<String, Object>> invokeRes_ = (List<Map<String, Object>>) invokeRes;
|
||||
if (invokeRes != null) finalRs_.addAll(invokeRes_);
|
||||
res = finalRs_;
|
||||
} else {
|
||||
final List list = new ArrayList();
|
||||
list.add(finalRs);
|
||||
list.add(invokeRes);
|
||||
res = list;// merge the others class type,but in this
|
||||
}
|
||||
}
|
||||
return res;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return (Map<String, Object>) invoke("getSqlStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return (Map<String, Object>) invoke("getDataSourceStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return (Map<String, Object>) invoke("returnJSONBasicStat", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return (List<Map<String, Object>>) invoke("getSqlStatDataList", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return (List<String>) invoke("getActiveConnectionStackTraceByDataSourceId", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return (List<Object>) invoke("getDataSourceStatList", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return (List<Map<String, Object>>) invoke("getPoolingConnectionInfoByDataSourceId", id);
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer id) {
|
||||
return invoke("getDruidDataSourceById", id);
|
||||
}
|
||||
|
||||
public Object getSqlStatById(Integer id) {
|
||||
return invoke("getSqlStatById", id);
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
invoke("resetAll", null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -234,7 +234,7 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
|
||||
public CompositeDataSupport getCompositeData(Object dataSource) throws JMException {
|
||||
CompositeType rowType = getDruidDataSourceCompositeType();
|
||||
|
||||
Map<String, Object> map = DruidDataSourceUtils.getStatData(dataSource);
|
||||
Map<String, Object> map = DruidDataSourceUtils.getStatDataForMBean(dataSource);
|
||||
|
||||
return new CompositeDataSupport(rowType, map);
|
||||
}
|
||||
|
||||
@@ -1,27 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public interface DruidDataSourceStatStrategy {
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id);
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList();
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id);
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat();
|
||||
|
||||
public List<Object> getDataSourceStatList();
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id);
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id);
|
||||
|
||||
public void resetAll();
|
||||
|
||||
public Object getDruidDataSourceById(Integer id);
|
||||
|
||||
public Object getSqlStatById(Integer id);
|
||||
}
|
||||
@@ -1,54 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
public class DruidDataSourceStatStrategyContext {
|
||||
|
||||
private DruidDataSourceStatStrategy druidDataSourceStatStrategy;
|
||||
|
||||
public DruidDataSourceStatStrategyContext(DruidDataSourceStatStrategy druidDataSourceStatStrategy){
|
||||
this.druidDataSourceStatStrategy = druidDataSourceStatStrategy;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return druidDataSourceStatStrategy.getSqlStatData(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return druidDataSourceStatStrategy.getSqlStatDataList();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return druidDataSourceStatStrategy.getActiveConnectionStackTraceByDataSourceId(id);
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return druidDataSourceStatStrategy.returnJSONBasicStat();
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return druidDataSourceStatStrategy.getDataSourceStatList();
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return druidDataSourceStatStrategy.getDataSourceStatData(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return druidDataSourceStatStrategy.getPoolingConnectionInfoByDataSourceId(id);
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
druidDataSourceStatStrategy.resetAll();
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer id) {
|
||||
return druidDataSourceStatStrategy.getDruidDataSourceById(id);
|
||||
}
|
||||
|
||||
public Object getSqlStatById(Integer id) {
|
||||
return druidDataSourceStatStrategy.getSqlStatById(id);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,7 +1,17 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.sql.Driver;
|
||||
import java.sql.DriverManager;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Enumeration;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Set;
|
||||
|
||||
import com.alibaba.druid.VERSION;
|
||||
import com.alibaba.druid.util.DruidDataSourceUtils;
|
||||
import com.alibaba.druid.util.JdbcSqlStatUtils;
|
||||
|
||||
/**
|
||||
* 监控相关的对外数据暴露
|
||||
@@ -15,16 +25,7 @@ import java.util.Map;
|
||||
*/
|
||||
public class DruidStatManagerFacade {
|
||||
|
||||
private final static DruidStatManagerFacade instance = new DruidStatManagerFacade();
|
||||
|
||||
private static DruidDataSourceStatStrategyContext ctx = null;
|
||||
|
||||
private static DruidDataSourceStatStrategy strategy = null;
|
||||
|
||||
static {
|
||||
strategy = new DruidDataSourceStatJNDIStatStrategy();
|
||||
ctx = new DruidDataSourceStatStrategyContext(strategy);
|
||||
}
|
||||
private final static DruidStatManagerFacade instance = new DruidStatManagerFacade();
|
||||
|
||||
private DruidStatManagerFacade(){
|
||||
}
|
||||
@@ -33,43 +34,135 @@ public class DruidStatManagerFacade {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return ctx.getSqlStatData(id);
|
||||
private Set<Object> getDruidDataSourceInstances() {
|
||||
return DruidDataSourceStatManager.getInstances().keySet();
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return ctx.getSqlStatDataList();
|
||||
public void resetDataSourceStat() {
|
||||
DruidDataSourceStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return ctx.getActiveConnectionStackTraceByDataSourceId(id);
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return ctx.returnJSONBasicStat();
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return ctx.getDataSourceStatList();
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return ctx.getDataSourceStatData(id);
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return ctx.getPoolingConnectionInfoByDataSourceId(id);
|
||||
public void resetSqlStat() {
|
||||
JdbcStatManager.getInstance().reset();
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
ctx.resetAll();
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer id) {
|
||||
return ctx.getDruidDataSourceById(id);
|
||||
resetSqlStat();
|
||||
resetDataSourceStat();
|
||||
}
|
||||
|
||||
public Object getSqlStatById(Integer id) {
|
||||
return ctx.getSqlStatById(id);
|
||||
for (Object ds : getDruidDataSourceInstances()) {
|
||||
Object sqlStat = DruidDataSourceUtils.getSqlStat(ds, id);
|
||||
if (sqlStat != null) {
|
||||
return sqlStat;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object datasource = getDruidDataSourceById(id);
|
||||
return datasource == null ? null : dataSourceToMapData(datasource);
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer identity) {
|
||||
if (identity == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
for (Object datasource : getDruidDataSourceInstances()) {
|
||||
if (System.identityHashCode(datasource) == identity) {
|
||||
return datasource;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
List<Map<String, Object>> result = new ArrayList<Map<String, Object>>();
|
||||
for (Object datasource : getDruidDataSourceInstances()) {
|
||||
for (Object sqlStat : DruidDataSourceUtils.getSqlStatMap(datasource).values()) {
|
||||
|
||||
Map<String, Object> data = JdbcSqlStatUtils.getData(sqlStat);
|
||||
|
||||
long executeCount = (Long) data.get("ExecuteCount");
|
||||
long runningCount = (Long) data.get("RunningCount");
|
||||
|
||||
if (executeCount == 0 && runningCount == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
result.add(data);
|
||||
}
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
if (id == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
Object sqlStat = getSqlStatById(id);
|
||||
|
||||
if (sqlStat == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return JdbcSqlStatUtils.getData(sqlStat);
|
||||
}
|
||||
|
||||
public List<Object> getDataSourceStatList() {
|
||||
List<Object> datasourceList = new ArrayList<Object>();
|
||||
for (Object dataSource : getDruidDataSourceInstances()) {
|
||||
datasourceList.add(dataSourceToMapData(dataSource));
|
||||
}
|
||||
return datasourceList;
|
||||
}
|
||||
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
Map<String, Object> dataMap = new LinkedHashMap<String, Object>();
|
||||
dataMap.put("Version", VERSION.getVersionNumber());
|
||||
dataMap.put("Drivers", getDriversData());
|
||||
return dataMap;
|
||||
}
|
||||
|
||||
private List<String> getDriversData() {
|
||||
List<String> drivers = new ArrayList<String>();
|
||||
for (Enumeration<Driver> e = DriverManager.getDrivers(); e.hasMoreElements();) {
|
||||
Driver driver = e.nextElement();
|
||||
drivers.add(driver.getClass().getName());
|
||||
}
|
||||
return drivers;
|
||||
}
|
||||
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
Object datasource = getDruidDataSourceById(id);
|
||||
|
||||
if (datasource == null) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return DruidDataSourceUtils.getPoolingConnectionInfo(datasource);
|
||||
}
|
||||
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
Object datasource = getDruidDataSourceById(id);
|
||||
|
||||
if (datasource == null || DruidDataSourceUtils.isRemoveAbandoned(datasource)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return DruidDataSourceUtils.getActiveConnectionStackTrace(datasource);
|
||||
}
|
||||
|
||||
private Map<String, Object> dataSourceToMapData(Object dataSource) {
|
||||
|
||||
return DruidDataSourceUtils.getStatData(dataSource);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,7 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.management.ObjectName;
|
||||
@@ -22,7 +23,7 @@ public class DruidDataSourceUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static ObjectName getObjectName(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getObjectName");
|
||||
@@ -33,7 +34,41 @@ public class DruidDataSourceUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public static Object getSqlStat(Object druidDatasource, int sqlId) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getSqlStat", int.class);
|
||||
Object obj = method.invoke(druidDatasource, sqlId);
|
||||
return obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getSqlStat error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static boolean isRemoveAbandoned(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("isRemoveAbandoned");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (Boolean) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("isRemoveAbandoned error", e);
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> getStatDataForMBean(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getStatDataForMBean");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (Map<String, Object>) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getStatDataForMBean error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> getStatData(Object druidDatasource) {
|
||||
try {
|
||||
@@ -45,4 +80,40 @@ public class DruidDataSourceUtils {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings({ "rawtypes" })
|
||||
public static Map getSqlStatMap(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getSqlStatMap");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (Map) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getSqlStatMap error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<Map<String, Object>> getPoolingConnectionInfo(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getPoolingConnectionInfo");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (List<Map<String, Object>>) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getPoolingConnectionInfo error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static List<String> getActiveConnectionStackTrace(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getActiveConnectionStackTrace");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (List<String>) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getActiveConnectionStackTrace error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user