Spring中实现简单的性能监控

以下介绍在Spring中如何实现简单的性能监控,监控每一个Spring管理的方法调用过程中花费的时间,并把相关信息记录到日志数据库中;

根据以上的问题,我们首先想到的应该就是Spring提供的AOP了,确实是用AOP可以很容易的解决这个问题;

参考如下步骤:
1 创建MethodBeforeAdvice,记录方法执行的开始时间,如下
package whf.framework.aop;

import java.lang.reflect.Method;

import whf.framework.service.Service;

public class MethodBeforeAdvice implements org.springframework.aop.MethodBeforeAdvice {
public static ThreadLocal<Long> threadLocal = new ThreadLocal<Long>();

public void before(Method method, Object[] args, Object target)
throws Throwable {
if(target instanceof Service){
threadLocal.set(System.currentTimeMillis());
}
}
}
2 创建MethodAfterAdvice,将监控的信息记录到数据库
package whf.framework.aop;

import java.lang.reflect.Method;

import org.springframework.aop.AfterReturningAdvice;

import whf.framework.config.ApplicationConfig;
import whf.framework.log.Log;
import whf.framework.log.LogFactory;
import whf.framework.log.entity.Logable;
import whf.framework.log.util.LoggerUtils;
import whf.framework.meta.Metaable;
import whf.framework.security.entity.User;
import whf.framework.service.Service;
import whf.framework.util.StringUtils;
import whf.framework.util.ThreadContext;

public class MethodAfterAdvice implements AfterReturningAdvice {
private static Log log = LogFactory.getLog(MethodAfterAdvice.class);

public void afterReturning(Object object, Method method, Object[] args,
Object target) throws Throwable {
if(target instanceof Service){
long consumeTime = System.currentTimeMillis() - MethodBeforeAdvice.threadLocal.get();
User user = ThreadContext.getUserInUserContext();
String sessionId = ThreadContext.getSessionId();
String department = user ==null? null:user.getDept() == null?null: user.getDept().getCode();
String operator = user == null?"anonymous":user.getName()+"("+user.getUsername()+")";
String ip = ThreadContext.getUserContext() != null? ThreadContext.getUserContext().getRemoteIpAddress(): null;
if(ApplicationConfig.getInstance().isServiceMonitoring() &&
!(target instanceof Logable) && !(target instanceof Metaable)) {
String params = StringUtils.toString(args);
whf.framework.log.entity.Log logEntity = new whf.framework.log.entity.Log(department, operator,
target.getClass().getSuperclass().getName(), "SERVICE INVOKE", method.getName(), params,
sessionId, ip, consumeTime, null);
LoggerUtils.log(logEntity);
}
if(object != null)
log.debug("Method:" + consumeTime + " -\t" + operator + " - \t" + target.getClass().getName() +":"+object.getClass().getName()+"->"+method.getName());
else
log.debug("Method:" + consumeTime + " -\t" + operator + " - \t" + target.getClass().getName()+"->"+method.getName());
}
}
}

3 增加配置,将监控配置到Spring中
<bean id="methodBeforeAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean class="whf.framework.aop.MethodBeforeAdvice" />
</property>
<property name="mappedName">
<value>*</value>
</property>
</bean>
<bean id="methodAfterAdvisor" class="org.springframework.aop.support.NameMatchMethodPointcutAdvisor">
<property name="advice">
<bean class="whf.framework.aop.MethodAfterAdvice" />
</property>
<property name="mappedName">
<value>*</value>
</property>
</bean>

<bean class="org.springframework.aop.framework.autoproxy.BeanNameAutoProxyCreator">
<property name="proxyTargetClass">
<value>true</value>
</property>
<property name="beanNames">
<value>*Service</value>
</property>
<property name="interceptorNames">
<list>
<value>transactionInterceptor</value>
<value>methodBeforeAdvisor</value>
<value>methodAfterAdvisor</value>
</list>
</property>
</bean>

完成了,现在Spring会自动的将所有的监控相关信息记录到数据库;
其中需要说明的是,如果你监视的结果存储到数据库的话,那么你的日志的表一定要在这里面过滤掉,不然会出现死循环(插入日志 -〉 监控到日志查询信息 -〉 插入日志 。。。。。。)

详细源代码参考:[url]http://whfframework.googlecode.com/svn/trunk/[/url]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值