How to add description for MBean method to see it in jmx-console of JBOSS

筅森魡賤 提交于 2019-12-04 07:17:06

In Java, you can do this, if you don't use standard MBeans, but e.g. DynamicMBeans for which you need to implement getMBeanInfo() which is returning all that data. This is a generic way, not limited to JBoss. But it is also a lot of work, which (IMO) only makes sense if you really need the dynamic features of a DynamicMBean.

For completeness sake (and as this may be the easier approach):

You can write an xmbean-descriptor and put that e.g. into $SERVER/conf/xmdesc/ In addition to this you need to enhance the standard MBeean-descriptor like this (note the xmbean-dd attribute:

<mbean code="org.jnp.server.NamingBeanImpl"
   name="jboss:service=NamingBeanImpl"
   xmbean-dd="resource:xmdesc/NamingBean-xmbean.xml">
</mbean>

This example is taken from $SERVER/conf/jboss-service.xml and the NamingBean-xmban.xml is in the path described by the attribute.

I have created a small wrapper that will create a dynamic MBean out of a normal Java class through annotations. With it you can also add descriptions to beans, attributes, operations and parameters.

It also supports externalization and localization of names and descriptions using Java ResourceBundles.

Example of an annotated class:

@JMXBean(description = "My first JMX bean test")
public class MyBean {
    int level = 0;

    @JMXBeanAttribute(name = "Floor Level", description = "The current floor level")
    public int getLevel() {
      return level;
    }

    @JMXBeanAttribute
    public void setLevel(int newLevel) {
      level = newLevel;
    }

    @JMXBeanOperation(name = "Echo Test", description = "Echoes the parameter back to you")
    public String myMethod(
             @JMXBeanParameter(name = "Input", description = "String of what to echo") String param) {
      return "You said " + param;
    }
}

Example of an annotated class using ResourceBundles:

@JMXBean(resourceBundleName="com.example.my.package.BundleName")
public class MyBean {

  int level = 0;

  @JMXBeanAttribute(nameKey="level", descriptionKey="levelDescription")
  public int getLevel() {
    return level;
  }
}

How to use it:

MyBean bean = new MyBean();
JMXBeanWrapper wrappedBean = new JMXBeanWrapper(bean);
MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
mbs.registerMBean(wrappedBean, new Objectname("com.example.my.package:type=TestBean,name=My Bean"));

You can find the source on GitHub

I've had success with mixing Spring XML and Spring annotations where I had multiple MBeans of the same Java class. The approach allowed tight control over bean names and allowed me to define descriptions etc at the class level. I needed to define an annotation based bean assembler for an MBeanExporter, and supply a map of bean names and bean references:

<bean id="exporter" class="org.springframework.jmx.export.MBeanExporter"
      lazy-init="false">
    <property name="server" ref="mbeanServer" />
    <property name="assembler">
        <!-- will create management interface using annotation metadata -->
        <bean class="org.springframework.jmx.export.assembler.MetadataMBeanInfoAssembler">
            <property name="attributeSource">
                <bean class="org.springframework.jmx.export.annotation.AnnotationJmxAttributeSource"/>
            </property>
        </bean>
    </property>
    <property name="beans">
        <map>
           <!-- entries -->
        </map>
    </property>
</bean>

An example of what I read from Java annotations might be:

@ManagedAttribute(description = "A detailed description to show in JConsole tooltips etc")
public String getFoo() {
    return foo;
}

I had the assemblers defined privately to the exporter, but you could share those beans more widely I'm sure.

BTW this was on Tomcat.

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