mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
Merge branch 'master' of https://github.com/AlibabaTech/druid
Conflicts: src/main/java/com/alibaba/druid/stat/DruidStatManagerFacade.java
This commit is contained in:
@@ -22,6 +22,7 @@ import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.ConcurrentModificationException;
|
||||
import java.util.Date;
|
||||
import java.util.HashMap;
|
||||
import java.util.Iterator;
|
||||
import java.util.LinkedHashMap;
|
||||
import java.util.List;
|
||||
@@ -32,6 +33,7 @@ import java.util.concurrent.TimeUnit;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.locks.Condition;
|
||||
|
||||
import javax.management.JMException;
|
||||
import javax.management.ObjectName;
|
||||
import javax.naming.NamingException;
|
||||
import javax.naming.Reference;
|
||||
@@ -61,9 +63,11 @@ 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;
|
||||
import com.alibaba.druid.util.JMXUtils;
|
||||
import com.alibaba.druid.util.JdbcUtils;
|
||||
|
||||
/**
|
||||
@@ -72,7 +76,7 @@ import com.alibaba.druid.util.JdbcUtils;
|
||||
*/
|
||||
public class DruidDataSource extends DruidAbstractDataSource implements DruidDataSourceMBean, ManagedDataSource, Referenceable, Closeable, Cloneable, ConnectionPoolDataSource {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSource.class);
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSource.class);
|
||||
|
||||
private static final long serialVersionUID = 1L;
|
||||
|
||||
@@ -418,7 +422,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
initedLatch.await();
|
||||
|
||||
initedTime = new Date();
|
||||
ObjectName objectName = DruidDataSourceStatManager.add(this, this.name);
|
||||
ObjectName objectName = DruidDataSourceStatManager.addDataSource(this, this.name);
|
||||
this.setObjectName(objectName);
|
||||
|
||||
if (connectError != null && poolingCount == 0) {
|
||||
@@ -774,7 +778,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
}
|
||||
}
|
||||
poolingCount = 0;
|
||||
DruidDataSourceStatManager.remove(this);
|
||||
DruidDataSourceStatManager.removeDataSource(this);
|
||||
|
||||
enable = false;
|
||||
notEmpty.signalAll();
|
||||
@@ -1514,4 +1518,154 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
|
||||
return x;
|
||||
}
|
||||
|
||||
public Map<String, Object> getStatDataForMBean() {
|
||||
try {
|
||||
Map<String, Object> map = new HashMap<String, Object>();
|
||||
|
||||
// 0 - 4
|
||||
map.put("Name", this.getName());
|
||||
map.put("URL", this.getUrl());
|
||||
map.put("CreateCount", this.getCreateCount());
|
||||
map.put("DestroyCount", this.getDestroyCount());
|
||||
map.put("ConnectCount", this.getConnectCount());
|
||||
|
||||
// 5 - 9
|
||||
map.put("CloseCount", this.getCloseCount());
|
||||
map.put("ActiveCount", this.getActivePeak());
|
||||
map.put("PoolingCount", this.getPoolingCount());
|
||||
map.put("LockQueueLength", this.getLockQueueLength());
|
||||
map.put("WaitThreadCount", this.getNotEmptyWaitThreadPeak());
|
||||
|
||||
// 10 - 14
|
||||
map.put("InitialSize", this.getInitialSize());
|
||||
map.put("MaxActive", this.getMaxActive());
|
||||
map.put("MinIdle", this.getMinIdle());
|
||||
map.put("PoolPreparedStatements", this.isPoolPreparedStatements());
|
||||
map.put("TestOnBorrow", this.isTestOnBorrow());
|
||||
|
||||
// 15 - 19
|
||||
map.put("TestOnReturn", this.isTestOnReturn());
|
||||
map.put("MinEvictableIdleTimeMillis", this.getMinEvictableIdleTimeMillis());
|
||||
map.put("ConnectErrorCount", this.getConnectErrorCount());
|
||||
map.put("CreateTimespanMillis", this.getCreateTimespanMillis());
|
||||
map.put("DbType", this.getDbType());
|
||||
|
||||
// 20 - 24
|
||||
map.put("ValidationQuery", this.getValidationQuery());
|
||||
map.put("ValidationQueryTimeout", this.getValidationQueryTimeout());
|
||||
map.put("DriverClassName", this.getDriverClassName());
|
||||
map.put("Username", this.getUsername());
|
||||
map.put("RemoveAbandonedCount", this.getRemoveAbandonedCount());
|
||||
|
||||
// 25 - 29
|
||||
map.put("NotEmptyWaitCount", this.getNotEmptyWaitCount());
|
||||
map.put("NotEmptyWaitNanos", this.getNotEmptyWaitNanos());
|
||||
map.put("ErrorCount", this.getErrorCount());
|
||||
map.put("ReusePreparedStatementCount", this.getCachedPreparedStatementHitCount());
|
||||
map.put("StartTransactionCount", this.getStartTransactionCount());
|
||||
|
||||
// 30 - 34
|
||||
map.put("CommitCount", this.getCommitCount());
|
||||
map.put("RollbackCount", this.getRollbackCount());
|
||||
map.put("LastError", JMXUtils.getErrorCompositeData(this.getLastError()));
|
||||
map.put("LastCreateError", JMXUtils.getErrorCompositeData(this.getLastCreateError()));
|
||||
map.put("PreparedStatementCacheDeleteCount", this.getCachedPreparedStatementDeleteCount());
|
||||
|
||||
// 35 - 39
|
||||
map.put("PreparedStatementCacheAccessCount", this.getCachedPreparedStatementAccessCount());
|
||||
map.put("PreparedStatementCacheMissCount", this.getCachedPreparedStatementMissCount());
|
||||
map.put("PreparedStatementCacheHitCount", this.getCachedPreparedStatementHitCount());
|
||||
map.put("PreparedStatementCacheCurrentCount", this.getCachedPreparedStatementCount());
|
||||
map.put("Version", this.getVersion());
|
||||
|
||||
// 40 -
|
||||
map.put("LastErrorTime", this.getLastErrorTime());
|
||||
map.put("LastCreateErrorTime", this.getLastCreateErrorTime());
|
||||
map.put("CreateErrorCount", this.getCreateErrorCount());
|
||||
map.put("DiscardCount", this.getDiscardCount());
|
||||
|
||||
return map;
|
||||
} catch (JMException ex) {
|
||||
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,144 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.util.ArrayList;
|
||||
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;
|
||||
|
||||
/**
|
||||
* JNDI数据源通过反射调用Druid相关对象获得相关统计数据, 并处理了WebApp与WebContainer两种数据源同时存在场景的数据合并
|
||||
*
|
||||
* @author septer<awnugdpygl@163.com>
|
||||
*/
|
||||
public class DruidDataSourceStatReflectStatStrategy implements DruidDataSourceStatStrategy {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSourceStatReflectStatStrategy.class);
|
||||
|
||||
private static Object clazzFromWebContainer_instacne = null;
|
||||
private static Object clazzFromCurClassLoader_instacne = null;
|
||||
|
||||
public DruidDataSourceStatReflectStatStrategy(){
|
||||
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 (clazzFromWebContainer != null) clazzFromWebContainer_instacne = clazzFromCurClassLoader.newInstance();
|
||||
if (clazzFromCurClassLoader != null && clazzFromCurClassLoader != clazzFromWebContainer) {
|
||||
clazzFromCurClassLoader_instacne = clazzFromCurClassLoader.newInstance();
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
LOG.error(e.getMessage(), e);
|
||||
}
|
||||
}
|
||||
|
||||
@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;
|
||||
}
|
||||
|
||||
private Object mergeStatData(String methodName, Integer methodParam) {
|
||||
Object result = null;
|
||||
if (clazzFromWebContainer_instacne != null) {
|
||||
/*ReflectStatStrategy strategy = new WebAppReflectStatStrategy();
|
||||
result = invokeMapResHandler(result, strategy.invoke(clazzFromWebContainer_instacne, methodName,
|
||||
methodParam));
|
||||
*/
|
||||
result = invokeMapResHandler(result, ReflectHelper.getInstance().invoke(clazzFromWebContainer_instacne,
|
||||
methodName, methodParam));
|
||||
}
|
||||
if (clazzFromCurClassLoader_instacne != null) {
|
||||
/*ReflectStatStrategy strategy = new WebContainerReflectStatStrategy();
|
||||
result = invokeMapResHandler(result, strategy.invoke(clazzFromWebContainer_instacne, methodName,
|
||||
methodParam));*/
|
||||
|
||||
result = invokeMapResHandler(result, ReflectHelper.getInstance().invoke(clazzFromCurClassLoader_instacne,
|
||||
methodName, methodParam));
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getSqlStatData(Integer id) {
|
||||
return (Map<String, Object>) mergeStatData("getSqlStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> getDataSourceStatData(Integer id) {
|
||||
return (Map<String, Object>) mergeStatData("getDataSourceStatData", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public Map<String, Object> returnJSONBasicStat() {
|
||||
return (Map<String, Object>) mergeStatData("returnJSONBasicStat", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getSqlStatDataList() {
|
||||
return (List<Map<String, Object>>) mergeStatData("getSqlStatDataList", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<String> getActiveConnectionStackTraceByDataSourceId(Integer id) {
|
||||
return (List<String>) mergeStatData("getActiveConnectionStackTraceByDataSourceId", id);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Object> getDataSourceStatList() {
|
||||
return (List<Object>) mergeStatData("getDataSourceStatList", null);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public List<Map<String, Object>> getPoolingConnectionInfoByDataSourceId(Integer id) {
|
||||
return (List<Map<String, Object>>) mergeStatData("getPoolingConnectionInfoByDataSourceId", id);
|
||||
}
|
||||
|
||||
public Object getDruidDataSourceById(Integer id) {
|
||||
return mergeStatData("getDruidDataSourceById", id);
|
||||
}
|
||||
|
||||
public Object getSqlStatById(Integer id) {
|
||||
return mergeStatData("getSqlStatById", id);
|
||||
}
|
||||
|
||||
public void resetAll() {
|
||||
mergeStatData("resetAll", null);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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 DruidDataSourceStatReflectStatStrategy();
|
||||
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,46 +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 com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
public class ReflectHelper {
|
||||
|
||||
private final static List<Object> list = new ArrayList<Object>();
|
||||
private final static HashMap<Object, HashMap<String, Method>> methodHM = new HashMap<Object, HashMap<String, Method>>();
|
||||
|
||||
private ReflectHelper(){
|
||||
};
|
||||
|
||||
private static ReflectHelper instance = new ReflectHelper();
|
||||
|
||||
public static ReflectHelper getInstance() {
|
||||
return instance;
|
||||
}
|
||||
|
||||
public Object invoke(Object obj, String methodName, Integer id) {
|
||||
Object target = null;
|
||||
int objIndex = list.indexOf(obj);
|
||||
if (objIndex != -1) {
|
||||
target = list.get(objIndex);
|
||||
} else {
|
||||
target = obj;
|
||||
}
|
||||
HashMap<String, Method> h = methodHM.get(target);
|
||||
boolean existsMethod = h.containsKey(methodName);
|
||||
Method method = null;
|
||||
if (existsMethod) {
|
||||
method = h.get(methodName);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
} else {
|
||||
method = ReflectionUtils.getObjectMethod(target, methodName, id);
|
||||
final HashMap<String, Method> hm = new HashMap<String, Method>();
|
||||
hm.put(methodName, method);
|
||||
methodHM.put(target, hm);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,26 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
public class WebAppReflectStatStrategy extends ReflectStatStrategy {
|
||||
|
||||
public Object invoke(Object target, String methodName, Integer id) {
|
||||
HashMap<String, Method> h = methodHM.get(target);
|
||||
boolean existsMethod = h.containsKey(methodName);
|
||||
Method method = null;
|
||||
if (existsMethod) {
|
||||
method = h.get(methodName);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
} else {
|
||||
method = ReflectionUtils.getObjectMethod(target, methodName, id);
|
||||
final HashMap<String, Method> hm = new HashMap<String, Method>();
|
||||
hm.put(methodName, method);
|
||||
methodHM.put(target, hm);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,25 +0,0 @@
|
||||
package com.alibaba.druid.stat;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.HashMap;
|
||||
|
||||
import com.alibaba.druid.util.ReflectionUtils;
|
||||
|
||||
public class WebContainerReflectStatStrategy extends ReflectStatStrategy {
|
||||
|
||||
public Object invoke(Object target, String methodName, Integer id) {
|
||||
HashMap<String, Method> h = methodHM.get(target);
|
||||
boolean existsMethod = h.containsKey(methodName);
|
||||
Method method = null;
|
||||
if (existsMethod) {
|
||||
method = h.get(methodName);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
} else {
|
||||
method = ReflectionUtils.getObjectMethod(target, methodName, id);
|
||||
final HashMap<String, Method> hm = new HashMap<String, Method>();
|
||||
hm.put(methodName, method);
|
||||
methodHM.put(target, hm);
|
||||
return ReflectionUtils.callObjectMethod(target, method, id);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,115 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.Executors;
|
||||
import java.util.concurrent.ScheduledExecutorService;
|
||||
import java.util.concurrent.ScheduledFuture;
|
||||
import java.util.concurrent.TimeUnit;
|
||||
|
||||
import com.alibaba.druid.pool.DruidDataSource;
|
||||
import com.alibaba.druid.stat.DruidDataSourceStatManager;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
import com.alibaba.druid.stat.service.dto.SqlInfo;
|
||||
import com.alibaba.druid.stat.service.impl.DruidStatMemoryStore;
|
||||
|
||||
public class DruidStatService {
|
||||
|
||||
private ScheduledExecutorService scheduler;
|
||||
|
||||
private ScheduledFuture<?> collectPlan;
|
||||
|
||||
private int collectPeriodSeconds = 60 * 5;
|
||||
|
||||
private DruidStatStore store;
|
||||
|
||||
public int getCollectPeriodSeconds() {
|
||||
return collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public void setCollectPeriodSeconds(int collectPeriodSeconds) {
|
||||
this.collectPeriodSeconds = collectPeriodSeconds;
|
||||
}
|
||||
|
||||
public synchronized void start() {
|
||||
if (store != null) {
|
||||
store = new DruidStatMemoryStore();
|
||||
}
|
||||
|
||||
scheduler = Executors.newSingleThreadScheduledExecutor();
|
||||
collectPlan = scheduler.scheduleAtFixedRate(new CollectTask(), collectPeriodSeconds, collectPeriodSeconds,
|
||||
TimeUnit.SECONDS);
|
||||
}
|
||||
|
||||
public synchronized void stop() {
|
||||
if (collectPlan == null) {
|
||||
collectPlan.cancel(true);
|
||||
}
|
||||
if (scheduler != null) {
|
||||
scheduler.shutdown();
|
||||
}
|
||||
}
|
||||
|
||||
public void collect() {
|
||||
List<DataSourceInfo> dataSourceStatList = new ArrayList<DataSourceInfo>();
|
||||
for (DruidDataSource dataSource : DruidDataSourceStatManager.getDruidDataSourceInstances()) {
|
||||
DataSourceInfo dataSourceStat = new DataSourceInfo();
|
||||
|
||||
dataSourceStat.setCollectTimeMillis(System.currentTimeMillis());
|
||||
|
||||
dataSourceStat.setId(dataSource.getID());
|
||||
dataSourceStat.setUrl(dataSource.getUrl());
|
||||
dataSourceStat.setDbType(dataSource.getDbType());
|
||||
|
||||
dataSourceStat.setActiveCount(dataSource.getActiveCount());
|
||||
dataSourceStat.setActivePeak(dataSource.getActivePeak());
|
||||
dataSourceStat.setCloseCount((int) dataSource.getCloseCount());
|
||||
dataSourceStat.setConnectCount((int) dataSource.getConnectCount());
|
||||
dataSourceStat.setConnectErrorCount((int) dataSource.getConnectErrorCount());
|
||||
dataSourceStat.setCreateCount((int) dataSource.getCreateCount());
|
||||
dataSourceStat.setCreateErrorCount((int) dataSource.getCreateErrorCount());
|
||||
dataSourceStat.setDestoryCount((int) dataSource.getDestroyCount());
|
||||
dataSourceStat.setExecuteCount((int) dataSource.getExecuteCount());
|
||||
dataSourceStat.setPoolingCount(dataSource.getPoolingCount());
|
||||
dataSourceStat.setConnectionHoldHistogram(dataSource.getDataSourceStat().getConnectionHistogramValues());
|
||||
dataSourceStat.setTransactionHistogram(dataSource.getTransactionHistogramValues());
|
||||
|
||||
Collection<JdbcSqlStat> sqlStats = dataSource.getDataSourceStat().getSqlStatMap().values();
|
||||
|
||||
Map<String, SqlInfo> sqlStatInfoMap = new HashMap<String, SqlInfo>(sqlStats.size());
|
||||
for (JdbcSqlStat sqlStat : dataSource.getDataSourceStat().getSqlStatMap().values()) {
|
||||
|
||||
if (sqlStat.getExecuteCount() == 0 && sqlStat.getRunningCount() == 0) {
|
||||
continue;
|
||||
}
|
||||
|
||||
SqlInfo sqlStatInfo = DruidStatServiceUtils.createSqlInfo(sqlStat);
|
||||
|
||||
SqlInfo oldsqlStatInfo = sqlStatInfoMap.get(sqlStatInfo.getSql());
|
||||
if (oldsqlStatInfo != null) {
|
||||
oldsqlStatInfo.merge(sqlStatInfo);
|
||||
} else {
|
||||
sqlStatInfoMap.put(sqlStatInfo.getSql(), sqlStatInfo);
|
||||
}
|
||||
}
|
||||
|
||||
dataSourceStat.setSqlList(new ArrayList<SqlInfo>(sqlStatInfoMap.values()));
|
||||
|
||||
dataSourceStatList.add(dataSourceStat);
|
||||
}
|
||||
|
||||
store.saveDataSource(dataSourceStatList);
|
||||
}
|
||||
|
||||
private final class CollectTask implements Runnable {
|
||||
|
||||
public void run() {
|
||||
collect();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,40 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import com.alibaba.druid.sql.visitor.ParameterizedOutputVisitorUtils;
|
||||
import com.alibaba.druid.stat.JdbcSqlStat;
|
||||
import com.alibaba.druid.stat.service.dto.SqlInfo;
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class DruidStatServiceUtils {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidStatServiceUtils.class);
|
||||
|
||||
public static SqlInfo createSqlInfo(JdbcSqlStat sqlStat) {
|
||||
SqlInfo info = new SqlInfo();
|
||||
|
||||
String sql = sqlStat.getSql();
|
||||
try {
|
||||
sql = ParameterizedOutputVisitorUtils.parameterize(sql, sqlStat.getDbType());
|
||||
} catch (Exception e) {
|
||||
LOG.error("merge sql error", e);
|
||||
}
|
||||
|
||||
info.setSql(sql);
|
||||
info.setExecuteCount((int) sqlStat.getExecuteCount());
|
||||
info.setRunningCount((int) sqlStat.getRunningCount());
|
||||
info.setConcurrentMax((int) sqlStat.getConcurrentMax());
|
||||
info.setErrorCount((int) sqlStat.getErrorCount());
|
||||
info.setInTransactionCount((int) sqlStat.getInTransactionCount());
|
||||
info.setFetchRowCount(sqlStat.getFetchRowCount());
|
||||
info.setUpdateCount(sqlStat.getUpdateCount());
|
||||
info.setResultSetHoldTimeMilis(sqlStat.getResultSetHoldTimeMilis());
|
||||
|
||||
info.setHisogram(sqlStat.getHistogram().toArray());
|
||||
info.setExecuteAndResultHoldTimeHistogram(sqlStat.getExecuteAndResultHoldTimeHistogram().toArray());
|
||||
info.setFetchRowCountHistogram(sqlStat.getFetchRowCountHistogram().toArray());
|
||||
info.setUpdateCountHistogram(sqlStat.getUpdateCountHistogram().toArray());
|
||||
|
||||
return info;
|
||||
}
|
||||
}
|
||||
@@ -1,10 +0,0 @@
|
||||
package com.alibaba.druid.stat.service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
|
||||
public interface DruidStatStore {
|
||||
|
||||
void saveDataSource(List<DataSourceInfo> dataSourceList);
|
||||
}
|
||||
@@ -1,185 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.dto;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class DataSourceInfo {
|
||||
|
||||
private long id;
|
||||
private String url;
|
||||
private String dbType;
|
||||
|
||||
private int activeCount;
|
||||
private int activePeak;
|
||||
|
||||
private int poolingCount;
|
||||
|
||||
private int connectCount;
|
||||
private int connectErrorCount;
|
||||
|
||||
private int createCount;
|
||||
private int createErrorCount;
|
||||
|
||||
private int executeCount;
|
||||
|
||||
private int closeCount;
|
||||
private int destoryCount;
|
||||
|
||||
private long collectTimeMillis;
|
||||
|
||||
private int[] transactionHistogram;
|
||||
|
||||
private int[] connectionHoldHistogram;
|
||||
|
||||
private List<SqlInfo> sqlList;
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void setDbType(String dbType) {
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public void setUrl(String url) {
|
||||
this.url = url;
|
||||
}
|
||||
|
||||
public List<SqlInfo> getSqlList() {
|
||||
return sqlList;
|
||||
}
|
||||
|
||||
public void setSqlList(List<SqlInfo> sqlList) {
|
||||
this.sqlList = sqlList;
|
||||
}
|
||||
|
||||
public long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public void setId(long id) {
|
||||
this.id = id;
|
||||
}
|
||||
|
||||
public int[] getTransactionHistogram() {
|
||||
return transactionHistogram;
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(long[] values) {
|
||||
this.transactionHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
transactionHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public void setTransactionHistogram(int[] values) {
|
||||
this.transactionHistogram = values;
|
||||
}
|
||||
|
||||
public int[] getConnectionHoldHistogram() {
|
||||
return connectionHoldHistogram;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(int[] values) {
|
||||
this.connectionHoldHistogram = values;
|
||||
}
|
||||
|
||||
public void setConnectionHoldHistogram(long[] values) {
|
||||
this.connectionHoldHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
connectionHoldHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int getActiveCount() {
|
||||
return activeCount;
|
||||
}
|
||||
|
||||
public void setActiveCount(int activeCount) {
|
||||
this.activeCount = activeCount;
|
||||
}
|
||||
|
||||
public int getActivePeak() {
|
||||
return activePeak;
|
||||
}
|
||||
|
||||
public void setActivePeak(int activePeak) {
|
||||
this.activePeak = activePeak;
|
||||
}
|
||||
|
||||
public int getPoolingCount() {
|
||||
return poolingCount;
|
||||
}
|
||||
|
||||
public void setPoolingCount(int poolingCount) {
|
||||
this.poolingCount = poolingCount;
|
||||
}
|
||||
|
||||
public int getConnectCount() {
|
||||
return connectCount;
|
||||
}
|
||||
|
||||
public void setConnectCount(int connectCount) {
|
||||
this.connectCount = connectCount;
|
||||
}
|
||||
|
||||
public int getConnectErrorCount() {
|
||||
return connectErrorCount;
|
||||
}
|
||||
|
||||
public void setConnectErrorCount(int connectErrorCount) {
|
||||
this.connectErrorCount = connectErrorCount;
|
||||
}
|
||||
|
||||
public int getCreateCount() {
|
||||
return createCount;
|
||||
}
|
||||
|
||||
public void setCreateCount(int createCount) {
|
||||
this.createCount = createCount;
|
||||
}
|
||||
|
||||
public int getCreateErrorCount() {
|
||||
return createErrorCount;
|
||||
}
|
||||
|
||||
public void setCreateErrorCount(int createErrorCount) {
|
||||
this.createErrorCount = createErrorCount;
|
||||
}
|
||||
|
||||
public int getExecuteCount() {
|
||||
return executeCount;
|
||||
}
|
||||
|
||||
public void setExecuteCount(int executeCount) {
|
||||
this.executeCount = executeCount;
|
||||
}
|
||||
|
||||
public int getCloseCount() {
|
||||
return closeCount;
|
||||
}
|
||||
|
||||
public void setCloseCount(int closeCount) {
|
||||
this.closeCount = closeCount;
|
||||
}
|
||||
|
||||
public int getDestoryCount() {
|
||||
return destoryCount;
|
||||
}
|
||||
|
||||
public void setDestoryCount(int destoryCount) {
|
||||
this.destoryCount = destoryCount;
|
||||
}
|
||||
|
||||
public long getCollectTimeMillis() {
|
||||
return collectTimeMillis;
|
||||
}
|
||||
|
||||
public void setCollectTimeMillis(long collectTimeMillis) {
|
||||
this.collectTimeMillis = collectTimeMillis;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1,175 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.dto;
|
||||
|
||||
public class SqlInfo {
|
||||
|
||||
private String sql;
|
||||
private int executeCount;
|
||||
private int runningCount;
|
||||
private int concurrentMax;
|
||||
private int errorCount;
|
||||
private int inTransactionCount;
|
||||
|
||||
private long fetchRowCount;
|
||||
private long updateCount;
|
||||
|
||||
private long resultSetHoldTimeMilis;
|
||||
|
||||
private int[] histogram;
|
||||
|
||||
private int[] executeAndResultHoldTimeHistogram;
|
||||
|
||||
private int[] updateCountHistogram;
|
||||
|
||||
private int[] fetchRowCountHistogram;
|
||||
|
||||
public SqlInfo(){
|
||||
|
||||
}
|
||||
|
||||
public void setExecuteAndResultHoldTimeHistogram(long[] values) {
|
||||
executeAndResultHoldTimeHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
executeAndResultHoldTimeHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getExecuteAndResultHoldTimeHistogram() {
|
||||
return executeAndResultHoldTimeHistogram;
|
||||
}
|
||||
|
||||
public void setFetchRowCountHistogram(long[] values) {
|
||||
fetchRowCountHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
fetchRowCountHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getFetchRowCountHistogram() {
|
||||
return fetchRowCountHistogram;
|
||||
}
|
||||
|
||||
public void setUpdateCountHistogram(long[] values) {
|
||||
updateCountHistogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
updateCountHistogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getUpdateCountHistogram() {
|
||||
return updateCountHistogram;
|
||||
}
|
||||
|
||||
public void setHisogram(long[] values) {
|
||||
histogram = new int[values.length];
|
||||
for (int i = 0; i < values.length; ++i) {
|
||||
histogram[i] = (int) values[i];
|
||||
}
|
||||
}
|
||||
|
||||
public int[] getHistogram() {
|
||||
return histogram;
|
||||
}
|
||||
|
||||
public String getSql() {
|
||||
return sql;
|
||||
}
|
||||
|
||||
public void setSql(String sql) {
|
||||
this.sql = sql;
|
||||
}
|
||||
|
||||
public int getExecuteCount() {
|
||||
return executeCount;
|
||||
}
|
||||
|
||||
public void setExecuteCount(int executeCount) {
|
||||
this.executeCount = executeCount;
|
||||
}
|
||||
|
||||
public int getRunningCount() {
|
||||
return runningCount;
|
||||
}
|
||||
|
||||
public void setRunningCount(int runningCount) {
|
||||
this.runningCount = runningCount;
|
||||
}
|
||||
|
||||
public int getConcurrentMax() {
|
||||
return concurrentMax;
|
||||
}
|
||||
|
||||
public void setConcurrentMax(int concurrentMax) {
|
||||
this.concurrentMax = concurrentMax;
|
||||
}
|
||||
|
||||
public int getErrorCount() {
|
||||
return errorCount;
|
||||
}
|
||||
|
||||
public void setErrorCount(int errorCount) {
|
||||
this.errorCount = errorCount;
|
||||
}
|
||||
|
||||
public int getInTransactionCount() {
|
||||
return inTransactionCount;
|
||||
}
|
||||
|
||||
public void setInTransactionCount(int inTransactionCount) {
|
||||
this.inTransactionCount = inTransactionCount;
|
||||
}
|
||||
|
||||
public long getFetchRowCount() {
|
||||
return fetchRowCount;
|
||||
}
|
||||
|
||||
public void setFetchRowCount(long fetchRowCount) {
|
||||
this.fetchRowCount = fetchRowCount;
|
||||
}
|
||||
|
||||
public long getUpdateCount() {
|
||||
return updateCount;
|
||||
}
|
||||
|
||||
public void setUpdateCount(long updateCount) {
|
||||
this.updateCount = updateCount;
|
||||
}
|
||||
|
||||
public long getResultSetHoldTimeMilis() {
|
||||
return resultSetHoldTimeMilis;
|
||||
}
|
||||
|
||||
public void setResultSetHoldTimeMilis(long resultSetHoldTimeMilis) {
|
||||
this.resultSetHoldTimeMilis = resultSetHoldTimeMilis;
|
||||
}
|
||||
|
||||
public void merge(SqlInfo o) {
|
||||
executeCount += o.executeCount;
|
||||
runningCount += o.runningCount;
|
||||
if (o.concurrentMax > concurrentMax) {
|
||||
concurrentMax = o.concurrentMax;
|
||||
}
|
||||
errorCount += o.errorCount;
|
||||
inTransactionCount += o.inTransactionCount;
|
||||
|
||||
fetchRowCount += o.fetchRowCount;
|
||||
updateCount += o.updateCount;
|
||||
|
||||
resultSetHoldTimeMilis += o.resultSetHoldTimeMilis;
|
||||
|
||||
for (int i = 0; i < histogram.length; ++i) {
|
||||
this.histogram[i] += o.histogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < executeAndResultHoldTimeHistogram.length; ++i) {
|
||||
this.executeAndResultHoldTimeHistogram[i] += o.executeAndResultHoldTimeHistogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < updateCountHistogram.length; ++i) {
|
||||
this.updateCountHistogram[i] += o.updateCountHistogram[i];
|
||||
}
|
||||
|
||||
for (int i = 0; i < fetchRowCountHistogram.length; ++i) {
|
||||
this.fetchRowCountHistogram[i] += o.fetchRowCountHistogram[i];
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,90 +0,0 @@
|
||||
package com.alibaba.druid.stat.service.impl;
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Collections;
|
||||
import java.util.List;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.concurrent.ConcurrentSkipListMap;
|
||||
|
||||
import com.alibaba.druid.stat.service.DruidStatStore;
|
||||
import com.alibaba.druid.stat.service.dto.DataSourceInfo;
|
||||
|
||||
public class DruidStatMemoryStore implements DruidStatStore {
|
||||
|
||||
private ConcurrentHashMap<Long, DataSourceData> dataSourceDataMap = new ConcurrentHashMap<Long, DataSourceData>();
|
||||
|
||||
@Override
|
||||
public void saveDataSource(List<DataSourceInfo> dataSourceList) {
|
||||
for (DataSourceInfo dataSourceInfo : dataSourceList) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
|
||||
if (data == null) {
|
||||
dataSourceDataMap.putIfAbsent(dataSourceInfo.getId(), new DataSourceData(dataSourceInfo.getId(),
|
||||
dataSourceInfo.getUrl(),
|
||||
dataSourceInfo.getDbType()));
|
||||
data = dataSourceDataMap.get(dataSourceInfo.getId());
|
||||
}
|
||||
|
||||
data.add(dataSourceInfo);
|
||||
}
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(long dataSourceId, Long startTimeMillis, Long endTimeMillis) {
|
||||
DataSourceData data = dataSourceDataMap.get(dataSourceId);
|
||||
|
||||
if (data == null) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
return data.scan(startTimeMillis, endTimeMillis);
|
||||
}
|
||||
|
||||
public static class DataSourceData {
|
||||
|
||||
private final Long id;
|
||||
private final String url;
|
||||
private final String dbType;
|
||||
|
||||
private ConcurrentSkipListMap<Long, DataSourceInfo> data = new ConcurrentSkipListMap<Long, DataSourceInfo>();
|
||||
|
||||
public DataSourceData(Long id, String url, String dbType){
|
||||
super();
|
||||
this.id = id;
|
||||
this.url = url;
|
||||
this.dbType = dbType;
|
||||
}
|
||||
|
||||
public Long getId() {
|
||||
return id;
|
||||
}
|
||||
|
||||
public String getUrl() {
|
||||
return url;
|
||||
}
|
||||
|
||||
public String getDbType() {
|
||||
return dbType;
|
||||
}
|
||||
|
||||
public void add(DataSourceInfo dataSourceInfo) {
|
||||
data.put(dataSourceInfo.getCollectTimeMillis(), dataSourceInfo);
|
||||
}
|
||||
|
||||
public Collection<DataSourceInfo> scan(Long startTimeMillis, Long endTimeMillis) {
|
||||
if (data.size() == 0) {
|
||||
return Collections.emptyList();
|
||||
}
|
||||
|
||||
if (startTimeMillis == null) {
|
||||
startTimeMillis = data.keySet().iterator().next();
|
||||
}
|
||||
|
||||
if (endTimeMillis == null) {
|
||||
endTimeMillis = data.descendingKeySet().iterator().next();
|
||||
}
|
||||
|
||||
return data.subMap(startTimeMillis, endTimeMillis).values();
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,119 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
|
||||
import javax.management.ObjectName;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
public class DruidDataSourceUtils {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(DruidDataSourceUtils.class);
|
||||
|
||||
public static String getUrl(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getUrl");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (String) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getUrl error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static ObjectName getObjectName(Object druidDatasource) {
|
||||
try {
|
||||
Method method = druidDatasource.getClass().getMethod("getObjectName");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (ObjectName) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getObjectName error", e);
|
||||
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 {
|
||||
Method method = druidDatasource.getClass().getMethod("getStatData");
|
||||
Object obj = method.invoke(druidDatasource);
|
||||
return (Map<String, Object>) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getStatData error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
import java.util.Map;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
|
||||
public class JdbcSqlStatUtils {
|
||||
private final static Log LOG = LogFactory.getLog(JdbcSqlStatUtils.class);
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
public static Map<String, Object> getData(Object jdbcSqlStat) {
|
||||
try {
|
||||
Method method = jdbcSqlStat.getClass().getMethod("getData");
|
||||
Object obj = method.invoke(jdbcSqlStat);
|
||||
return (Map<String, Object>) obj;
|
||||
} catch (Exception e) {
|
||||
LOG.error("getData error", e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -1,20 +0,0 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.sql.Connection;
|
||||
import java.sql.SQLException;
|
||||
|
||||
|
||||
public class MySqlUtils {
|
||||
public static com.mysql.jdbc.Connection unwrap(Connection conn) throws SQLException {
|
||||
if (conn instanceof com.mysql.jdbc.Connection) {
|
||||
return (com.mysql.jdbc.Connection) conn;
|
||||
}
|
||||
|
||||
return conn.unwrap(com.mysql.jdbc.Connection.class);
|
||||
}
|
||||
|
||||
public static void ping(Connection conn) throws SQLException {
|
||||
com.mysql.jdbc.Connection mysqlConn = unwrap(conn);
|
||||
mysqlConn.ping();
|
||||
}
|
||||
}
|
||||
@@ -1,151 +0,0 @@
|
||||
package com.alibaba.druid.util;
|
||||
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
|
||||
import com.alibaba.druid.support.logging.Log;
|
||||
import com.alibaba.druid.support.logging.LogFactory;
|
||||
|
||||
/**
|
||||
* @author sandzhang<sandzhangtoo@gmail.com>
|
||||
*/
|
||||
public class ReflectionUtils {
|
||||
|
||||
private final static Log LOG = LogFactory.getLog(ReflectionUtils.class);
|
||||
|
||||
public static Class<?> loadClass(ClassLoader classLoader, String className) {
|
||||
try {
|
||||
return classLoader.loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Class<?> getClassFromWebContainer(String className) {
|
||||
return loadClass(HttpServletRequest.class.getClassLoader(), className);
|
||||
}
|
||||
|
||||
public static Class<?> getClassFromCurrentClassLoader(String className) {
|
||||
Class<?> result = loadClass(Thread.currentThread().getContextClassLoader(), className);
|
||||
try {
|
||||
result = Thread.currentThread().getContextClassLoader().loadClass(className);
|
||||
} catch (ClassNotFoundException e) {
|
||||
LOG.error("can'r find class in current thread context classLoader ", e);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Class<?> getClassFromWebContainerOrCurrentClassLoader(String className) {
|
||||
Class<?> result = getClassFromWebContainer(className);
|
||||
if (result == null) {
|
||||
result = getClassFromCurrentClassLoader(className);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
public static Object callStaticMethod(Class<?> classObject, String methodName) {
|
||||
|
||||
try {
|
||||
Method m = classObject.getMethod(methodName);
|
||||
return m.invoke(classObject);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callStaticMethod fail:class=" + classObject.getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, String methodName) {
|
||||
try {
|
||||
Method m = obj.getClass().getMethod(methodName);
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Method getObjectMethod(Object obj, String methodName) {
|
||||
try {
|
||||
return obj.getClass().getMethod(methodName);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Method getObjectMethod(Object obj, String methodName, Class<?>... parameterTypes) {
|
||||
try {
|
||||
return obj.getClass().getMethod(methodName, parameterTypes);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, String methodName, Class<?>[] parameterTypes, Object[] parameters) {
|
||||
try {
|
||||
Method m = getObjectMethod(obj, methodName, parameterTypes);
|
||||
return m.invoke(obj, parameters);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, Method m, Object[] parameters) {
|
||||
try {
|
||||
return m.invoke(obj, parameters);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, Method m) {
|
||||
try {
|
||||
return m.invoke(obj);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, String methodName, Integer id) {
|
||||
try {
|
||||
Method m = obj.getClass().getMethod(methodName, Integer.class);
|
||||
return m.invoke(obj, id);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Method getObjectMethod(Object obj, String methodName, Integer id) {
|
||||
try {
|
||||
return obj.getClass().getMethod(methodName, Integer.class);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("getObjectMethod fail:class=" + obj.getClass().getName() + " method=" + methodName, e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public static Object callObjectMethod(Object obj, Method m, Integer id) {
|
||||
try {
|
||||
return m.invoke(obj, id);
|
||||
} catch (Exception e) {
|
||||
LOG.warn("callObjectMethod fail:class=" + obj.getClass().getName() + " method=" + m.getName(), e);
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
public void getTest() {
|
||||
System.out.println("dd");
|
||||
// return "test";
|
||||
}
|
||||
|
||||
public static void main(String args[]) {
|
||||
System.out.println(callObjectMethod(new ReflectionUtils(), "getTest"));
|
||||
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user