/** * {@inheritDoc} */ @Override public void mbeanServerAdded(MBeanServer server) { // if null or already registered do nothing if ((null == server) || activeServerMap.containsKey(server)) { return; } try { // create holder MBeanServerHolder holder = new MBeanServerHolder(server); // register listener try { server.addNotificationListener(new ObjectName(MBEAN_SERVER_DELEGATE_NAME), holder, NOTIFICATION_FILTER, null); } catch (Exception e) { log.warn("Failed to add notification listener to the MBean server " + server.toString() + ". New added beans/attributes will not be monitored.", e); } // register already existing beans registerMBeans(holder, null); // add to map if it all works activeServerMap.put(server, holder); } catch (Throwable t) { // NOPMD // catching throwable if anything goes wrong log.warn("Unable to add the MBean server " + server.toString() + ".", t); } }
@Test public void wrongNotification() throws Exception { jmxSensor.mbeanServerAdded(mBeanServer); ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null)); Notification notification = mock(Notification.class); when(connection.isConnected()).thenReturn(true); notificationListener.getValue().handleNotification(notification, null); verifyZeroInteractions(mBeanServer); } }
MBeanServerNotification notification = new MBeanServerNotification(MBeanServerNotification.REGISTRATION_NOTIFICATION, this, 1, objectName); notificationListener.getValue().handleNotification(notification, null); jmxSensor.update(coreService);
ArgumentCaptor<MBeanServerHolder> notificationListener = ArgumentCaptor.forClass(MBeanServerHolder.class); verify(mBeanServer).addNotificationListener(Matchers.<ObjectName> any(), notificationListener.capture(), Matchers.<NotificationFilter> any(), eq(null)); notificationListener.getValue().handleNotification(notification, null); jmxSensor.update(coreService);