How to add description to JMX MXBean

孤者浪人 提交于 2021-01-29 01:31:25

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!