Search in sources :

Example 1 with CounterMonitorMBean

use of javax.management.monitor.CounterMonitorMBean in project jdk8u_jdk by JetBrains.

the class CounterMonitorInitThresholdTest method runTest.

public static void runTest() throws Exception {
    // Retrieve the platform MBean server
    //
    System.out.println("\nRetrieve the platform MBean server");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = mbs.getDefaultDomain();
    // Create and register TestMBeans
    //
    ObjectName name1 = new ObjectName(domain + ":type=" + Test.class.getName() + ",name=1");
    mbs.createMBean(Test.class.getName(), name1);
    TestMBean mbean1 = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name1, TestMBean.class, false);
    ObjectName name2 = new ObjectName(domain + ":type=" + Test.class.getName() + ",name=2");
    mbs.createMBean(Test.class.getName(), name2);
    TestMBean mbean2 = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name2, TestMBean.class, false);
    // Create and register CounterMonitorMBean
    //
    ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName());
    CounterMonitor m = new CounterMonitor();
    mbs.registerMBean(m, cmn);
    CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, cmn, CounterMonitorMBean.class, true);
    ((NotificationEmitter) cm).addNotificationListener(new Listener(), null, null);
    cm.setObservedAttribute("Counter");
    cm.setGranularityPeriod(100);
    cm.setInitThreshold(3);
    cm.setNotify(true);
    // Add observed object name1
    //
    System.out.println("\nObservedObject \"" + name1 + "\" registered before starting the monitor");
    cm.addObservedObject(name1);
    // Start the monitor
    //
    System.out.println("\nStart monitoring...");
    cm.start();
    // Play with counter for name1
    //
    System.out.println("\nTest ObservedObject \"" + name1 + "\"");
    for (int i = 0; i < 4; i++) {
        mbean1.setCounter(i);
        System.out.println("\nCounter = " + mbean1.getCounter());
        Thread.sleep(300);
        Number thresholdValue = cm.getThreshold(name1);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != 3) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = 3");
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
        Thread.sleep(300);
    }
    // Add observed object name2
    //
    System.out.println("\nObservedObject \"" + name2 + "\" registered after starting the monitor");
    cm.addObservedObject(name2);
    // Play with counter for name2
    //
    System.out.println("\nTest ObservedObject \"" + name2 + "\"");
    for (int i = 0; i < 4; i++) {
        mbean2.setCounter(i);
        System.out.println("\nCounter = " + mbean2.getCounter());
        Thread.sleep(300);
        Number thresholdValue = cm.getThreshold(name2);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != 3) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = 3");
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
        Thread.sleep(300);
    }
    // Stop the monitor
    //
    System.out.println("\nStop monitoring...");
    cm.stop();
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) NotificationListener(javax.management.NotificationListener) CounterMonitorMBean(javax.management.monitor.CounterMonitorMBean) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 2 with CounterMonitorMBean

use of javax.management.monitor.CounterMonitorMBean in project jdk8u_jdk by JetBrains.

the class CounterMonitorThresholdTest method runTest.

public static void runTest(int offset, int[] counter, int[] derivedGauge, int[] threshold) throws Exception {
    // Retrieve the platform MBean server
    //
    System.out.println("\nRetrieve the platform MBean server");
    MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    String domain = mbs.getDefaultDomain();
    // Create and register TestMBean
    //
    ObjectName name = new ObjectName(domain + ":type=" + Test.class.getName() + ",offset=" + offset);
    mbs.createMBean(Test.class.getName(), name);
    TestMBean mbean = (TestMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, name, TestMBean.class, false);
    // Create and register CounterMonitorMBean
    //
    ObjectName cmn = new ObjectName(domain + ":type=" + CounterMonitor.class.getName() + ",offset=" + offset);
    CounterMonitor m = new CounterMonitor();
    mbs.registerMBean(m, cmn);
    CounterMonitorMBean cm = (CounterMonitorMBean) MBeanServerInvocationHandler.newProxyInstance(mbs, cmn, CounterMonitorMBean.class, true);
    ((NotificationEmitter) cm).addNotificationListener(new Listener(), null, null);
    cm.addObservedObject(name);
    cm.setObservedAttribute("Counter");
    cm.setGranularityPeriod(100);
    cm.setInitThreshold(1);
    cm.setOffset(offset);
    cm.setModulus(5);
    cm.setNotify(true);
    // Start the monitor
    //
    System.out.println("\nStart monitoring...");
    cm.start();
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // see 8025207
        do {
            Thread.sleep(150);
            derivedGaugeValue = (Integer) cm.getDerivedGauge(name);
        } while (derivedGaugeValue.intValue() != derivedGauge[i]);
        Number thresholdValue = cm.getThreshold(name);
        System.out.println("Threshold = " + thresholdValue);
        if (thresholdValue.intValue() != threshold[i]) {
            System.out.println("Wrong threshold! Current value = " + thresholdValue + " Expected value = " + threshold[i]);
            System.out.println("\nStop monitoring...");
            cm.stop();
            throw new IllegalArgumentException("wrong threshold");
        }
    }
    // Stop the monitor
    //
    System.out.println("\nStop monitoring...");
    cm.stop();
}
Also used : NotificationListener(javax.management.NotificationListener) ObjectName(javax.management.ObjectName) NotificationEmitter(javax.management.NotificationEmitter) CounterMonitorMBean(javax.management.monitor.CounterMonitorMBean) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Aggregations

MBeanServer (javax.management.MBeanServer)2 NotificationEmitter (javax.management.NotificationEmitter)2 NotificationListener (javax.management.NotificationListener)2 ObjectName (javax.management.ObjectName)2 CounterMonitor (javax.management.monitor.CounterMonitor)2 CounterMonitorMBean (javax.management.monitor.CounterMonitorMBean)2