martint / jmxutils

Exporting JMX mbeans made easy
Apache License 2.0
171 stars 47 forks source link

Registering with Guice TypeListener #34

Open nheitz opened 8 years ago

nheitz commented 8 years ago

I am very keen to switch to this JMX enabling library and move away from pojo-mbean, as the features here are a more natural fit. However, I am having trouble letting the export process "discover" managed beans as they are injected, a pattern I have used to great effect previously. Using jmxutils, the following works:

 private ExportBinder exporter;

 @Override
    protected void configure() {
        bind(MBeanServer.class).toInstance(ManagementFactory.getPlatformMBeanServer());
        install(new MBeanModule());
        exporter = ExportBinder.newExporter(binder());
        exporter.export(MyClass.class).withGeneratedName();
    }

but using Listeners doesn't (I'm using the ManagedBean annotation to "mark" my class...assume that MyClass has that annotation):

   private ExportBinder exporter;

   @Override
    protected void configure() {
        bind(MBeanServer.class).toInstance(ManagementFactory.getPlatformMBeanServer());
        bindListener(Matchers.any(), new JmxTypeListener());
        install(new MBeanModule());
        exporter = ExportBinder.newExporter(binder());
    }

    private class JmxTypeListener implements TypeListener {
        @Override
        public <I> void hear(TypeLiteral<I> type, final TypeEncounter<I> encounter) {

            final Class<? super I> rawType = type.getRawType();

            if (rawType.isAnnotationPresent(ManagedBean.class)) {
                exporter.export(rawType).withGeneratedName(); //this seems to have no effect!
...

Everything executes as expected, but no entry shows up in the jmx console for the second approach. Is it too late to export beans by the time the listener is "hearing" bindings? Is there any workaround that means I don't have to explicitly export beans by type?