support jndi & osgi datasources

This commit is contained in:
wenshao
2012-08-12 09:29:25 +08:00
parent 7f237602f4
commit 6f314a1b82
3 changed files with 131 additions and 71 deletions
@@ -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;
@@ -64,6 +66,7 @@ import com.alibaba.druid.stat.JdbcDataSourceStat;
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 +75,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;
@@ -1514,4 +1517,75 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
return x;
}
public Map<String, Object> getStatData() {
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);
}
}
}
@@ -17,7 +17,6 @@ package com.alibaba.druid.stat;
import java.lang.management.ManagementFactory;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.IdentityHashMap;
import java.util.Map;
import java.util.Properties;
@@ -38,6 +37,7 @@ import javax.management.openmbean.TabularType;
import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidDataSourceUtils;
import com.alibaba.druid.util.JMXUtils;
@SuppressWarnings("rawtypes")
@@ -155,18 +155,18 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
}
}
public synchronized static void remove(DruidDataSource dataSource) {
public synchronized static void remove(Object dataSource) {
IdentityHashMap<Object, ObjectName> dataSources = getInstances();
synchronized (dataSources) {
ObjectName objectName = (ObjectName) dataSources.remove(dataSource);
if (objectName == null) {
objectName = dataSource.getObjectName();
objectName = DruidDataSourceUtils.getObjectName(dataSource);
}
if (objectName == null) {
LOG.error("unregister mbean failed. url " + dataSource.getUrl());
LOG.error("unregister mbean failed. url " + DruidDataSourceUtils.getUrl(dataSource));
return;
}
@@ -223,80 +223,18 @@ public class DruidDataSourceStatManager implements DruidDataSourceStatManagerMBe
TabularType tabularType = new TabularType("DruidDataSourceStat", "DruidDataSourceStat", rowType, indexNames);
TabularData data = new TabularDataSupport(tabularType);
final Set<DruidDataSource> dataSources = getDruidDataSourceInstances();
for (DruidDataSource dataSource : dataSources) {
final Set<Object> dataSources = getInstances().keySet();
for (Object dataSource : dataSources) {
data.put(getCompositeData(dataSource));
}
return data;
}
public CompositeDataSupport getCompositeData(DruidDataSource dataSource) throws JMException {
public CompositeDataSupport getCompositeData(Object dataSource) throws JMException {
CompositeType rowType = getDruidDataSourceCompositeType();
Map<String, Object> map = new HashMap<String, Object>();
// 0 - 4
map.put("Name", dataSource.getName());
map.put("URL", dataSource.getUrl());
map.put("CreateCount", dataSource.getCreateCount());
map.put("DestroyCount", dataSource.getDestroyCount());
map.put("ConnectCount", dataSource.getConnectCount());
// 5 - 9
map.put("CloseCount", dataSource.getCloseCount());
map.put("ActiveCount", dataSource.getActivePeak());
map.put("PoolingCount", dataSource.getPoolingCount());
map.put("LockQueueLength", dataSource.getLockQueueLength());
map.put("WaitThreadCount", dataSource.getNotEmptyWaitThreadPeak());
// 10 - 14
map.put("InitialSize", dataSource.getInitialSize());
map.put("MaxActive", dataSource.getMaxActive());
map.put("MinIdle", dataSource.getMinIdle());
map.put("PoolPreparedStatements", dataSource.isPoolPreparedStatements());
map.put("TestOnBorrow", dataSource.isTestOnBorrow());
// 15 - 19
map.put("TestOnReturn", dataSource.isTestOnReturn());
map.put("MinEvictableIdleTimeMillis", dataSource.getMinEvictableIdleTimeMillis());
map.put("ConnectErrorCount", dataSource.getConnectErrorCount());
map.put("CreateTimespanMillis", dataSource.getCreateTimespanMillis());
map.put("DbType", dataSource.getDbType());
// 20 - 24
map.put("ValidationQuery", dataSource.getValidationQuery());
map.put("ValidationQueryTimeout", dataSource.getValidationQueryTimeout());
map.put("DriverClassName", dataSource.getDriverClassName());
map.put("Username", dataSource.getUsername());
map.put("RemoveAbandonedCount", dataSource.getRemoveAbandonedCount());
// 25 - 29
map.put("NotEmptyWaitCount", dataSource.getNotEmptyWaitCount());
map.put("NotEmptyWaitNanos", dataSource.getNotEmptyWaitNanos());
map.put("ErrorCount", dataSource.getErrorCount());
map.put("ReusePreparedStatementCount", dataSource.getCachedPreparedStatementHitCount());
map.put("StartTransactionCount", dataSource.getStartTransactionCount());
// 30 - 34
map.put("CommitCount", dataSource.getCommitCount());
map.put("RollbackCount", dataSource.getRollbackCount());
map.put("LastError", JMXUtils.getErrorCompositeData(dataSource.getLastError()));
map.put("LastCreateError", JMXUtils.getErrorCompositeData(dataSource.getLastCreateError()));
map.put("PreparedStatementCacheDeleteCount", dataSource.getCachedPreparedStatementDeleteCount());
// 35 - 39
map.put("PreparedStatementCacheAccessCount", dataSource.getCachedPreparedStatementAccessCount());
map.put("PreparedStatementCacheMissCount", dataSource.getCachedPreparedStatementMissCount());
map.put("PreparedStatementCacheHitCount", dataSource.getCachedPreparedStatementHitCount());
map.put("PreparedStatementCacheCurrentCount", dataSource.getCachedPreparedStatementCount());
map.put("Version", dataSource.getVersion());
// 40 -
map.put("LastErrorTime", dataSource.getLastErrorTime());
map.put("LastCreateErrorTime", dataSource.getLastCreateErrorTime());
map.put("CreateErrorCount", dataSource.getCreateErrorCount());
map.put("DiscardCount", dataSource.getDiscardCount());
Map<String, Object> map = DruidDataSourceUtils.getStatData(dataSource);
return new CompositeDataSupport(rowType, map);
}
@@ -0,0 +1,48 @@
package com.alibaba.druid.util;
import java.lang.reflect.Method;
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;
}
}
@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;
}
}
}