java - JDK1.6 中来自 ManagementFactory 的托管 Mbean - NotCompliantMBeanException :

标签 java jboss mbeans java-6

我在 JDK1.5 和 JBOSS 4.X 中使用 ManagementFactory 获取 ManagedMbeans。现在想将我的相同代码移动到 JDK 1.6。 Mbean部分中断抛出异常

Caused by: javax.management.NotCompliantMBeanException: Class does not expose a management interface: java.lang.Object Caused by: java.lang.Exception: Unable to register platform (JVM) MBeans with JBoss MBeanServer

最佳答案

这里有两种方法:

  1. 创建 JBossAS use the platform MBeanServer .
  2. 创建您自己的代理来实现 MXBean 接口(interface)(并在接口(interface)中具有 *MBean 兼容名称)并将调用委托(delegate)给实际的 MXBean。

#2 的示例代码

这是一个 JBoss ServiceMBean 实现,它将跨发布平台代理 MXBeans 到 JBoss MBeanServer。 (为简洁起见删除了导入)。这与我概述的方法略有不同,但精神上是相同的。那里有几个额外的 hoo-has 来管理通知的转发。

package org.helios.jboss.jmx.mxbean;

public class MXBeanPublisher extends ServiceMBeanSupport implements MXBeanPublisherMBean {
    protected final Map<ObjectName, DynamicMBeanNotifEnabled> published = new HashMap<ObjectName, DynamicMBeanNotifEnabled>();
    protected final ExecutorService executor = Executors.newCachedThreadPool();
    public Set<ObjectName> getPublishedNames() {
        return Collections.unmodifiableSet(published.keySet());
    }
    public static interface DynamicMBeanNotifEnabled extends NotificationBroadcaster, NotificationEmitter, DynamicMBean, NotificationListener {

        public void startNotifications() throws InstanceNotFoundException;
        public void stopNotifications() throws InstanceNotFoundException, ListenerNotFoundException;
    }


    protected DynamicMBeanNotifEnabled publishMXBean(final ObjectName on, Class<?> iface) throws InstanceAlreadyExistsException, MBeanRegistrationException, NotCompliantMBeanException, InstanceNotFoundException {
        final MBeanServer platformAgent = ManagementFactory.getPlatformMBeanServer();
        DynamicMBeanNotifEnabled dmb = new DynamicMBeanNotifEnabled() {
            NotificationBroadcasterSupport bcaster = new NotificationBroadcasterSupport(executor);
            public Object getAttribute(String attribute)throws AttributeNotFoundException, MBeanException, ReflectionException {
                try {
                    return platformAgent.getAttribute(on, attribute);
                } catch (InstanceNotFoundException e) {
                    throw new MBeanException(e, "Instance [" + on + "] not found");
                }
            }
            public void setAttribute(Attribute attribute)throws AttributeNotFoundException, InvalidAttributeValueException, MBeanException, ReflectionException {
                try {
                    platformAgent.setAttribute(on, attribute);
                } catch (InstanceNotFoundException e) {
                    throw new MBeanException(e, "Instance [" + on + "] not found");
                }               
            }
            public AttributeList getAttributes(String[] attributes) {
                try {
                    return platformAgent.getAttributes(on, attributes);
                } catch (Exception e) {
                    throw new RuntimeException("Failed to getAttributes on [" + on + "]", e);
                }               
            }
            public AttributeList setAttributes(AttributeList attributes) {
                try {
                    return platformAgent.setAttributes(on, attributes);
                } catch (Exception e) {
                    throw new RuntimeException("Failed to setAttributes on [" + on + "]", e);
                }                               
            }
            public Object invoke(String actionName, Object[] params,String[] signature) throws MBeanException, ReflectionException {
                try {
                    return platformAgent.invoke(on, actionName, params, signature);
                } catch (InstanceNotFoundException e) {
                    throw new MBeanException(e, "Instance [" + on + "] not found");
                }                                   
            }
            public MBeanInfo getMBeanInfo() {
                try {
                    return platformAgent.getMBeanInfo(on);
                } catch (Exception e) {
                    throw new RuntimeException("Failed to get MBeanInfo for Instance [" + on + "]", e);
                }                                               
            }
            public void addNotificationListener(NotificationListener listener,
                    NotificationFilter filter, Object handback) {
                bcaster.addNotificationListener(listener, filter, handback);
            }
            public void removeNotificationListener(NotificationListener listener)
                    throws ListenerNotFoundException {
                bcaster.removeNotificationListener(listener);
            }
            public void removeNotificationListener(
                    NotificationListener listener, NotificationFilter filter,
                    Object handback) throws ListenerNotFoundException {
                bcaster.removeNotificationListener(listener, filter, handback);
            }
            public MBeanNotificationInfo[] getNotificationInfo() {
                return bcaster.getNotificationInfo();
            }

            public void startNotifications() throws InstanceNotFoundException {
                ManagementFactory.getPlatformMBeanServer().addNotificationListener(on, this, null, null);
            }
            public void stopNotifications() throws InstanceNotFoundException, ListenerNotFoundException {
                ManagementFactory.getPlatformMBeanServer().removeNotificationListener(on, this, null, null);
            }

            @Override
            public void handleNotification(Notification notification, Object handback) {
                bcaster.sendNotification(notification);
            }
        }; 
        if(server.isRegistered(on)) return null;
        server.registerMBean(dmb, on);
        published.put(on, dmb);
        dmb.startNotifications();
        return dmb;
    }

    public void startService() throws Exception {
        try { publishMXBean(new ObjectName(ManagementFactory.OPERATING_SYSTEM_MXBEAN_NAME), OperatingSystemMXBean.class); } catch (Exception e) {
            log.error("Failed to publish OperatingSystemMXBean", e);
        };
        try { publishMXBean(new ObjectName(ManagementFactory.CLASS_LOADING_MXBEAN_NAME), ClassLoadingMXBean.class); } catch (Exception e) {
            log.error("Failed to publish ClassLoadingMXBean", e);
        };
        try { publishMXBean(new ObjectName(ManagementFactory.MEMORY_MXBEAN_NAME), MemoryMXBean.class); } catch (Exception e) {
            log.error("Failed to publish MemoryMXBean", e);
        };
        try { publishMXBean(new ObjectName(ManagementFactory.RUNTIME_MXBEAN_NAME), RuntimeMXBean.class); } catch (Exception e) {
            log.error("Failed to publish RuntimeMXBean", e);
        };
        try { publishMXBean(new ObjectName(ManagementFactory.THREAD_MXBEAN_NAME), ThreadMXBean.class); } catch (Exception e) {
            log.error("Failed to publish RuntimeMXBean", e);
        };
        for(GarbageCollectorMXBean gc: ManagementFactory.getGarbageCollectorMXBeans()) {
            try { publishMXBean(new ObjectName(ManagementFactory.GARBAGE_COLLECTOR_MXBEAN_DOMAIN_TYPE + ",name=" + gc.getName()), GarbageCollectorMXBean.class); } catch (Exception e) {
                log.error("Failed to publish GarbageCollectorMXBean[" + gc.getName() + "]", e);
            };          
        }
        for(MemoryManagerMXBean mm: ManagementFactory.getMemoryManagerMXBeans()) {
            try { publishMXBean(new ObjectName(ManagementFactory.MEMORY_MANAGER_MXBEAN_DOMAIN_TYPE + ",name=" + mm.getName()), MemoryManagerMXBean.class); } catch (Exception e) {
            };          
        }
        for(MemoryPoolMXBean mp: ManagementFactory.getMemoryPoolMXBeans()) {
            try { publishMXBean(new ObjectName(ManagementFactory.MEMORY_POOL_MXBEAN_DOMAIN_TYPE + ",name=" + mp.getName()), MemoryPoolMXBean.class); } catch (Exception e) {
                log.error("Failed to publish MemoryPoolMXBean[" + mp.getName() + "]", e);
            };          
        }
    }

    public void stopService() {
        for(Map.Entry<ObjectName, DynamicMBeanNotifEnabled> entry: published.entrySet()) {
            try { server.unregisterMBean(entry.getKey()); } catch (Exception e) {}
            try { entry.getValue().stopNotifications(); } catch (Exception e) {}
        }
        published.clear();
    }
}

界面简单

package org.helios.jboss.jmx.mxbean;
public interface MXBeanPublisherMBean extends ServiceMBean {
    public Set<ObjectName> getPublishedNames();
}

此 MBean 部署描述符将部署服务:

<mbean code="org.helios.jboss.jmx.mxbean.MXBeanPublisher" name="jboss.deployer:service=MXBeanPublisher" />

Platform MXBeans in JBoss

关于java - JDK1.6 中来自 ManagementFactory 的托管 Mbean - NotCompliantMBeanException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5784551/

相关文章:

JavaFX TreeView ChangeListener 旧值始终为 null

jboss - Jboss 中创建了多少个消息驱动 Bean?

java - 访问远程 GarbageCollectorMXBean

java - 如何以编程方式检查 JMX MBean 操作和属性?

java - 创建 RMI 连接器客户端

java - NetBeans:如何启用/禁用 JTabedPane 中的特定选项卡

java - 是否可以使用 jvm.dll 执行 jar 文件。而不是 java.exe?

java - 我正在尝试使用 docx4j 将图像添加到新的 word 文档

ssl - 在 JBoss AS 7 中设置 SSL

java - 如何通过 java httpClient 查找目标 Web 服务器的任何信息?