Search in sources :

Example 26 with NotificationListener

use of javax.management.NotificationListener in project intellij-community by JetBrains.

the class PerformanceWatcher method watchCodeCache.

private void watchCodeCache(final MemoryPoolMXBean bean) {
    final long threshold = bean.getUsage().getMax() - 5 * 1024 * 1024;
    if (!bean.isUsageThresholdSupported() || threshold <= 0)
        return;
    bean.setUsageThreshold(threshold);
    final NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
    emitter.addNotificationListener(new NotificationListener() {

        @Override
        public void handleNotification(Notification n, Object hb) {
            if (bean.getUsage().getUsed() > threshold) {
                LOG.info("Code Cache is almost full");
                dumpThreads("codeCacheFull", true);
                try {
                    emitter.removeNotificationListener(this);
                } catch (ListenerNotFoundException e) {
                    LOG.error(e);
                }
            }
        }
    }, null, null);
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Example 27 with NotificationListener

use of javax.management.NotificationListener in project geode by apache.

the class MBeanUtil method registerServerNotificationListener.

static void registerServerNotificationListener() {
    if (mbeanServer == null) {
        return;
    }
    try {
        // the MBeanServerDelegate name is spec'ed as the following...
        ObjectName delegate = ObjectName.getInstance("JMImplementation:type=MBeanServerDelegate");
        mbeanServer.addNotificationListener(delegate, new NotificationListener() {

            public void handleNotification(Notification notification, Object handback) {
                MBeanServerNotification serverNotification = (MBeanServerNotification) notification;
                if (MBeanServerNotification.UNREGISTRATION_NOTIFICATION.equals(serverNotification.getType())) {
                    ObjectName objectName = serverNotification.getMBeanName();
                    synchronized (MBeanUtil.managedResources) {
                        Object entry = MBeanUtil.managedResources.get(objectName);
                        if (entry == null)
                            return;
                        if (!(entry instanceof ManagedResource)) {
                            throw new ClassCastException(LocalizedStrings.MBeanUtil_0_IS_NOT_A_MANAGEDRESOURCE.toLocalizedString(new Object[] { entry.getClass().getName() }));
                        }
                        ManagedResource resource = (ManagedResource) entry;
                        {
                            // call cleanup on managedResource
                            cleanupResource(resource);
                        }
                    }
                }
            }
        }, null, null);
    } catch (JMException e) {
        logStackTrace(Level.WARN, e, LocalizedStrings.MBeanUtil_FAILED_TO_REGISTER_SERVERNOTIFICATIONLISTENER.toLocalizedString());
    } catch (JMRuntimeException e) {
        logStackTrace(Level.WARN, e, LocalizedStrings.MBeanUtil_FAILED_TO_REGISTER_SERVERNOTIFICATIONLISTENER.toLocalizedString());
    }
}
Also used : MBeanServerNotification(javax.management.MBeanServerNotification) JMException(javax.management.JMException) JMRuntimeException(javax.management.JMRuntimeException) Notification(javax.management.Notification) MBeanServerNotification(javax.management.MBeanServerNotification) ObjectName(javax.management.ObjectName) NotificationListener(javax.management.NotificationListener)

Example 28 with NotificationListener

use of javax.management.NotificationListener in project geode by apache.

the class DistributedSystemDUnitTest method testNotificationHub.

@Test
public void testNotificationHub() throws Exception {
    this.managementTestRule.createMembers();
    this.managementTestRule.createManagers();
    class NotificationHubTestListener implements NotificationListener {

        @Override
        public synchronized void handleNotification(Notification notification, Object handback) {
            logger.info("Notification received {}", notification);
            notifications.add(notification);
        }
    }
    this.managerVM.invoke("addListenerToMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            getPlatformMBeanServer().addNotificationListener(objectName, listener, null, null);
            notificationListenerMap.put(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCount", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerMap = notificationHub.getListenerObjectMap();
            assertThat(listenerMap.keySet()).hasSize(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerMap.get(memberMBeanName);
            /*
         * Counter of listener should be 2 . One for default Listener which is added for each member
         * mbean by distributed system mbean One for the added listener in test
         */
            assertThat(listener.getNumCounter()).isEqualTo(2);
            // Raise some notifications
            NotificationBroadcasterSupport notifier = (MemberMBean) service.getMemberMXBean();
            String memberSource = getMemberNameOrId(this.managementTestRule.getDistributedMember());
            // Only a dummy notification , no actual region is created
            Notification notification = new Notification(JMXNotificationType.REGION_CREATED, memberSource, SequenceNumber.next(), System.currentTimeMillis(), ManagementConstants.REGION_CREATED_PREFIX + "/test");
            notifier.sendNotification(notification);
        });
    }
    this.managerVM.invoke("checkNotificationsAndRemoveListeners", () -> {
        await().until(() -> assertThat(notifications).hasSize(3));
        notifications.clear();
        for (ObjectName objectName : notificationListenerMap.keySet()) {
            NotificationListener listener = notificationListenerMap.get(objectName);
            getPlatformMBeanServer().removeNotificationListener(objectName, listener);
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("checkNotificationHubListenerCountAgain", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub hub = service.getNotificationHub();
            Map<ObjectName, NotificationHubListener> listenerObjectMap = hub.getListenerObjectMap();
            assertThat(listenerObjectMap.keySet().size()).isEqualTo(1);
            ObjectName memberMBeanName = getMemberMBeanName(this.managementTestRule.getDistributedMember());
            NotificationHubListener listener = listenerObjectMap.get(memberMBeanName);
            /*
         * Counter of listener should be 1 for the default Listener which is added for each member
         * mbean by distributed system mbean.
         */
            assertThat(listener.getNumCounter()).isEqualTo(1);
        });
    }
    this.managerVM.invoke("removeListenerFromMemberMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        DistributedSystemMXBean distributedSystemMXBean = service.getDistributedSystemMXBean();
        await().until(() -> assertThat(distributedSystemMXBean.listMemberObjectNames()).hasSize(5));
        for (ObjectName objectName : distributedSystemMXBean.listMemberObjectNames()) {
            NotificationHubTestListener listener = new NotificationHubTestListener();
            try {
                // because new
                getPlatformMBeanServer().removeNotificationListener(objectName, listener);
            // instance!!
            } catch (ListenerNotFoundException e) {
            // TODO: [old] apparently there is never a notification listener on any these mbeans at
            // this point [fix this]
            // fix this test so it doesn't hit these unexpected exceptions -- getLogWriter().error(e);
            }
        }
    });
    for (VM memberVM : this.memberVMs) {
        memberVM.invoke("verifyNotificationHubListenersWereRemoved", () -> {
            SystemManagementService service = this.managementTestRule.getSystemManagementService();
            NotificationHub notificationHub = service.getNotificationHub();
            notificationHub.cleanUpListeners();
            assertThat(notificationHub.getListenerObjectMap()).isEmpty();
            for (ObjectName objectName : notificationListenerMap.keySet()) {
                NotificationListener listener = notificationListenerMap.get(objectName);
                assertThatThrownBy(() -> getPlatformMBeanServer().removeNotificationListener(objectName, listener)).isExactlyInstanceOf(ListenerNotFoundException.class);
            }
        });
    }
}
Also used : Notification(javax.management.Notification) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) ObjectName(javax.management.ObjectName) NotificationHubListener(org.apache.geode.management.internal.NotificationHub.NotificationHubListener) SystemManagementService(org.apache.geode.management.internal.SystemManagementService) VM(org.apache.geode.test.dunit.VM) MemberMBean(org.apache.geode.management.internal.beans.MemberMBean) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationBroadcasterSupport(javax.management.NotificationBroadcasterSupport) NotificationHub(org.apache.geode.management.internal.NotificationHub) NotificationListener(javax.management.NotificationListener) Test(org.junit.Test) DistributedTest(org.apache.geode.test.junit.categories.DistributedTest)

Example 29 with NotificationListener

use of javax.management.NotificationListener in project geode by apache.

the class CacheManagementDUnitTest method attachListenerToDistributedSystemMXBean.

private void attachListenerToDistributedSystemMXBean(final VM managerVM) {
    managerVM.invoke("attachListenerToDistributedSystemMXBean", () -> {
        ManagementService service = this.managementTestRule.getManagementService();
        assertThat(service.isManager()).isTrue();
        NotificationListener listener = (final Notification notification, final Object handback) -> {
            if (notification.getType().equals(JMXNotificationType.REGION_CREATED)) {
                notifications.add(notification);
            }
        };
        ManagementFactory.getPlatformMBeanServer().addNotificationListener(MBeanJMXAdapter.getDistributedSystemName(), listener, null, null);
    });
}
Also used : SystemManagementService(org.apache.geode.management.internal.SystemManagementService) Notification(javax.management.Notification) NotificationListener(javax.management.NotificationListener)

Example 30 with NotificationListener

use of javax.management.NotificationListener in project jdk8u_jdk by JetBrains.

the class ScanManagerTest method doTestOperation.

/**
     * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
     */
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
    System.out.println("doTestOperation: " + testName);
    final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
    NotificationListener listener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            try {
                queue.put(notification);
            } catch (Exception x) {
                System.err.println("Failed to queue notif: " + x);
            }
        }
    };
    NotificationFilter filter = null;
    Object handback = null;
    final ScanState before;
    final NotificationEmitter emitter = (NotificationEmitter) proxy;
    emitter.addNotificationListener(listener, filter, handback);
    before = proxy.getState();
    op.call();
    try {
        final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
        assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
        assertEquals(AttributeChangeNotification.class, notification.getClass());
        assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
        AttributeChangeNotification acn = (AttributeChangeNotification) notification;
        assertEquals("State", acn.getAttributeName());
        assertEquals(ScanState.class.getName(), acn.getAttributeType());
        assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
        assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
        emitter.removeNotificationListener(listener, filter, handback);
    } finally {
        try {
            op.cancel();
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) JMException(javax.management.JMException) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationListener(javax.management.NotificationListener)

Aggregations

NotificationListener (javax.management.NotificationListener)52 Notification (javax.management.Notification)36 ObjectName (javax.management.ObjectName)18 MBeanServer (javax.management.MBeanServer)11 NotificationFilter (javax.management.NotificationFilter)10 JMXConnector (javax.management.remote.JMXConnector)10 JMXServiceURL (javax.management.remote.JMXServiceURL)10 AttributeChangeNotification (javax.management.AttributeChangeNotification)9 MBeanServerConnection (javax.management.MBeanServerConnection)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 NotificationEmitter (javax.management.NotificationEmitter)8 JMXConnectorServer (javax.management.remote.JMXConnectorServer)8 Attribute (javax.management.Attribute)7 Test (org.junit.Test)6 JMXPluggableAuthenticator (com.sun.jmx.remote.security.JMXPluggableAuthenticator)5 RemoteException (java.rmi.RemoteException)5 LocateRegistry (java.rmi.registry.LocateRegistry)5 Registry (java.rmi.registry.Registry)5 Properties (java.util.Properties)5