检测新注册的MBean [英] Detecting newly registered MBeans

查看:96
本文介绍了检测新注册的MBean的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Java 1.6中的平台MBeans服务器,在OSGi容器中运行。

I'm using the platform MBeans server in Java 1.6, running in an OSGi container.

主要使用MBean作为统计计数器和事件。它们的实现在一个包中,但它们在其他几个包中实例化。每个MBean都使用平台MBean服务器自行注册。

Using the MBeans for statistic counters and events mainly. The implementation of them are in one bundle, but they're instantiated in several other bundles. Every MBean autoregisters itself with the platform MBean server.

问题是,当我通过JMX附加并查询MBean时,我只获得当前注册的那些,并且它们在被实例化之前不会被注册(因为静态类在第一次访问之前不存在,或者因为bundle还没有启动,或者计数器在某些逻辑中很深,直到第一次使用才会存在)

The problem is that when I attach via JMX and query for MBeans, I only get the ones that are currently registered, and they wont be registered until they've been instantiated (either because static classes don't exist until first access, or because the bundle hasn't started yet, or the counter is deep in some logic that wont exist until first use)

我需要某种方式订阅MBeans服务器中的注册事件。或者确定何时向服务器添加新MBean的其他方法。检测删除的MBean将是一个额外的好处,但不是必需的。

I need some way of subscribing to "register" events in the MBeans server. Or some other way of determining when there are new MBeans added to the server. Detecting removed MBeans would be an added bonus, but not necessary.

我得到的唯一解决方案基本上是一个每隔5秒轮询一次服务器的线程并比较结果使用已保存的MBean列表,这非常难看。

The only solution I've got is basically a thread that polls the server every 5 seconds and compares the result with a saved list of MBeans, and that is quite ugly.

推荐答案

所有符合条件的MBeanServers都会通知侦听器MBean注册和取消注册事件。关键是在MBeanServerDelegate上注册通知监听器。

All compliant MBeanServers will notify listeners of MBean registration and unregistration events. The key is to register a notification listener on the MBeanServerDelegate.

例如, javax.management.NotificationListener 实现:

public class MBeanEventListener implements NotificationListener {
    public void handleNotification(Notification notification, Object handback) {
        MBeanServerNotification mbs = (MBeanServerNotification) notification;
        if(MBeanServerNotification.REGISTRATION_NOTIFICATION.equals(mbs.getType())) {
            log("MBean Registered [" + mbs.getMBeanName() + "]");
        } else if(MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(mbs.getType())) {
            log("MBean Unregistered [" + mbs.getMBeanName() + "]");
        }
    }       
}

要注册监听器,请添加针对 MBeanServerDelegate 的通知侦听器。如果要筛选哪些MBean,可以使用 MBeanServerNotificationFilter 你实际上得到了通知。在此示例中,为所有ObjectNames启用了过滤器。

To register the listener, add the notification listener against the MBeanServerDelegate. You can use an MBeanServerNotificationFilter if you want to filter which MBeans you are actually notified about. In this example, the filter is enabled for all ObjectNames.

    // Get a reference to the target MBeanServer
    MBeanServerConnection server = ManagementFactory.getPlatformMBeanServer();
    MBeanServerNotificationFilter filter = new MBeanServerNotificationFilter();
    filter.enableAllObjectNames();
    server.addNotificationListener(MBeanServerDelegate.DELEGATE_NAME, new MBeanEventListener(), filter, null);

每次MBean注册或取消注册时,您的侦听器实现都会收到回调。

Your listener implementation will get a callback every time an MBean is registered or unregistered.

这篇关于检测新注册的MBean的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆