SPRING INTERCEPTOR 监控 JMS 发送接受性能

13 篇文章 0 订阅
2 篇文章 0 订阅
将spring aspectj 和 jms 整合了一下,用spring interceptor 监控jms 发送接受方法。通过方法注解 标记执行总次数,拦截器在指定的方法执行到目标执行次数后,将性能跟踪信息记录日志。

aop-conf.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:oxm="http://www.springframework.org/schema/oxm" xmlns:cache="http://www.springframework.org/schema/cache"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/oxm http://www.springframework.org/schema/oxm/spring-oxm-3.2.xsd
http://www.springframework.org/schema/cache
http://www.springframework.org/schema/cache/spring-cache.xsd">

<bean id="jmsMessageHanderMonitor"
class="com.cn.ld.modules.jms.monitor.JmsMessageHanderMonitor">
<property name="useDynamicLogger" value="false" />
<constructor-arg index="0">
<value>1</value>
</constructor-arg>
</bean>

<aop:config>
<aop:pointcut id="allServiceMethods"
expression="execution(* com.cn.ld.modules.jms.worker..*(..))" />
<aop:advisor pointcut-ref="allServiceMethods" advice-ref="jmsMessageHanderMonitor"
order="2" />
</aop:config>
</beans>



log4j.properties 开启监控interceptor race 日志功能

log4j.logger.com.cn.ld.modules.jms.monitor.JmsMessageHanderMonitor=TRACE



applicationContext.xml

<!-- aop source -->
<import resource="aop-config.xml" />


MethodMonitorCount 注解接口

package com.cn.ld.modules.annotation;

import java.lang.annotation.Retention;
import java.lang.annotation.Target;
import java.lang.annotation.ElementType;
import java.lang.annotation.RetentionPolicy;
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface MethodMonitorCount {
int value() default 1;
}



JmsMessageHanderMonitor 性能监控的aspect


package com.cn.ld.modules.jms.monitor;

import java.lang.annotation.Annotation;
import java.lang.reflect.Method;
import java.text.MessageFormat;
import java.util.concurrent.ConcurrentHashMap;

import org.aopalliance.intercept.MethodInvocation;
import org.apache.commons.logging.Log;
import org.springframework.aop.framework.ReflectiveMethodInvocation;
import org.springframework.aop.interceptor.AbstractMonitoringInterceptor;
import org.springframework.util.StopWatch;

import com.cn.ld.modules.annotation.MethodMonitorCount;

public class JmsMessageHanderMonitor extends AbstractMonitoringInterceptor {

private static final long serialVersionUID = 1L;
private ConcurrentHashMap<java.lang.reflect.Method, PerformanceSampl> methodPerfSampl = new ConcurrentHashMap<java.lang.reflect.Method, PerformanceSampl>();

private int monitorCount = 1;

public JmsMessageHanderMonitor(int monitorCount) {
this.monitorCount = monitorCount;
}

public JmsMessageHanderMonitor(boolean useDynamicLogger) {
setUseDynamicLogger(useDynamicLogger);
}

@Override
protected Object invokeUnderTrace(MethodInvocation methodinvocation,
Log logger) throws Throwable {
ReflectiveMethodInvocation reflectMethod = (ReflectiveMethodInvocation) methodinvocation;
Method m = reflectMethod.getMethod();
Annotation[] annotations = m.getAnnotations();

if (null == annotations || annotations.length == 0) {
return methodinvocation.proceed();
}

PerformanceSampl sampl = null;
// 将追踪的目标方法作为key缓存,如果已经存在,直接获取该方法的跟踪内容对象PerformanceSampl
if (methodPerfSampl.containsKey(m)) {
sampl = methodPerfSampl.get(m);
} else {
sampl = new PerformanceSampl();
methodPerfSampl.put(m, sampl);
}
String target = m.toString();
// 首次目标对象调用,初始化跟踪信息
if (sampl.getReceNo() == 0) {

StopWatch sw = new StopWatch(target);
sampl.setStopWatch(sw);
MethodMonitorCount mmc = (MethodMonitorCount) annotations[0];
sampl.setMaxReceNo(mmc.value());
sw.start(m.getName());
}

// 目标方法执行
Object obj = methodinvocation.proceed();

// 每次调用累加调用次数
sampl.setReceNo(sampl.getReceNo() + 1);

// 目标方法调用次数等于目标最大调用次数时,统计目标方法执行性能指标
if (sampl.getReceNo() == sampl.getMaxReceNo()) {
sampl.getStopWatch().stop();

// 记录日志
showTraceInfo(logger, sampl, target);

// 追踪结束,将该方法的cache信息清空
methodPerfSampl.remove(m);
}

return obj;
}

private void showTraceInfo(Log logger, PerformanceSampl sampl, String target) {
String formatStr = "monitor target method:{0} ; expect execute times:{1};actual execute times:{2};execution Speed:{3}/s";
MessageFormat paramMf = new MessageFormat(formatStr);
long costTime = (sampl.getStopWatch().getTotalTimeMillis() / 1000);
int executeTimes = sampl.getReceNo();
costTime = costTime == 0 ? costTime : (executeTimes / costTime);
logger.trace(paramMf.format(new Object[] { target,
sampl.getMaxReceNo(), executeTimes, costTime }));
}
/*
*性能信息
*/
private static class PerformanceSampl {
private StopWatch stopWatch;
private int maxReceNo;
// 接受数量
private int receNo;

public void setReceNo(int receNo) {
this.receNo = receNo;
}

public StopWatch getStopWatch() {
return stopWatch;
}

public void setStopWatch(StopWatch stopWatch) {
this.stopWatch = stopWatch;
}

public int getMaxReceNo() {
return maxReceNo;
}

public void setMaxReceNo(int maxReceNo) {
this.maxReceNo = maxReceNo;
}

public int getReceNo() {
return receNo;
}

}

public int getMonitorCount() {
return monitorCount;
}

public void setMonitorCount(int monitorCount) {
this.monitorCount = monitorCount;
}
}




console 信息如下

[2013-07-18 17:12:00,645][TRACE]<main>(JmsMessageHanderMonitor.java:87) - monitor target method:public void com.cn.ld.modules.jms.worker.JmsSender.sendSingle(java.lang.String,javax.jms.Destination) ; expect execute times:10,000;actual execute times:10,000;execution Speed:10,000/s
[2013-07-18 17:12:00,784][TRACE]<jmsContainer-1>(JmsMessageHanderMonitor.java:87) - monitor target method:public abstract void com.cn.ld.modules.jms.handler.MessageHandler.handleMessage(java.lang.String) ; expect execute times:10,000;actual execute times:10,000;execution Speed:10,000/s
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值