以JMX编程方式公开调用统计信息

时间:2014-09-30 13:00:28

标签: java spring tomcat jmx

我想做的是以下内容: 通过JMX公开呼叫统计(基本上是呼叫计数和平均呼叫时间)。 问题是我想用AspectJ支持(为我的实现类自动计算它)。

我创建的是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 bean的商店:

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包中有一个我在webapp中使用的Spring bean)。 webapp正常运行,使用JConsole我可以连接到服务器。在服务器上,在bean下面我可以看到带有HashMap属性的callStatistics bean。

在我不进行调用之前,hashmap为空(这很好)(在“值”框中显示{}) 在我调用之后(所以hashmap不再为空),Value变为Unavailable,如果我尝试通过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

2 个答案:

答案 0 :(得分:1)

我建议您Metrics library

它有Spring integration并通过JMX报告。

使用这个库进入Spring非常容易使用注释。这是一个例子;

  

公共课Foo {

@Timed
public void bar() { /* … */ }

public void baz() {
    this.bar(); // doesn't pass through the proxy

    // fix: reengineer
    // workaround: enable `expose-proxy` and change to:
    ((Foo) AopContext.currentProxy()).bar(); // hideous, but it works
} 
     

}

答案 1 :(得分:1)

管理解决这个问题。 修改了JmxStatisticsStore:

@Autowired
MBeanExporter exporter;

private static final Logger LOG = LogManager.getLogger();
private final HashMap<String, Object> jmxBeans = new HashMap();

JmxData getStatisticsBean(ProceedingJoinPoint joinPoint) {
    String id = "bean:name=" + joinPoint.getTarget().getClass().toString() + "." + joinPoint.getSignature().getName();
    if (!jmxBeans.containsKey(id)) {
        synchronized (jmxBeans) {
            if (!jmxBeans.containsKey(id)) {
                JmxData jmxStatistics = new JmxData(id);
                jmxBeans.put(id, jmxStatistics);
                try {
                    exporter.registerManagedResource(jmxStatistics, new ObjectName(id));
                } catch (MalformedObjectNameException ex) {
                    LOG.warn("Error while registering " + id, ex);
                }
            }
        }
    }
    return (JmxData) jmxBeans.get(id);
}

这样就可以在运行时创建新的MBean