On a new project with Guice, then, how do I magically wire my managed objects to JMX? Sadly JSR-255, aka JMX2, did not make JDK6 or JDK7 (there's always JDK8). I live in the here and now, and relied before on Spring JMX annotations and MBeanExporter.
There's no full implementation of JMX2, but covering a good minimal feature set is pojo-mbean. How to get Guice to register @MBean-annotated objects? Use injectors:
class SomeModule extends AbstractModule {
@Override
protected void configure() {
bindListener(Matchers.any(), new JMXTypeListener());
}
}
class JMXTypeListener implements TypeListener {
@Override
public <I> void hear(TypeLiteral<I> type,
TypeEncounter<I> encounter) {
Class<? super I> rawType = type.getRawType();
// From pojo-mbean; eventually JSR-255
if (rawType.isAnnotationPresent(MBean.class))
encounter.register(new InjectionListener<I>() {
@Override
public void afterInjection(final I injectee) {
try {
// From pojo-mbean; eventually JSR-255
new MBeanRegistration(injectee,
objectName(rawType)).
register();
} catch (Exception e) {
encounter.addError(e);
}
}
});
}
private static <I> ObjectName objectName(Class<? super I> type)
throws MalformedObjectNameException {
return new ObjectName(type.getPackage().getName(), "type",
type.getSimpleName());
}
}
No comments:
Post a Comment