Search in sources :

Example 11 with NotificationEmitter

use of javax.management.NotificationEmitter 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)

Example 12 with NotificationEmitter

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

the class TestUtils method makeNotificationEmitter.

/**
     * Transfroms a proxy implementing T in a proxy implementing T plus
     * NotificationEmitter
     *
     **/
public static <T> T makeNotificationEmitter(T proxy, Class<T> mbeanInterface) {
    if (proxy instanceof NotificationEmitter)
        return proxy;
    if (proxy == null)
        return null;
    if (!(proxy instanceof Proxy))
        throw new IllegalArgumentException("not a " + Proxy.class.getName());
    final Proxy p = (Proxy) proxy;
    final InvocationHandler handler = Proxy.getInvocationHandler(proxy);
    if (!(handler instanceof MBeanServerInvocationHandler))
        throw new IllegalArgumentException("not a JMX Proxy");
    final MBeanServerInvocationHandler h = (MBeanServerInvocationHandler) handler;
    final ObjectName name = h.getObjectName();
    final MBeanServerConnection mbs = h.getMBeanServerConnection();
    final boolean isMXBean = h.isMXBean();
    final T newProxy;
    if (isMXBean)
        newProxy = JMX.newMXBeanProxy(mbs, name, mbeanInterface, true);
    else
        newProxy = JMX.newMBeanProxy(mbs, name, mbeanInterface, true);
    return newProxy;
}
Also used : Proxy(java.lang.reflect.Proxy) NotificationEmitter(javax.management.NotificationEmitter) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) MBeanServerInvocationHandler(javax.management.MBeanServerInvocationHandler) InvocationHandler(java.lang.reflect.InvocationHandler) MBeanServerConnection(javax.management.MBeanServerConnection) ObjectName(javax.management.ObjectName)

Example 13 with NotificationEmitter

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

the class DirectoryScannerTest method doTestOperation.

private void doTestOperation(DirectoryScannerMXBean 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) makeNotificationEmitter(proxy, DirectoryScannerMXBean.class);
    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(getObjectName(proxy), 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) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) NotificationListener(javax.management.NotificationListener)

Example 14 with NotificationEmitter

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

the class HeapMemoryMonitor method stopMonitoring.

/**
   * Stops all three mechanisms from monitoring heap usage.
   */
@Override
public void stopMonitoring() {
    synchronized (this) {
        if (!this.started) {
            return;
        }
        // Stop the poller
        this.resourceManager.stopExecutor(this.pollerExecutor);
        // Stop the JVM threshold listener
        NotificationEmitter emitter = (NotificationEmitter) ManagementFactory.getMemoryMXBean();
        try {
            emitter.removeNotificationListener(this, null, null);
            this.cache.getLoggerI18n().fine("Removed Memory MXBean notification listener" + this);
        } catch (ListenerNotFoundException ignore) {
            logger.debug("This instance '{}' was not registered as a Memory MXBean listener", this);
        }
        // Stop the stats listener
        final GemFireStatSampler sampler = this.cache.getInternalDistributedSystem().getStatSampler();
        if (sampler != null) {
            sampler.removeLocalStatListener(this.statListener);
        }
        this.started = false;
    }
}
Also used : GemFireStatSampler(org.apache.geode.internal.statistics.GemFireStatSampler) NotificationEmitter(javax.management.NotificationEmitter) ListenerNotFoundException(javax.management.ListenerNotFoundException)

Example 15 with NotificationEmitter

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

the class MXMemoryPoolListenerExample method main.

public static void main(String[] args) {
    final MemoryMXBean mbean = ManagementFactory.getMemoryMXBean();
    final double threshold;
    {
        double t = 0.8;
        if (args.length > 0) {
            try {
                t = Integer.parseInt(args[0]) / 100;
            } catch (NumberFormatException useDefault) {
            }
        }
        if (t < 0.0 || t > 1.0) {
            throw new IllegalArgumentException("Theshold must be >= 0 and <= 100");
        }
        threshold = t;
    }
    final int percentTenured;
    {
        int p = 100;
        if (args.length > 1) {
            try {
                p = Integer.parseInt(args[1]);
            } catch (NumberFormatException useDefault) {
            }
        }
        if (p > 100 || p < 0) {
            throw new IllegalArgumentException("Percent Tenured must be >= 0 and <= 100");
        }
        percentTenured = p;
    }
    Properties dsProps = new Properties();
    // Loner
    dsProps.setProperty(MCAST_PORT, "0");
    dsProps.setProperty(ConfigurationProperties.LOG_LEVEL, "info");
    dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLE_RATE, "200");
    dsProps.setProperty(ConfigurationProperties.ENABLE_TIME_STATISTICS, "true");
    dsProps.setProperty(ConfigurationProperties.STATISTIC_SAMPLING_ENABLED, "true");
    DistributedSystem ds = DistributedSystem.connect(dsProps);
    final LogWriter logger = ds.getLogWriter();
    logger.info("Usage threshold: " + threshold + "; percent tenured: " + percentTenured + "; Runtime Maximum memory: " + (Runtime.getRuntime().maxMemory() / (1024 * 1024)) + "Mb" + "; Heap Maximum memory: " + (mbean.getHeapMemoryUsage().getMax() / (1024 * 1024)) + "Mb");
    MXMemoryPoolListenerExample me = new MXMemoryPoolListenerExample(ds);
    // Register this listener to NotificationEmitter
    NotificationEmitter emitter = (NotificationEmitter) mbean;
    emitter.addNotificationListener(me, null, null);
    List<MemoryPoolMXBean> pools = ManagementFactory.getMemoryPoolMXBeans();
    for (MemoryPoolMXBean p : pools) {
        if (p.isCollectionUsageThresholdSupported()) {
            // p.setCollectionUsageThreshold(0);
            logger.info("Pool which supports collection usage threshold: " + p.getName() + "; " + p.getCollectionUsage());
        }
        // On JRockit do not set the usage threshold on the Nursery pool
        if (p.getType().equals(MemoryType.HEAP) && p.isUsageThresholdSupported() && !p.getName().startsWith("Nursery")) {
            int byteThreshold = (int) Math.ceil(threshold * p.getUsage().getMax());
            logger.info("Setting threshold " + (byteThreshold / (1024 * 1024)) + "Mb on: " + p.getName() + "; " + p.getCollectionUsage());
            p.setUsageThreshold(byteThreshold);
        }
    }
    final Cache c = CacheFactory.create(ds);
    new MemoryHog("hog_1", c, me.critical).consumeMemory(percentTenured).printTenuredSize();
    ds.disconnect();
}
Also used : ConfigurationProperties(org.apache.geode.distributed.ConfigurationProperties) Properties(java.util.Properties) DistributedSystem(org.apache.geode.distributed.DistributedSystem) MemoryMXBean(java.lang.management.MemoryMXBean) NotificationEmitter(javax.management.NotificationEmitter) LogWriter(org.apache.geode.LogWriter) MemoryPoolMXBean(java.lang.management.MemoryPoolMXBean)

Aggregations

NotificationEmitter (javax.management.NotificationEmitter)15 NotificationListener (javax.management.NotificationListener)7 ListenerNotFoundException (javax.management.ListenerNotFoundException)5 MemoryPoolMXBean (java.lang.management.MemoryPoolMXBean)4 Notification (javax.management.Notification)3 ObjectName (javax.management.ObjectName)3 ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)2 GarbageCollectorMXBean (java.lang.management.GarbageCollectorMXBean)2 MemoryMXBean (java.lang.management.MemoryMXBean)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 AttributeChangeNotification (javax.management.AttributeChangeNotification)2 MBeanServer (javax.management.MBeanServer)2 NotificationFilter (javax.management.NotificationFilter)2 CounterMonitor (javax.management.monitor.CounterMonitor)2 CounterMonitorMBean (javax.management.monitor.CounterMonitorMBean)2 Duration (com.github.joschi.jadconfig.util.Duration)1 GcInfo (com.sun.management.GcInfo)1 IOException (java.io.IOException)1 InvocationHandler (java.lang.reflect.InvocationHandler)1 Proxy (java.lang.reflect.Proxy)1