Search in sources :

Example 51 with NotificationListener

use of javax.management.NotificationListener in project graylog2-server by Graylog2.

the class GarbageCollectionWarningThread method doRun.

@Override
public void doRun() {
    for (final GarbageCollectorMXBean gc : garbageCollectors) {
        switch(gc.getName()) {
            case "ParNew":
            case "ConcurrentMarkSweep":
                LOG.debug("Skipping GC warning listener for concurrent collector {}.", gc.getName());
                continue;
        }
        LOG.debug("Installing GC warning listener for collector {}, total runtime threshold is {}.", gc.getName(), gcWarningThreshold);
        final NotificationEmitter emitter = (NotificationEmitter) gc;
        final NotificationListener listener = new NotificationListener() {

            @SuppressForbidden("Deliberate usage of GcInfo and GarbageCollectionNotificationInfo")
            @Override
            public void handleNotification(javax.management.Notification notification, Object handback) {
                if (GarbageCollectionNotificationInfo.GARBAGE_COLLECTION_NOTIFICATION.equals(notification.getType())) {
                    final GcInfo gcInfo = GarbageCollectionNotificationInfo.from((CompositeData) notification.getUserData()).getGcInfo();
                    final Duration duration = Duration.milliseconds(gcInfo.getDuration());
                    if (duration.compareTo(gcWarningThreshold) > 0) {
                        LOG.warn("Last GC run with {} took longer than {} (last duration={})", gc.getName(), gcWarningThreshold, duration);
                        final Notification systemNotification = notificationService.buildNow().addNode(nodeId.toString()).addTimestamp(Tools.nowUTC()).addSeverity(Notification.Severity.URGENT).addType(Notification.Type.GC_TOO_LONG).addDetail("gc_name", gc.getName()).addDetail("gc_duration_ms", duration.toMilliseconds()).addDetail("gc_threshold_ms", gcWarningThreshold.toMilliseconds()).addDetail("gc_collection_count", gc.getCollectionCount()).addDetail("gc_collection_time", gc.getCollectionTime());
                        if (!notificationService.publishIfFirst(systemNotification)) {
                            LOG.debug("Couldn't publish notification: {}", notification);
                        }
                    }
                }
            }
        };
        emitter.addNotificationListener(listener, null, null);
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) GcInfo(com.sun.management.GcInfo) CompositeData(javax.management.openmbean.CompositeData) GarbageCollectorMXBean(java.lang.management.GarbageCollectorMXBean) Duration(com.github.joschi.jadconfig.util.Duration) Notification(org.graylog2.notifications.Notification) NotificationListener(javax.management.NotificationListener)

Aggregations

NotificationListener (javax.management.NotificationListener)51 Notification (javax.management.Notification)35 ObjectName (javax.management.ObjectName)17 MBeanServer (javax.management.MBeanServer)10 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