我想要做的是: 通过JMX公开呼叫统计信息(基本上调用计数和平均呼叫时间)。 问题是我想用AspectJ支持来做到这一点(为我的实现类自动计算它)。使用JMX编程式公开呼叫统计信息
我创造了什么是JmxBean:
public class JmxStatistics implements Serializable {
private final String name;
private long errorCount;
private long errorCallTime;
private long successCount;
private long successCallTime;
public JmxStatistics(String name) {
this.name = name;
}
public synchronized long getCallCount() {
return errorCount + successCount;
}
public synchronized long getAvgCallTimeInMillisecs() {
return (errorCallTime + successCallTime)/getCallCount();
}
public synchronized long getAvgSuccessfulCallTimeInMillisecs() {
return (errorCallTime + successCallTime)/successCount;
}
public synchronized long getAvgFailedCallTimeInMillisecs() {
return (errorCallTime + successCallTime)/errorCount;
}
public synchronized void increaseErrorCallTime(long sumCallTime) {
this.errorCallTime += sumCallTime;
}
public synchronized void increaseSuccessCallTime(long sumCallTime) {
this.successCallTime += sumCallTime;
}
public synchronized long getErrorCount() {
return errorCount;
}
public synchronized void incrementErrorCount() {
this.errorCount++;
}
public synchronized long getSuccessCount() {
return successCount;
}
public synchronized void incrementSuccessCount() {
this.successCount++;
}
@Override
public String toString() {
return name + "{" + "callCount=" + getCallCount()
+ " (s: " + successCount + ",e:" + errorCount + "); "
+ "avgCallTime=" + getAvgCallTimeInMillisecs() + "ms "
+ "(" + getAvgSuccessfulCallTimeInMillisecs() + "ms;e:" + getAvgFailedCallTimeInMillisecs() + "ms)" + "}";
}
的AOP包点:
public Object wrap(ProceedingJoinPoint joinPoint) throws Throwable {
JmxStatistics jmxs = store.getStatisticsBean(joinPoint);
long start = System.currentTimeMillis();
Object result;
try {
result = joinPoint.proceed();
} catch (Throwable t) {
long runtime = System.currentTimeMillis() - start;
jmxs.incrementErrorCount();
jmxs.increaseErrorCallTime(runtime);
throw t;
}
long runtime = System.currentTimeMillis() - start;
jmxs.incrementSuccessCount();
jmxs.increaseSuccessCallTime(runtime);
return result;
}
而对于公开所有(编程创建)JMX豆商店:
public class JmxStatisticsStore {
private final HashMap<String, JmxStatistics> jmxBeans = new HashMap<>();
public HashMap<String, JmxStatistics> getJmxBeans() {
return jmxBeans;
}
JmxStatistics getStatisticsBean(ProceedingJoinPoint joinPoint) {
String id = joinPoint.getTarget().getClass().toString() + "." + joinPoint.getSignature().getName();
if (!jmxBeans.containsKey(id)) {
synchronized (jmxBeans) {
if (!jmxBeans.containsKey(id)) {
JmxStatistics jmxStatistics = new JmxStatistics(id);
jmxBeans.put(id, jmxStatistics);
}
}
}
return jmxBeans.get(id);
}
}
我想为此使用Spring AOP和MBeanExporter。我的配置如下:
<bean id="jmxStatisticsStore" class="package.JmxStatisticsStore"/>
<bean id="jmxAspect" class="package.AspectJJmxCalculator" />
<aop:config>
<aop:aspect id="jmxAspectId" ref="jmxAspect">
<aop:pointcut id="pointCutAround"
expression="execution(* jmx.tester..*.*(..))" />
<aop:around method="wrap" pointcut-ref="pointCutAround" />
</aop:aspect>
</aop:config>
<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter" lazy-init="false">
<property name="beans">
<map>
<entry key="bean:name=callStatistics" value-ref="jmxStatisticsStore"/>
</map>
</property>
</bean>
目前,我尝试在Tomcat的运行它(jmx.tester包有我在web应用程序使用的Spring bean)。该webapp运行正常,并与JConsole我可以连接到服务器。在服务器上,在bean下,我可以看到带有HashMap属性的callStatistics bean。
直到我没有打电话,散列表是空的(这很好)(在“Value”框中显示{}) 在我拨打电话后(因此散列表不再为空),价值变得不可用,如果我尝试调用通过JMX的getJmxBeans方法,我得到以下异常:
Problem invoking getJmxBeans: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: package.JmxStatistics (no security manager: RMI class loader disabled)
编辑:我想看到的是,对于不同的ID来电显示为单独的JMX属性。
有没有办法做我想完成的事情? Thx
嗯......稍后会检查这一点,如果它是比我们现在更好的解决方案 – SzaboAdam 2014-09-30 13:53:59