support monitor

This commit is contained in:
wenshao
2013-08-14 17:24:40 +08:00
parent 5aa3d51128
commit a72da37909
41 changed files with 701 additions and 284 deletions
@@ -27,8 +27,8 @@ import java.util.concurrent.ConcurrentHashMap;
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.JdbcUtils;
import com.alibaba.druid.util.Utils;
public class FilterManager {
@@ -99,7 +99,7 @@ public class FilterManager {
continue;
}
Class<?> filterClass = IOUtils.loadClass(filterClassName);
Class<?> filterClass = Utils.loadClass(filterClassName);
if (filterClass == null) {
LOG.error("load filter error, filter not found : " + filterClassName);
@@ -126,7 +126,7 @@ public class FilterManager {
return;
}
Class<?> filterClass = IOUtils.loadClass(filterName);
Class<?> filterClass = Utils.loadClass(filterName);
if (filterClass == null) {
LOG.error("load filter error, filter not found : " + filterName);
return;
@@ -32,7 +32,7 @@ import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
import com.alibaba.druid.proxy.jdbc.PreparedStatementProxy;
import com.alibaba.druid.proxy.jdbc.ResultSetProxy;
import com.alibaba.druid.proxy.jdbc.StatementProxy;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
/**
* @author wenshao<szujobs@hotmail.com>
@@ -196,7 +196,7 @@ public class EncodingConvertFilter extends FilterAdapter {
if (object instanceof Reader) {
Reader reader = (Reader) object;
String text = IOUtils.read(reader);
String text = Utils.read(reader);
return new StringReader(decode(connection, text));
}
@@ -210,7 +210,7 @@ public class EncodingConvertFilter extends FilterAdapter {
if (object instanceof Reader) {
Reader reader = (Reader) object;
String text = IOUtils.read(reader);
String text = Utils.read(reader);
return new StringReader(decode(stmt.getConnectionProxy(), text));
}
@@ -382,7 +382,7 @@ public class EncodingConvertFilter extends FilterAdapter {
@Override
public void preparedStatement_setCharacterStream(FilterChain chain, PreparedStatementProxy statement,
int parameterIndex, java.io.Reader reader) throws SQLException {
String text = IOUtils.read(reader);
String text = Utils.read(reader);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setCharacterStream(chain, statement, parameterIndex, new StringReader(encodedText));
}
@@ -391,7 +391,7 @@ public class EncodingConvertFilter extends FilterAdapter {
public void preparedStatement_setCharacterStream(FilterChain chain, PreparedStatementProxy statement,
int parameterIndex, java.io.Reader reader, int length)
throws SQLException {
String text = IOUtils.read(reader, length);
String text = Utils.read(reader, length);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setCharacterStream(chain, statement, parameterIndex, new StringReader(encodedText),
encodedText.length());
@@ -401,7 +401,7 @@ public class EncodingConvertFilter extends FilterAdapter {
public void preparedStatement_setCharacterStream(FilterChain chain, PreparedStatementProxy statement,
int parameterIndex, java.io.Reader reader, long length)
throws SQLException {
String text = IOUtils.read(reader, (int) length);
String text = Utils.read(reader, (int) length);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setCharacterStream(chain, statement, parameterIndex, new StringReader(encodedText),
encodedText.length());
@@ -414,7 +414,7 @@ public class EncodingConvertFilter extends FilterAdapter {
String encodedText = encode(statement.getConnectionProxy(), (String) x);
super.preparedStatement_setObject(chain, statement, parameterIndex, encodedText);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setObject(chain, statement, parameterIndex, new StringReader(encodedText));
} else {
@@ -429,7 +429,7 @@ public class EncodingConvertFilter extends FilterAdapter {
String encodedText = encode(statement.getConnectionProxy(), (String) x);
super.preparedStatement_setObject(chain, statement, parameterIndex, encodedText, targetSqlType);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setObject(chain, statement, parameterIndex, new StringReader(encodedText),
targetSqlType);
@@ -446,7 +446,7 @@ public class EncodingConvertFilter extends FilterAdapter {
super.preparedStatement_setObject(chain, statement, parameterIndex, encodedText, targetSqlType,
scaleOrLength);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.preparedStatement_setObject(chain, statement, parameterIndex, new StringReader(encodedText),
targetSqlType, scaleOrLength);
@@ -471,7 +471,7 @@ public class EncodingConvertFilter extends FilterAdapter {
@Override
public java.io.Reader clob_getCharacterStream(FilterChain chain, ClobProxy wrapper) throws SQLException {
Reader reader = super.clob_getCharacterStream(chain, wrapper);
String text = IOUtils.read(reader);
String text = Utils.read(reader);
return new StringReader(decode(wrapper.getConnectionWrapper(), text));
}
@@ -479,7 +479,7 @@ public class EncodingConvertFilter extends FilterAdapter {
public Reader clob_getCharacterStream(FilterChain chain, ClobProxy wrapper, long pos, long length)
throws SQLException {
Reader reader = super.clob_getCharacterStream(chain, wrapper, pos, length);
String text = IOUtils.read(reader);
String text = Utils.read(reader);
return new StringReader(decode(wrapper.getConnectionWrapper(), text));
}
@@ -499,7 +499,7 @@ public class EncodingConvertFilter extends FilterAdapter {
@Override
public void callableStatement_setCharacterStream(FilterChain chain, CallableStatementProxy statement,
String parameterName, java.io.Reader reader) throws SQLException {
String text = IOUtils.read(reader);
String text = Utils.read(reader);
Reader encodeReader = new StringReader(encode(statement.getConnectionProxy(), text));
super.callableStatement_setCharacterStream(chain, statement, parameterName, encodeReader);
}
@@ -508,7 +508,7 @@ public class EncodingConvertFilter extends FilterAdapter {
public void callableStatement_setCharacterStream(FilterChain chain, CallableStatementProxy statement,
String parameterName, java.io.Reader reader, int length)
throws SQLException {
String text = IOUtils.read(reader, length);
String text = Utils.read(reader, length);
String encodeText = encode(statement.getConnectionProxy(), text);
Reader encodeReader = new StringReader(encodeText);
super.callableStatement_setCharacterStream(chain, statement, parameterName, encodeReader, encodeText.length());
@@ -518,7 +518,7 @@ public class EncodingConvertFilter extends FilterAdapter {
public void callableStatement_setCharacterStream(FilterChain chain, CallableStatementProxy statement,
String parameterName, java.io.Reader reader, long length)
throws SQLException {
String text = IOUtils.read(reader, (int) length);
String text = Utils.read(reader, (int) length);
String encodeText = encode(statement.getConnectionProxy(), text);
Reader encodeReader = new StringReader(encodeText);
super.callableStatement_setCharacterStream(chain, statement, parameterName, encodeReader,
@@ -538,7 +538,7 @@ public class EncodingConvertFilter extends FilterAdapter {
String encodedText = encode(statement.getConnectionProxy(), (String) x);
super.callableStatement_setObject(chain, statement, parameterName, encodedText);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.callableStatement_setObject(chain, statement, parameterName, new StringReader(encodedText));
} else {
@@ -553,7 +553,7 @@ public class EncodingConvertFilter extends FilterAdapter {
String encodedText = encode(statement.getConnectionProxy(), (String) x);
super.callableStatement_setObject(chain, statement, parameterName, encodedText, targetSqlType);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.callableStatement_setObject(chain, statement, parameterName, new StringReader(encodedText),
targetSqlType);
@@ -569,7 +569,7 @@ public class EncodingConvertFilter extends FilterAdapter {
String encodedText = encode(statement.getConnectionProxy(), (String) x);
super.callableStatement_setObject(chain, statement, parameterName, encodedText, targetSqlType, scale);
} else if (x instanceof Reader) {
String text = IOUtils.read((Reader) x);
String text = Utils.read((Reader) x);
String encodedText = encode(statement.getConnectionProxy(), text);
super.callableStatement_setObject(chain, statement, parameterName, new StringReader(encodedText),
targetSqlType, scale);
@@ -61,7 +61,7 @@ import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.DruidPasswordCallback;
import com.alibaba.druid.util.Histogram;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
@@ -563,7 +563,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public void setValidConnectionCheckerClassName(String validConnectionCheckerClass) throws Exception {
Class<?> clazz = IOUtils.loadClass(validConnectionCheckerClass);
Class<?> clazz = Utils.loadClass(validConnectionCheckerClass);
ValidConnectionChecker validConnectionChecker = null;
if (clazz != null) {
validConnectionChecker = (ValidConnectionChecker) clazz.newInstance();
@@ -798,7 +798,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
}
public void setPasswordCallbackClassName(String passwordCallbackClassName) throws Exception {
Class<?> clazz = IOUtils.loadClass(passwordCallbackClassName);
Class<?> clazz = Utils.loadClass(passwordCallbackClassName);
if (clazz != null) {
this.passwordCallback = (PasswordCallback) clazz.newInstance();
} else {
@@ -1130,7 +1130,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
return;
}
Class<?> clazz = IOUtils.loadClass(exceptionSorter);
Class<?> clazz = Utils.loadClass(exceptionSorter);
if (clazz == null) {
LOG.error("load exceptionSorter error : " + exceptionSorter);
} else {
@@ -1286,7 +1286,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
List<String> list = new ArrayList<String>();
for (DruidPooledConnection conn : this.getActiveConnections()) {
list.add(IOUtils.toString(conn.getConnectStackTrace()));
list.add(Utils.toString(conn.getConnectStackTrace()));
}
return list;
@@ -1515,7 +1515,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
if (createError != null) {
map.put("ConnectionConnectErrorLastTime", getLastCreateErrorTime());
map.put("ConnectionConnectErrorLastMessage", createError.getMessage());
map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(createError));
map.put("ConnectionConnectErrorLastStackTrace", Utils.getStackTrace(createError));
} else {
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
@@ -1561,7 +1561,7 @@ public abstract class DruidAbstractDataSource extends WrapperAdapter implements
map.put("ConnectionConnectCount", this.getConnectCount());
if (createError != null) {
map.put("ConnectionErrorLastMessage", createError.getMessage());
map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(createError));
map.put("ConnectionErrorLastStackTrace", Utils.getStackTrace(createError));
} else {
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
@@ -28,7 +28,7 @@ import javax.sql.StatementEventListener;
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.Utils;
import com.alibaba.druid.util.JdbcConstants;
/**
@@ -251,14 +251,14 @@ public final class DruidConnectionHolder {
buf.append("{ID:");
buf.append(System.identityHashCode(conn));
buf.append(", ConnectTime:\"");
buf.append(IOUtils.toString(new Date(this.connecttimeMillis)));
buf.append(Utils.toString(new Date(this.connecttimeMillis)));
buf.append("\", UseCount:");
buf.append(useCount);
if (lastActiveTimeMillis > 0) {
buf.append(", LastActiveTime:\"");
buf.append(IOUtils.toString(new Date(this.lastActiveTimeMillis)));
buf.append(Utils.toString(new Date(this.lastActiveTimeMillis)));
buf.append("\"");
}
@@ -15,7 +15,7 @@
*/
package com.alibaba.druid.pool;
import static com.alibaba.druid.util.IOUtils.getBoolean;
import static com.alibaba.druid.util.Utils.getBoolean;
import java.io.Closeable;
import java.security.AccessController;
@@ -82,7 +82,7 @@ import com.alibaba.druid.stat.JdbcSqlStat;
import com.alibaba.druid.stat.JdbcSqlStatValue;
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.Utils;
import com.alibaba.druid.util.JMXUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.alibaba.druid.util.JdbcUtils;
@@ -492,7 +492,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
init = true;
initStackTrace = IOUtils.toString(Thread.currentThread().getStackTrace());
initStackTrace = Utils.toString(Thread.currentThread().getStackTrace());
this.id = DruidDriver.createDataSourceId();
if (this.id > 1) {
@@ -1995,7 +1995,7 @@ public class DruidDataSource extends DruidAbstractDataSource implements DruidDat
buf.append("{");
buf.append("\n\tCreateTime:\"");
buf.append(IOUtils.toString(getCreatedTime()));
buf.append(Utils.toString(getCreatedTime()));
buf.append("\"");
buf.append(",\n\tActiveCount:");
@@ -30,8 +30,8 @@ import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
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.JdbcUtils;
import com.alibaba.druid.util.Utils;
public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
@@ -44,7 +44,7 @@ public class MySqlValidConnectionChecker extends ValidConnectionCheckerAdapter i
public MySqlValidConnectionChecker(){
try {
clazz = IOUtils.loadClass("com.mysql.jdbc.Connection");
clazz = Utils.loadClass("com.mysql.jdbc.Connection");
ping = clazz.getMethod("ping");
if (ping != null) {
usePingMethod = true;
@@ -29,8 +29,8 @@ import com.alibaba.druid.pool.ValidConnectionCheckerAdapter;
import com.alibaba.druid.proxy.jdbc.ConnectionProxy;
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.JdbcUtils;
import com.alibaba.druid.util.Utils;
public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter implements ValidConnectionChecker, Serializable {
@@ -44,7 +44,7 @@ public class OracleValidConnectionChecker extends ValidConnectionCheckerAdapter
public OracleValidConnectionChecker(){
try {
clazz = IOUtils.loadClass("oracle.jdbc.driver.OracleConnection");
clazz = Utils.loadClass("oracle.jdbc.driver.OracleConnection");
if (clazz != null) {
ping = clazz.getMethod("pingDatabase", new Class[] { Integer.TYPE });
} else {
@@ -43,7 +43,7 @@ import com.alibaba.druid.proxy.jdbc.DataSourceProxyImpl;
import com.alibaba.druid.stat.JdbcStatManager;
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.Utils;
import com.alibaba.druid.util.JMXUtils;
import com.alibaba.druid.util.JdbcUtils;
@@ -256,7 +256,7 @@ public class DruidDriver implements Driver, DruidDriverMBean {
}
public Driver createDriver(String className) throws SQLException {
Class<?> rawDriverClass = IOUtils.loadClass(className);
Class<?> rawDriverClass = Utils.loadClass(className);
if (rawDriverClass == null) {
throw new SQLException("jdbc-driver's class not found. '" + className + "'");
@@ -35,7 +35,7 @@ import com.alibaba.druid.filter.FilterChain;
import com.alibaba.druid.filter.FilterChainImpl;
import com.alibaba.druid.stat.JdbcDataSourceStat;
import com.alibaba.druid.stat.JdbcStatManager;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.util.JdbcUtils;
/**
@@ -216,7 +216,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
if (lastConnectionConnectError != null) {
map.put("ConnectionConnectErrorLastTime", stat.getConnectionStat().getErrorLastTime());
map.put("ConnectionConnectErrorLastMessage", lastConnectionConnectError.getMessage());
map.put("ConnectionConnectErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionConnectError));
map.put("ConnectionConnectErrorLastStackTrace", Utils.getStackTrace(lastConnectionConnectError));
} else {
map.put("ConnectionConnectErrorLastTime", null);
map.put("ConnectionConnectErrorLastMessage", null);
@@ -237,7 +237,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
map.put("StatementLastErrorTime", stat.getStatementStat().getLastErrorTime());
map.put("StatementLastErrorMessage", lastStatementError.getMessage());
map.put("StatementLastErrorStackTrace", IOUtils.getStackTrace(lastStatementError));
map.put("StatementLastErrorStackTrace", Utils.getStackTrace(lastStatementError));
} else {
map.put("StatementLastErrorTime", null);
map.put("StatementLastErrorMessage", null);
@@ -261,7 +261,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
Throwable lastResultSetError = stat.getResultSetStat().getLastError();
if (lastResultSetError != null) {
map.put("ResultSetLastErrorMessage", lastResultSetError.getMessage());
map.put("ResultSetLastErrorStackTrace", IOUtils.getStackTrace(lastResultSetError));
map.put("ResultSetLastErrorStackTrace", Utils.getStackTrace(lastResultSetError));
} else {
map.put("ResultSetLastErrorMessage", null);
map.put("ResultSetLastErrorStackTrace", null);
@@ -271,7 +271,7 @@ public class DataSourceProxyImpl implements DataSourceProxy, DataSourceProxyImpl
Throwable lastConnectionError = stat.getConnectionStat().getErrorLast();
if (lastConnectionError != null) {
map.put("ConnectionErrorLastMessage", lastConnectionError.getMessage());
map.put("ConnectionErrorLastStackTrace", IOUtils.getStackTrace(lastConnectionError));
map.put("ConnectionErrorLastStackTrace", Utils.getStackTrace(lastConnectionError));
} else {
map.put("ConnectionErrorLastMessage", null);
map.put("ConnectionErrorLastStackTrace", null);
@@ -34,7 +34,7 @@ import com.alibaba.druid.sql.visitor.SQLEvalVisitorUtils;
import com.alibaba.druid.support.http.stat.WebAppStatManager;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.alibaba.druid.util.DruidDataSourceUtils;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.util.JdbcSqlStatUtils;
import com.alibaba.druid.util.StringUtils;
@@ -344,7 +344,7 @@ public final class DruidStatManagerFacade {
dataMap.put("JavaVMName", System.getProperty("java.vm.name"));
dataMap.put("JavaVersion", System.getProperty("java.version"));
dataMap.put("JavaClassPath", System.getProperty("java.class.path"));
dataMap.put("StartTime", IOUtils.getStartTime());
dataMap.put("StartTime", Utils.getStartTime());
return dataMap;
}
@@ -32,10 +32,12 @@ import javax.management.openmbean.SimpleType;
import com.alibaba.druid.proxy.DruidDriver;
import com.alibaba.druid.proxy.jdbc.StatementExecuteType;
import com.alibaba.druid.util.JMXUtils;
import com.alibaba.druid.util.Utils;
public final class JdbcSqlStat implements JdbcSqlStatMBean {
private final String sql;
private long sqlHash;
private long id;
private String dataSource;
private long executeLastStartTime;
@@ -385,6 +387,7 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
val.setDbType(dbType);
val.setSql(sql);
val.setSqlHash(getSqlHash());
val.setId(id);
val.setExecuteLastStartTime(executeLastStartTime);
if (reset) {
@@ -586,6 +589,13 @@ public final class JdbcSqlStat implements JdbcSqlStatMBean {
public String getSql() {
return sql;
}
public long getSqlHash() {
if (sqlHash == 0) {
sqlHash = Utils.murmurhash2_64(sql);
}
return sqlHash;
}
public Date getExecuteLastStartTime() {
if (executeLastStartTime <= 0) {
@@ -21,61 +21,139 @@ import java.util.Map;
import javax.management.JMException;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.support.monitor.MField;
import com.alibaba.druid.util.Utils;
import com.alibaba.druid.util.JMXUtils;
public class JdbcSqlStatValue {
protected String sql;
protected long id;
protected String sql;
@MField
private long sqlHash;
@MField
protected String dataSource;
@MField
protected long executeLastStartTime;
@MField
protected long executeBatchSizeTotal;
@MField
protected int executeBatchSizeMax;
@MField
protected long executeSuccessCount;
@MField
protected long executeSpanNanoTotal;
@MField
protected long executeSpanNanoMax;
@MField
protected int runningCount;
@MField
protected int concurrentMax;
@MField
protected long resultSetHoldTimeNano;
@MField
protected long executeAndResultSetHoldTime;
@MField
protected String name;
@MField
protected String file;
@MField
protected String dbType;
@MField
protected long executeNanoSpanMaxOccurTime;
@MField
protected long executeErrorCount;
protected Throwable executeErrorLast;
@MField
protected String executeErrorLastMessage;
@MField
protected String executeErrorLastClass;
@MField
protected String executeErrorLastStackTrace;
@MField
protected long executeErrorLastTime;
@MField
protected long updateCount;
@MField
protected long updateCountMax;
@MField
protected long fetchRowCount;
@MField
protected long fetchRowCountMax;
@MField
protected long inTransactionCount;
@MField
protected String lastSlowParameters;
@MField
protected long clobOpenCount;
@MField
protected long blobOpenCount;
@MField
protected long readStringLength;
@MField
protected long readBytesLength;
@MField
protected long inputStreamOpenCount;
@MField
protected long readerOpenCount;
@MField(name = "h1")
protected long histogram_0_1;
@MField(name = "h10")
protected long histogram_1_10;
@MField(name = "h100")
protected int histogram_10_100;
@MField(name = "h1000")
protected int histogram_100_1000;
@MField(name = "h10000")
protected int histogram_1000_10000;
@MField(name = "h100000")
protected int histogram_10000_100000;
@MField(name = "h1000000")
protected int histogram_100000_1000000;
@MField(name = "hmore")
protected int histogram_1000000_more;
public long[] getExecuteHistogram() {
@@ -90,13 +168,28 @@ public class JdbcSqlStatValue {
};
}
@MField(name = "eh1")
protected long executeAndResultHoldTime_0_1;
@MField(name = "eh10")
protected long executeAndResultHoldTime_1_10;
@MField(name = "eh100")
protected int executeAndResultHoldTime_10_100;
@MField(name = "eh1000")
protected int executeAndResultHoldTime_100_1000;
@MField(name = "eh10000")
protected int executeAndResultHoldTime_1000_10000;
@MField(name = "eh100000")
protected int executeAndResultHoldTime_10000_100000;
@MField(name = "eh1000000")
protected int executeAndResultHoldTime_100000_1000000;
@MField(name = "ehmore")
protected int executeAndResultHoldTime_1000000_more;
public long[] getExecuteAndResultHoldHistogram() {
@@ -111,11 +204,22 @@ public class JdbcSqlStatValue {
};
}
@MField(name = "f1")
protected long fetchRowCount_0_1;
@MField(name = "f10")
protected long fetchRowCount_1_10;
@MField(name = "f100")
protected long fetchRowCount_10_100;
@MField(name = "f1000")
protected int fetchRowCount_100_1000;
@MField(name = "f10000")
protected int fetchRowCount_1000_10000;
@MField(name = "fmore")
protected int fetchRowCount_10000_more;
public long[] getFetchRowHistogram() {
@@ -128,11 +232,22 @@ public class JdbcSqlStatValue {
};
}
@MField(name = "u1")
protected long updateCount_0_1;
@MField(name = "u10")
protected long updateCount_1_10;
@MField(name = "u100")
protected long updateCount_10_100;
@MField(name = "u1000")
protected int updateCount_100_1000;
@MField(name = "u10000")
protected int updateCount_1000_10000;
@MField(name = "umore")
protected int updateCount_10000_more;
public long[] getUpdateHistogram() {
@@ -165,6 +280,14 @@ public class JdbcSqlStatValue {
this.sql = sql;
}
public long getSqlHash() {
return sqlHash;
}
public void setSqlHash(long sqlHash) {
this.sqlHash = sqlHash;
}
public long getId() {
return id;
}
@@ -184,7 +307,7 @@ public class JdbcSqlStatValue {
public long getExecuteLastStartTimeMillis() {
return executeLastStartTime;
}
public Date getExecuteLastStartTime() {
if (executeLastStartTime <= 0) {
return null;
@@ -296,7 +419,7 @@ public class JdbcSqlStatValue {
public long getExecuteNanoSpanMaxOccurTimeMillis() {
return executeNanoSpanMaxOccurTime;
}
public Date getExecuteNanoSpanMaxOccurTime() {
if (executeNanoSpanMaxOccurTime <= 0) {
return null;
@@ -329,6 +452,12 @@ public class JdbcSqlStatValue {
public void setExecuteErrorLast(Throwable executeErrorLast) {
this.executeErrorLast = executeErrorLast;
if (executeErrorLast != null) {
this.executeErrorLastMessage = executeErrorLast.getMessage();
this.executeErrorLastClass = executeErrorLast.getClass().getName();
this.executeErrorLastStackTrace = Utils.toString(executeErrorLast.getStackTrace());
}
}
public long getExecuteErrorLastTimeMillis() {
@@ -448,7 +577,7 @@ public class JdbcSqlStatValue {
histogram_1000000_more //
};
}
public long[] getFetchRowCountHistogramValues() {
return new long[] {
//
@@ -472,7 +601,7 @@ public class JdbcSqlStatValue {
updateCount_10000_more //
};
}
public long[] getExecuteAndResultHoldTimeHistogramValues() {
return new long[] {
//
@@ -524,12 +653,11 @@ public class JdbcSqlStatValue {
map.put("Name", getName()); // 16
map.put("File", getFile()); // 17
Throwable lastError = this.executeErrorLast;
if (lastError != null) {
map.put("LastErrorMessage", lastError.getMessage()); // 18
map.put("LastErrorClass", lastError.getClass().getName()); // 19
if (executeErrorLastMessage != null) {
map.put("LastErrorMessage", executeErrorLastMessage); // 18
map.put("LastErrorClass", executeErrorLastClass); // 19
map.put("LastErrorStackTrace", IOUtils.getStackTrace(lastError)); // 20
map.put("LastErrorStackTrace", executeErrorLastStackTrace); // 20
map.put("LastErrorTime", new Date(executeErrorLastTime)); // 21
} else {
map.put("LastErrorMessage", null);
@@ -562,4 +690,69 @@ public class JdbcSqlStatValue {
return map;
}
public long getHistogram_0_1() {
return histogram_0_1;
}
public void setHistogram_0_1(long histogram_0_1) {
this.histogram_0_1 = histogram_0_1;
}
public long getHistogram_1_10() {
return histogram_1_10;
}
public void setHistogram_1_10(long histogram_1_10) {
this.histogram_1_10 = histogram_1_10;
}
public int getHistogram_10_100() {
return histogram_10_100;
}
public void setHistogram_10_100(int histogram_10_100) {
this.histogram_10_100 = histogram_10_100;
}
public int getHistogram_100_1000() {
return histogram_100_1000;
}
public void setHistogram_100_1000(int histogram_100_1000) {
this.histogram_100_1000 = histogram_100_1000;
}
public int getHistogram_1000_10000() {
return histogram_1000_10000;
}
public void setHistogram_1000_10000(int histogram_1000_10000) {
this.histogram_1000_10000 = histogram_1000_10000;
}
public int getHistogram_10000_100000() {
return histogram_10000_100000;
}
public void setHistogram_10000_100000(int histogram_10000_100000) {
this.histogram_10000_100000 = histogram_10000_100000;
}
public int getHistogram_100000_1000000() {
return histogram_100000_1000000;
}
public void setHistogram_100000_1000000(int histogram_100000_1000000) {
this.histogram_100000_1000000 = histogram_100000_1000000;
}
public int getHistogram_1000000_more() {
return histogram_1000000_more;
}
public void setHistogram_1000000_more(int histogram_1000000_more) {
this.histogram_1000000_more = histogram_1000000_more;
}
}
@@ -37,7 +37,7 @@ import com.alibaba.druid.support.http.util.IPAddress;
import com.alibaba.druid.support.http.util.IPRange;
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.Utils;
import com.alibaba.druid.util.StringUtils;
/**
@@ -89,7 +89,7 @@ public class StatViewServlet extends HttpServlet {
initAuthEnv();
try {
templatePage = IOUtils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH);
templatePage = Utils.readFromResource(TEMPLATE_PAGE_RESOURCE_PATH);
} catch (IOException e) {
throw new ServletException("error read templatePage:" + TEMPLATE_PAGE_RESOURCE_PATH, e);
@@ -392,7 +392,7 @@ public class StatViewServlet extends HttpServlet {
private void returnResourceFile(String fileName, String uri, HttpServletResponse response) throws ServletException,
IOException {
if (fileName.endsWith(".jpg")) {
byte[] bytes = IOUtils.readByteArrayFromResource(RESOURCE_PATH + fileName);
byte[] bytes = Utils.readByteArrayFromResource(RESOURCE_PATH + fileName);
if (bytes != null) {
response.getOutputStream().write(bytes);
}
@@ -400,7 +400,7 @@ public class StatViewServlet extends HttpServlet {
return;
}
String text = IOUtils.readFromResource(RESOURCE_PATH + fileName);
String text = Utils.readFromResource(RESOURCE_PATH + fileName);
if (text == null) {
response.sendRedirect(uri + "/index.html");
return;
@@ -60,8 +60,8 @@ public class WebURIStatValue {
protected long histogram_0_1;
protected long histogram_1_10;
protected long histogram_10_100;
protected long histogram_100_1000;
protected long histogram_10_100;
protected long histogram_100_1000;
protected int histogram_1000_10000;
protected int histogram_10000_100000;
protected int histogram_100000_1000000;
@@ -23,7 +23,7 @@ import java.util.Map;
import javax.management.openmbean.CompositeData;
import javax.management.openmbean.TabularData;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
public class JSONWriter {
@@ -152,7 +152,7 @@ public class JSONWriter {
write(",\"Message\":");
writeString(error.getMessage());
write(",\"StackTrace\":");
writeString(IOUtils.getStackTrace(error));
writeString(Utils.getStackTrace(error));
write('}');
}
@@ -0,0 +1,27 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.support.monitor;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Retention(RetentionPolicy.RUNTIME)
@Target({ ElementType.METHOD, ElementType.FIELD, ElementType.PARAMETER })
public @interface MField {
String name() default "";
}
@@ -34,9 +34,9 @@ import com.alibaba.druid.support.monitor.dao.MonitorDao;
import com.alibaba.druid.support.spring.stat.SpringMethodStatValue;
import com.alibaba.druid.support.spring.stat.SpringStat;
import com.alibaba.druid.support.spring.stat.SpringStatManager;
import com.alibaba.druid.util.IOUtils;
import com.alibaba.druid.util.Utils;
public class MonitorService {
public class MonitorClient {
private final static long DEFAULT_TIME_BETWEEN_COLLECT = 60 * 5;
@@ -111,8 +111,8 @@ public class MonitorService {
private MonitorContext createContext() {
MonitorContext ctx = new MonitorContext();
ctx.setCollectTime(new Date());
ctx.setPID(IOUtils.getPID());
ctx.setCollectTime(IOUtils.getStartTime());
ctx.setPID(Utils.getPID());
ctx.setCollectTime(Utils.getStartTime());
return ctx;
}
@@ -15,19 +15,152 @@
*/
package com.alibaba.druid.support.monitor.dao;
import java.lang.reflect.Field;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.sql.Types;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.sql.DataSource;
import com.alibaba.druid.DruidRuntimeException;
import com.alibaba.druid.pool.DruidDataSourceStatValue;
import com.alibaba.druid.stat.JdbcSqlStatValue;
import com.alibaba.druid.support.http.stat.WebAppStatValue;
import com.alibaba.druid.support.http.stat.WebURIStatValue;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.support.monitor.MField;
import com.alibaba.druid.support.monitor.MonitorContext;
import com.alibaba.druid.support.spring.stat.SpringMethodStatValue;
import com.alibaba.druid.util.JdbcUtils;
import com.alibaba.druid.util.StringUtils;
import com.alibaba.druid.util.Utils;
public class MonitorDaoJdbcImpl implements MonitorDao {
private final static Log LOG = LogFactory.getLog(MonitorDaoJdbcImpl.class);
private DataSource dataSource;
private BeanInfo sqlStatBeanInfo = new BeanInfo(JdbcSqlStatValue.class);
public MonitorDaoJdbcImpl(){
}
public DataSource getDataSource() {
return dataSource;
}
public void setDataSource(DataSource dataSource) {
this.dataSource = dataSource;
}
@Override
public void saveSql(MonitorContext ctx, List<DruidDataSourceStatValue> dataSourceList) {
for (DruidDataSourceStatValue dataSourceStatValue : dataSourceList) {
List<JdbcSqlStatValue> sqlList = dataSourceStatValue.getSqlList();
String sql = buildInsertSql(sqlStatBeanInfo);
Connection conn = null;
PreparedStatement stmt = null;
try {
conn = dataSource.getConnection();
stmt = conn.prepareStatement(sql);
for (JdbcSqlStatValue statValue : sqlList) {
setParameterForSqlStat(sqlStatBeanInfo, stmt, statValue);
stmt.addBatch();
}
stmt.executeBatch();
stmt.close();
} catch (SQLException ex) {
LOG.error("save sql error", ex);
} finally {
JdbcUtils.close(stmt);
JdbcUtils.close(conn);
}
}
}
protected void setParameterForSqlStat(BeanInfo beanInfo, PreparedStatement stmt, JdbcSqlStatValue sqlStat)
throws SQLException {
int paramIndex = 1;
try {
List<FieldInfo> fields = beanInfo.getFields();
for (int i = 0; i < fields.size(); ++i) {
FieldInfo field = fields.get(i);
Class<?> fieldType = field.getFieldType();
Object value = field.getField().get(sqlStat);
if (fieldType.equals(int.class) || fieldType.equals(Integer.class)) {
setParam(stmt, paramIndex, (Integer) value);
} else if (fieldType.equals(long.class) || fieldType.equals(Long.class)) {
setParam(stmt, paramIndex, (Long) value);
} else if (fieldType.equals(String.class)) {
setParam(stmt, paramIndex, (String) value);
} else if (fieldType.equals(Date.class)) {
setParam(stmt, paramIndex, (Date) value);
} else {
throw new UnsupportedOperationException();
}
paramIndex++;
}
} catch (RuntimeException ex) {
throw ex;
} catch (Exception ex) {
throw new DruidRuntimeException("setParam error", ex);
}
}
public String buildInsertSql(BeanInfo beanInfo) {
String sql = beanInfo.insertSql;
if (sql != null) {
return sql;
}
StringBuilder buf = new StringBuilder();
buf.append("INSERT INTO ") //
.append(getTableName(beanInfo));
buf.append(" (");
List<FieldInfo> fields = beanInfo.getFields();
for (int i = 0; i < fields.size(); ++i) {
FieldInfo field = fields.get(i);
if (i != 0) {
buf.append(", ");
}
buf.append(field.getColumnName());
}
buf.append(")\nVALUES (");
for (int i = 0; i < fields.size(); ++i) {
if (i != 0) {
buf.append(", ?");
} else {
buf.append("?");
}
}
buf.append(")");
sql = buf.toString();
beanInfo.setInsertSql(sql);
return sql;
}
protected String getTableName(BeanInfo beanInfo) {
return "druid_sql";
}
protected long getSqlHash(String sql) {
return Utils.murmurhash2_64(sql);
}
@Override
@@ -42,7 +175,104 @@ public class MonitorDaoJdbcImpl implements MonitorDao {
@Override
public void saveWebApp(MonitorContext ctx, List<WebAppStatValue> uriList) {
}
static void setParam(PreparedStatement stmt, int paramIndex, String value) throws SQLException {
if (value == null) {
stmt.setNull(paramIndex, Types.VARCHAR);
} else {
stmt.setString(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Long value) throws SQLException {
if (value == null || value == 0) {
stmt.setNull(paramIndex, Types.BIGINT);
} else {
stmt.setLong(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Integer value) throws SQLException {
if (value == null || value == 0) {
stmt.setNull(paramIndex, Types.INTEGER);
} else {
stmt.setInt(paramIndex, value);
}
}
static void setParam(PreparedStatement stmt, int paramIndex, Date value) throws SQLException {
if (value == null) {
stmt.setNull(paramIndex, Types.TIMESTAMP);
} else {
stmt.setTimestamp(paramIndex, new Timestamp(value.getTime()));
}
}
public static class BeanInfo {
private final Class<?> clazz;
private final List<FieldInfo> fields = new ArrayList<FieldInfo>();
private String insertSql;
public BeanInfo(Class<?> clazz){
this.clazz = clazz;
for (Field field : JdbcSqlStatValue.class.getDeclaredFields()) {
MField annotation = field.getAnnotation(MField.class);
if (annotation == null) {
continue;
}
String columnName = annotation.name();
if (StringUtils.isEmpty(columnName)) {
columnName = field.getName();
}
fields.add(new FieldInfo(field, columnName));
}
}
public Class<?> getClazz() {
return clazz;
}
public String getInsertSql() {
return insertSql;
}
public void setInsertSql(String insertSql) {
this.insertSql = insertSql;
}
public List<FieldInfo> getFields() {
return fields;
}
}
public static class FieldInfo {
private final Field field;
private final String columnName;
public FieldInfo(Field field, String columnName){
this.field = field;
this.columnName = columnName;
field.setAccessible(true);
}
public Field getField() {
return field;
}
public String getColumnName() {
return columnName;
}
public Class<?> getFieldType() {
return field.getType();
}
}
}
@@ -91,7 +91,7 @@ public final class JMXUtils {
map.put("class", error.getClass().getName());
map.put("message", error.getMessage());
map.put("stackTrace", IOUtils.getStackTrace(error));
map.put("stackTrace", Utils.getStackTrace(error));
return new CompositeDataSupport(getThrowableCompositeType(), map);
}
@@ -29,7 +29,7 @@ import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
public class IOUtils {
public class Utils {
public final static int DEFAULT_BUFFER_SIZE = 1024 * 4;
@@ -48,14 +48,14 @@ public class IOUtils {
try {
in = Thread.currentThread().getContextClassLoader().getResourceAsStream(resource);
if (in == null) {
in = IOUtils.class.getResourceAsStream(resource);
in = Utils.class.getResourceAsStream(resource);
}
if (in == null) {
return null;
}
String text = IOUtils.read(in);
String text = Utils.read(in);
return text;
} finally {
JdbcUtils.close(in);
@@ -150,6 +150,19 @@ public class IOUtils {
return buf.toString();
}
public static String toString(Throwable error) {
if (error == null) {
return null;
}
StringWriter strWriter = new StringWriter();
PrintWriter out = new PrintWriter(strWriter);
error.printStackTrace(out);
out.close();
String text = strWriter.toString();
return text;
}
public static String toString(StackTraceElement[] stackTrace) {
StringBuilder buf = new StringBuilder();
for (StackTraceElement item : stackTrace) {
@@ -246,4 +259,69 @@ public class IOUtils {
}
return startTime;
}
public static long murmurhash2_64(String text) {
final byte[] bytes = text.getBytes();
return murmurhash2_64(bytes, bytes.length, 0xe17a1465);
}
/**
* murmur hash 2.0, The murmur hash is a relatively fast hash function from http://murmurhash.googlepages.com/ for
* platforms with efficient multiplication.
*
* @author Viliam Holub
*/
public static long murmurhash2_64(final byte[] data, int length, int seed) {
final long m = 0xc6a4a7935bd1e995L;
final int r = 47;
long h = (seed & 0xffffffffl) ^ (length * m);
int length8 = length / 8;
for (int i = 0; i < length8; i++) {
final int i8 = i * 8;
long k = ((long) data[i8 + 0] & 0xff) //
+ (((long) data[i8 + 1] & 0xff) << 8) //
+ (((long) data[i8 + 2] & 0xff) << 16)//
+ (((long) data[i8 + 3] & 0xff) << 24) //
+ (((long) data[i8 + 4] & 0xff) << 32)//
+ (((long) data[i8 + 5] & 0xff) << 40)//
+ (((long) data[i8 + 6] & 0xff) << 48) //
+ (((long) data[i8 + 7] & 0xff) << 56);
k *= m;
k ^= k >>> r;
k *= m;
h ^= k;
h *= m;
}
switch (length % 8) {
case 7:
h ^= (long) (data[(length & ~7) + 6] & 0xff) << 48;
case 6:
h ^= (long) (data[(length & ~7) + 5] & 0xff) << 40;
case 5:
h ^= (long) (data[(length & ~7) + 4] & 0xff) << 32;
case 4:
h ^= (long) (data[(length & ~7) + 3] & 0xff) << 24;
case 3:
h ^= (long) (data[(length & ~7) + 2] & 0xff) << 16;
case 2:
h ^= (long) (data[(length & ~7) + 1] & 0xff) << 8;
case 1:
h ^= (long) (data[length & ~7] & 0xff);
h *= m;
}
;
h ^= h >>> r;
h *= m;
h ^= h >>> r;
return h;
}
}
@@ -15,7 +15,7 @@
*/
package com.alibaba.druid.wall;
import static com.alibaba.druid.util.IOUtils.getBoolean;
import static com.alibaba.druid.util.Utils.getBoolean;
import java.sql.DatabaseMetaData;
import java.sql.SQLException;
@@ -45,9 +45,9 @@ import com.alibaba.druid.wall.violation.SyntaxErrorViolation;
public class WallFilter extends FilterAdapter implements WallFilterMBean {
private final static Log LOG = LogFactory.getLog(WallFilter.class);
private final static Log LOG = LogFactory.getLog(WallFilter.class);
private boolean inited = false;
private boolean inited = false;
private WallProvider provider;
@@ -55,12 +55,10 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
private WallConfig config;
private volatile boolean logViolation = false;
private volatile boolean throwException = true;
private volatile boolean logViolation = false;
private volatile boolean throwException = true;
private boolean autoRegisterToGlobalTimer = false;
public final static String ATTR_SQL_STAT = "wall.sqlStat";
public final static String ATTR_SQL_STAT = "wall.sqlStat";
public WallFilter(){
configFromProperties(System.getProperties());
@@ -80,12 +78,6 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
this.throwException = value;
}
}
{
Boolean value = getBoolean(properties, "druid.wall.autoRegisterToGlobalTimer");
if (value != null) {
this.autoRegisterToGlobalTimer = value;
}
}
}
@Override
@@ -138,15 +130,6 @@ public class WallFilter extends FilterAdapter implements WallFilterMBean {
throw new IllegalStateException("dbType not support : " + dbType + ", url " + dataSource.getUrl());
}
if (autoRegisterToGlobalTimer) {
WallProviderStatTimer timer = WallProviderStatTimer.getInstance();
if (timer == null) {
timer = new WallProviderStatTimer();
timer.start();
}
timer.register(provider);
}
this.inited = true;
}
@@ -1,130 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.wall;
import static com.alibaba.druid.util.IOUtils.getInteger;
import static com.alibaba.druid.util.IOUtils.getLong;
import java.util.List;
import java.util.Properties;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.TimeUnit;
import com.alibaba.druid.support.logging.Log;
import com.alibaba.druid.support.logging.LogFactory;
import com.alibaba.druid.util.IOUtils;
public class WallProviderStatTimer implements Runnable {
private final static Log LOG = LogFactory.getLog(WallProviderStatTimer.class);
private ScheduledExecutorService scheduler;
private int threadCount = 1;
private long period = 60 * 5;
private TimeUnit unit = TimeUnit.SECONDS;
private final List<WallProvider> providers = new CopyOnWriteArrayList<WallProvider>();
private WallProviderStatLogger statLogger = new WallProviderStatLoggerImpl();
private static WallProviderStatTimer instance;
public WallProviderStatTimer(){
configFromPropety(System.getProperties());
}
public static WallProviderStatTimer getInstance() {
return instance;
}
public static void setInstance(WallProviderStatTimer instance) {
WallProviderStatTimer.instance = instance;
}
public void configFromPropety(Properties properties) {
{
Long value = getLong(properties, "druid.wall.timer.period");
if (value != null && value.intValue() > 0) {
this.period = value.intValue();
}
}
{
Integer value = getInteger(properties, "druid.wall.timer.threadCount");
if (value != null && value.intValue() > 0) {
this.threadCount = value.intValue();
}
}
{
String value = properties.getProperty("druid.wall.timer.statLoggerClass");
if (value != null && value.length() > 0) {
Class<?> clazz = IOUtils.loadClass(value);
try {
statLogger = (WallProviderStatLogger) clazz.newInstance();
} catch (Exception ex) {
LOG.error("configStatLogger error", ex);
}
}
}
}
public List<WallProvider> getProviders() {
return providers;
}
public void register(WallProvider provider) {
this.providers.add(provider);
}
public void setProviders(List<WallProvider> providers) {
this.providers.clear();
this.providers.addAll(providers);
}
public WallProviderStatLogger getStatLogger() {
return statLogger;
}
public void setStatLogger(WallProviderStatLogger statLogger) {
this.statLogger = statLogger;
}
public void start() {
if (statLogger == null) {
throw new IllegalStateException("statLogger is null");
}
scheduler = Executors.newScheduledThreadPool(threadCount);
scheduler.scheduleAtFixedRate(this, period, period, unit);
}
@Override
public void run() {
for (WallProvider provider : this.providers) {
WallProviderStatValue statValue = getStatValue(provider);
log(statValue);
}
}
protected WallProviderStatValue getStatValue(WallProvider provider) {
return provider.getStatValue(true);
}
protected void log(WallProviderStatValue statValue) {
statLogger.log(statValue);
}
}