简体   繁体   中英

Abstract or annotated class instead interface as MXBean

Is there a way to use abstract or annotated class as MXBean descriptor? I have interface like:

@MXBean
public interface Peer {
    public String getName();
}

and want that MXBean to be combined in class with more local-side-only methods like:

public class PeerCombinedMXBean {
    // Expose this as MXBean attribute
    public String getName() { ... }

    // This method is local-instance-oriented
    public boolean isValid() { ... }
}

I need model like above to avoid chain-in proxy object instead to use complex half-proxified instance like:

PeerCombinedMXBean peer = JMX.newMXBeanProxy(connection, name, PeerCombinedMXBean.class);
if (peer.isValid()) System.out.println(peer.getName());

Edit

This question is related to java.net article . What is they progress? Can I use MBeans with annotation safely now?

What I ended up doing for this was writing a custom annotation that you place on a method or property. Then, I implemented the DynamicMBean interface in such a way that it parsed out the annotations on the class in question and then registers them with the PlatformMBeanServer. As far as I know, there are no public implementations of this available, I also searched extensively about this topic before I just did it myself.

For example, here is the class that I wish to manage from JConsole:

public class Foo
{
  // In JMX Console
  @Managed
  private boolean isBar;

  // Not in JMX Console
  private boolean isFoo;

  // In JMX Console
  @Managed
  public String getClassName()
  {
    return Foo.class.getName();
  }
}

Then, when my application starts up, I register an instance of this class using my implementation of DynamicMBean and parse out the annotations.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM