问题
I want to expose my MXBeans on Apache-Tomcat 7.0. Though my MXBean registers successfully, I am unable to add description to the Operations that are exposed by thoese MXBeans.
Registering MXBeans
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
ObjectName m_mxbeanOName = new ObjectName( "MyMXBean:type=" + "MyComponent"+",name=MyMXBean");
MyMXBean m_mxbean = new MyMXBean ();
if(!mbs.isRegistered(m_mxbeanOName))
mbs.registerMBean(m_mxbean, m_mxbeanOName);
MyMXBean Interface
public interface MyMXBean {
public int add (int x, int y);
}
MyMXBean Implementation
import com.sun.org.glassfish.gmbal.Description;
import com.sun.org.glassfish.gmbal.DescriptorFields;
import com.sun.org.glassfish.gmbal.Impact;
import com.sun.org.glassfish.gmbal.ManagedOperation;
public class MyMXBeanImpl implements MyMXBean {
@ManagedOperation(impact=Impact.ACTION_INFO)
@Description("Integer Addition: First parameter is the augend and second parameter is the addend.")
@DescriptorFields({"p1=augend","p2=addend"})
public int add(int x, int y) {
return x + y;
}
The annotation @ManagedOperation, @Description, @DescriptorFields has no effect on the jconsole. JConsole continues to show default values
Please tell me ways to show the description about my MXBean operations on JConsole.
回答1:
The cleanest way I have found to do this is to use StandardMBean (or StandardEmitterMBean) as the actual object you register with JMX. Then, subclass StandardMBean and override the various getDescription methods. In those methods, read your annotations that contain the descriptions.
回答2:
I found this very nice blogentry with code for @Descriptor and @Name attributes and a AnnotatedStandardMXBean wrapper which handles this.
http://actimem.com/java/jmx-annotations/
Sample MXBean using this:
@MXBean
@Description("A test resource")
public interface SampleMXBean {
@Description("string#1")
String getString1();
@Description("string#2")
String getString2();
@Description("string#3")
String string3(@Description("int i") @Name("i") int i, @Description("long j") @Name("j") long j);
}
来源:https://stackoverflow.com/questions/22810055/how-to-add-description-to-jmx-mxbean