0

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.
        }
    }    
}
4

1 回答 1

0

孤独者;

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

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

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

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

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

一些可能无关的额外观察结果:

  1. 您正在生成的 MBeanInfo 指定唯一的操作称为getBasicCMInstrumentation(基本),但您的调用处理程序仅尝试解码称为getBulkCMInstrumentation(批量)的操作。为 JConsole 提供了 MBeanInfo 以呈现操作,因此控制台认为该操作称为基本操作,但 MBean 只会响应称为bulk的请求。
  2. 方法getBasicCMInstrumentation的签名返回一个Object但代码真正返回的是一个String[]。这可能会起作用,因为您的 MBeanInfo 指定返回String[],但为了保持一致和清晰,我会更改方法签名。

//尼古拉斯

于 2012-04-17T12:51:05.220 回答