Discover New Registered MBeans

I am using the MBeans platform server in Java 1.6 running in an OSGi container.

Using MBeans for statistical counters and events mainly. Their implementation is in one set, but they are created in several other packages. Each MBean autoregisters itself with the MBean platform server.

The problem is that when I join through JMX and request MBeans, I get only those that are currently registered and they will not be registered until they are created (or because static classes do not exist until first access, or because the package is not running yet, or the counter is deep in some logic that does not exist before the first use)

I need some way to subscribe to the "register" events on the MBeans server. Or some other way to determine when new MBeans are added to the server. Detecting deleted MBeans will be an added bonus, but not required.

The only solution I have is basically a thread that polls the server every 5 seconds and compares the result with the saved MBeans list, and this is pretty ugly.

+6
java jmx mbeans
source share
2 answers

All compatible MBeanServers will notify listeners of MBean registration and deregistration events. The key is to register the notification listener with MBeanServerDelegate.

For example, 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() + "]"); } } } 

To register a listener, add a notification listener to MBeanServerDelegate . You can use MBeanServerNotificationFilter if you want to filter which MBeans you are really notified about. In this example, a filter is enabled for all object names.

  // 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); 

Your listener implementation will receive a callback each time an MBean is registered or unregistered.

+12
source share

It is like you want to see all MBeans that exist anywhere, but you cannot, because the code does not create them all at once.

I suggest using a code generator that creates an โ€œappearanceโ€ of MBeans for all real MBeans. For example, use the class or marker interface to search for MBeans. The beans view must be created at startup.

Now that a real MBean appears, he needs to look at his presentation and hook himself.

Thus, all MBeans will always be visible, the launch will not suffer much (since the presentation of MBeans will be really cheap), and the view of MBeans can tell you the status of real MBeans.

[EDIT] If you really need to know when a new MBean is registered, expand the existing MBeanServer and override registerMBean(Object, ObjectName) . Install the new MBeanServer by setting the System property javax.management.builder.initial .

Now identify the MBean that provides this information.

+3
source share

All Articles