Reputation: 324
What I would like to do is the following: Exposing call statistics (basically call count and avg. call time) through JMX. What the problem is that I would like to do this with AspectJ support (to automatically calculate it for my implementation classes).
What I created is the 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)" + "}";
}
The AOP wrap point:
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;
}
And a store for exposing all (programatically created) JMX beans:
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);
}
}
I would like to use Spring AOP and MBeanExporter for this. My configuration is the following:
<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>
Currently, I try to run it in tomcat (the jmx.tester package has a Spring bean that I use in a webapp). The webapp is running correctly, and with JConsole I can connect to the server. On the server, under beans I can see the callStatistics bean, with the HashMap Attribute.
Until I don't make a call, the hashmap is empty (which is good) (on the "Value" box it shows {}) After I make a call (so the hashmap is not empty anymore), the Value becomes Unavailable, and if I try to call the getJmxBeans method through JMX I get the following exception:
Problem invoking getJmxBeans: java.rmi.UnmarshalException: error unmarshalling return; nested exception is:
java.lang.ClassNotFoundException: package.JmxStatistics (no security manager: RMI class loader disabled)
EDIT: What I would like to see is that the calls for the different Ids show as separate JMX attributes.
Is there any way to do what I want to accomplish? Thx
Upvotes: 1
Views: 1216
Reputation: 738
I recommend you Metrics library.
It has Spring integration and reports via JMX.
Use this library into Spring is very easy using annotations. This is an example;
public class 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 }
}
Upvotes: 1
Reputation: 324
Managed to solve this. Modified the 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);
}
This way this creates the new MBeans at runtime
Upvotes: 1