监控sql执行时间
2014-05-06 09:18
344 查看
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.time.StopWatch;
import com.njhh.fmcp.log.PortalRunLog;
import com.njhh.fmcp.util.BeanMaker;
import com.njhh.fmcp.util.SystemConfigUtil;
/**
* 监控系统dao执行时间
* @author
*
*/
public class SqlTimeAdvice implements MethodInterceptor{
private PortalRunLog log = (PortalRunLog)BeanMaker.getBean("monitorLog");
private PortalRunLog runLog = (PortalRunLog)BeanMaker.getBean("portalRunLog");
private long time = SystemConfigUtil.getTimeThresholdKey();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
StopWatch clock = new StopWatch();
// 计时开始
clock.start();
Object result = null;
// 监控的类名
String className = invocation.getMethod().getDeclaringClass().getSimpleName();
//监控的方法名
String methodName = className + "." + invocation.getMethod().getName();
try {
//这个是我们监控的bean的执行并返回结果
result = invocation.proceed();
} catch (Throwable e) {
//监控的参数
runLog.error("数据库执行异常 , 方法名[" + methodName + "]" + " 参数:" + getString(invocation.getArguments()));
throw e;
}
clock.stop(); //计时结束
// 执行超过1秒的sql需要记录到监控日志
if (clock.getTime() >= time){
log.info("执行时间:" + clock.getTime() + " ms [" + methodName + "]" + " 参数:" + getString(invocation.getArguments()));
}
return result;
}
public String getString(Object[] objs) {
StringBuffer stringBuffer = new StringBuffer();
for (Object object : objs) {
stringBuffer.append("[").append(object.toString()).append("],");
}
if (stringBuffer.length() > 0){
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
}
return stringBuffer.toString();
}
}
<bean id="SqlProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
p:beanNames="*Dao,*DaoImpl" p:interceptorNames="sqlTimeAdvice" />
<bean id="sqlTimeAdvice" class="com.njhh.fmcp.interceptor.SqlTimeAdvice" />
import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.lang.time.StopWatch;
import com.njhh.fmcp.log.PortalRunLog;
import com.njhh.fmcp.util.BeanMaker;
import com.njhh.fmcp.util.SystemConfigUtil;
/**
* 监控系统dao执行时间
* @author
*
*/
public class SqlTimeAdvice implements MethodInterceptor{
private PortalRunLog log = (PortalRunLog)BeanMaker.getBean("monitorLog");
private PortalRunLog runLog = (PortalRunLog)BeanMaker.getBean("portalRunLog");
private long time = SystemConfigUtil.getTimeThresholdKey();
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
StopWatch clock = new StopWatch();
// 计时开始
clock.start();
Object result = null;
// 监控的类名
String className = invocation.getMethod().getDeclaringClass().getSimpleName();
//监控的方法名
String methodName = className + "." + invocation.getMethod().getName();
try {
//这个是我们监控的bean的执行并返回结果
result = invocation.proceed();
} catch (Throwable e) {
//监控的参数
runLog.error("数据库执行异常 , 方法名[" + methodName + "]" + " 参数:" + getString(invocation.getArguments()));
throw e;
}
clock.stop(); //计时结束
// 执行超过1秒的sql需要记录到监控日志
if (clock.getTime() >= time){
log.info("执行时间:" + clock.getTime() + " ms [" + methodName + "]" + " 参数:" + getString(invocation.getArguments()));
}
return result;
}
public String getString(Object[] objs) {
StringBuffer stringBuffer = new StringBuffer();
for (Object object : objs) {
stringBuffer.append("[").append(object.toString()).append("],");
}
if (stringBuffer.length() > 0){
stringBuffer.deleteCharAt(stringBuffer.length() - 1);
}
return stringBuffer.toString();
}
}
<bean id="SqlProxyCreator"
class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator"
p:beanNames="*Dao,*DaoImpl" p:interceptorNames="sqlTimeAdvice" />
<bean id="sqlTimeAdvice" class="com.njhh.fmcp.interceptor.SqlTimeAdvice" />
相关文章推荐
- 基于spring,mybatis的监控sql执行时间
- 监控sql语句执行时间
- 监控SQL:执行表中所有sql语句、记录每个语句运行时间(3)
- JAMon监控SQL执行时间
- 监控sql执行时剩余时间
- sqlserver监控sql执行
- 设置Sql Server禁止执行时间长的Sql语句执行
- MSSQL(T-SQL)查询语句执行时间
- 如何得到精确的SQL执行时间
- 获得某段SQL语句执行时间的办法
- 查看sql语句执行时间/测试sql语句性能
- SQLServer 查看SQL语句的执行时间
- 查看SQL执行时间
- 查看sql语句执行时间/测试sql语句性能
- 查看sql语句执行时间/测试sql语句性能
- MySQL 显示SQL执行时间
- 设置显示sql语句执行时间
- oracle通过v$sql视图查询sql的平均执行时间
- oracle 中--怎么查看当前表空间在作什么操作?--查锁,死锁,当前执行时间长的Sql语句,没提交的事务,对象为哪些进程所用
- spring aop 实现方法执行时间监控