什么时候初始化JMXBean

时间:2012-04-16 10:19:13

标签: java jmx

IMEI我是MXbeans的新手。我想了解MXBean实际初始化的时间,在我支持的应用程序中,我们有一个通知系统,在有一些通知到达之前我无法在JConsole中看到我的MXBean。这是我的MXBean的代码。

    package mecomaany.instrumentation;

    import java.lang.management.ManagementFactory;
    import java.util.ArrayList;

    import javax.management.Attribute;
    import javax.management.AttributeList;
    import javax.management.AttributeNotFoundException;
    import javax.management.DynamicMBean;
    import javax.management.InvalidAttributeValueException;
    import javax.management.MBeanAttributeInfo;
    import javax.management.MBeanException;
    import javax.management.MBeanInfo;
    import javax.management.MBeanOperationInfo;
    import javax.management.MalformedObjectNameException;
    import javax.management.ObjectName;
    import javax.management.ReflectionException;

    import  BulkCMIRP_MxBean;
    import SoapIRPLogger;

    /**
     * This dynamic MBean exposes a method to return the 10 latest Basic CM
     * operations.
     * 
 * @author eshtrom
 * 
 */
    public class BasicCMIRP_MxBean implements DynamicMBean{
    private static final String CLASS_NAME =        BasicCMIRP_MxBean.class.getCanonicalName();
    private static final String MX_BEAN_NAME =   BasicCMIRP_MxBean.class.getCanonicalName() + ":Type=BasicCMIRP_MxBean";
    private static final String BEAN_DESCRIPTION = "Instrumentation bean for SOAP Basic CM IRP.";
    private static final int NUM_OPERATIONS_TO_RECORD = 10;
    private final ArrayList<String> basicCMOperations = new ArrayList<String>();

    /**
     * Register the bean. This is a best effort attempt. If registration fails
     * we'll report it but nothing more.
     */
    public BasicCMIRP_MxBean() {
        registerMxBean();
    }

    /**
     * Attempt to unregister and clean up the MX beans.
     */
    public void destroy() {
        basicCMOperations.clear();
        unregisterMxBean();
    }

    /**
     * This method returns a description of this bean to the JMX interface. The
     * description is just a list of names of the currently stored attributes.
     */
    public synchronized MBeanInfo getMBeanInfo() {
        final MBeanAttributeInfo[] attributes = new MBeanAttributeInfo[0];
        final MBeanOperationInfo[] operations = { new MBeanOperationInfo("getBasicCMInstrumentation", "Get instrumentation Basic CM IRP bean.", null, "String[]",
                MBeanOperationInfo.INFO) };
        return new MBeanInfo(this.getClass().getName(), BEAN_DESCRIPTION, attributes, null, operations, null);
    }



    /**
     * Callback to execute methods exposed by this dynamic MBean.
     */
    public Object invoke(final String actionName, final Object[] params, final String[] signature) throws MBeanException, ReflectionException {
        SoapIRPLogger.enter(CLASS_NAME, "invoke");
        if (actionName != null) {
            if (actionName.equals("getBulkCMInstrumentation")) {
                return getBasicCMInstrumentation();
            }
        }
        SoapIRPLogger.exit(CLASS_NAME, "invoke");
        throw new ReflectionException(new NoSuchMethodException(actionName));
    }


    /**
     * Construct a human readable very of the last 10 operations and return it.
     * 
     * @return string array as an object.
     */
    private Object getBasicCMInstrumentation() {
        SoapIRPLogger.enter(CLASS_NAME, "getBasicCMInstrumentation");
        String[] result = new String[basicCMOperations.size()];
        int index = 0;
        for (String operation : basicCMOperations) {
            result[index] = operation;
            index++;
        }
        SoapIRPLogger.exit(CLASS_NAME, "getBasicCMInstrumentation");
        return result;
    }

    /**
     * No attributes are writable so this method will throw an exception.
     */
    public void setAttribute(final Attribute arg0) throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
        throw new AttributeNotFoundException("All attributes on this bean are read only.");
    }

    /**
     * No attributes are writable so this method will return an empty list.
     */
    public AttributeList setAttributes(final AttributeList arg0) {
        return new AttributeList();
    }


    public Object getAttribute(final String attribute) throws AttributeNotFoundException, MBeanException, ReflectionException {
        throw new AttributeNotFoundException("No attributes are defined on this MX bean.");
    }

    /**
     * No attributes are readable so this method will return an empty list.
     */
    public AttributeList getAttributes(final String[] attributes) {
        return new AttributeList();
    }

    /**
     * Add or update and instrumentation attribute. A null attribute name will
     * be ignored.
     * 
     * @param name
     * @param value
     */
    public void updateInstrumentationAttibute(final String name, final String value) {
        // The only attribute that this bean supports is Bulk CM operations.
        if (name.compareTo("BasicCM_Operations") == 0) {
            basicCMOperations.add(value);
            // We'll only record a maximum of 10 operations. If we exceed the
            // max, remove the oldest.
            if (basicCMOperations.size() > NUM_OPERATIONS_TO_RECORD) {
                basicCMOperations.remove(0);
            }
        }
    }


    public ObjectName getBeanName() throws MalformedObjectNameException, NullPointerException {
        // Construct the ObjectName for the MBean we will register
        return new ObjectName(MX_BEAN_NAME);
    }

    /**
     * Register the bean.
     */
    protected void registerMxBean() {
        try {
            ManagementFactory.getPlatformMBeanServer().registerMBean(this, this.getBeanName());
        } catch (Exception e) {
            SoapIRPLogger.exception(CLASS_NAME, "Constructor", "Failed to register BulkCMIRP_MxBean management beans.", e);
        }
    }
    protected void unregisterMxBean() {
        try {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(this.getBeanName());
        } catch (Exception e) {
            // Suppress exceptions, we're closing down anyway.
        }
    }    
}

1 个答案:

答案 0 :(得分:0)

孤独者;

一旦构造它,您的MXBean应该在JConsole中可见,因为构造函数调用 registerMxBean(),它将MBean注册到平台MBeanServer。

假设您的JConsole在注册MBean时已经在运行,JConsole的MBean视图将由注册时生成的MBeanInfo的第一个实例决定。由于您将其编码为 DynamicMBean ,我想知道您的意图是定期修改MBeanInfo,但请注意,一旦JConsole连接检索到已注册bean的MBeanInfo,它就不会刷新它。如果MBeanInfo更改,则必须在JConsole中关闭连接并再次打开它。

因此,您的问题可能在您提供的代码之外,或者更简单地说,是什么时候创建 BasicCMIRP_MxBean 的实例?

关于在通知开始到达之前没有看到bean的评论让我想知道你的意思是: A.在通知开始到达之前,您无法看到MBean,在这种情况下,我会假设有一些激活代码在[第一个?]通知到达时创建实例,或者 B.您的意思是,在通知开始到达之前,您无法看到MBean的特定属性。

也许你可以在这里澄清更大的图片。

可能不相关的一些其他观察结果:

  1. 您正在生成的MBeanInfo指定唯一的操作名为 getBasicCMInstrumentation (基本),但 invoke 处理程序仅尝试解码名为的操作getBulkCMInstrumentation (批量)。 JConsole提供MBeanInfo以呈现操作,因此控制台认为该操作被称为 basic ,但MBean将仅响应名为 bulk 的请求。
  2. 方法 getBasicCMInstrumentation 的签名会返回对象,但代码确实返回的是 String [] 。这可能有效,因为您的MBeanInfo指定返回 String [] ,但为了保持一致性和清晰度,我将更改方法签名。
  3. //尼古拉斯