Search in sources :

Example 6 with CounterMonitor

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

the class ThreadPoolTest method runTest.

/**
     * Run test
     */
public int runTest(int monitorType) throws Exception {
    ObjectName[] mbeanNames = new ObjectName[nTasks];
    ObservedObject[] monitored = new ObservedObject[nTasks];
    ObjectName[] monitorNames = new ObjectName[nTasks];
    Monitor[] monitor = new Monitor[nTasks];
    String[] attributes = { "Integer", "Double", "String" };
    try {
        echo(">>> CREATE MBeanServer");
        MBeanServer server = MBeanServerFactory.newMBeanServer();
        String domain = server.getDefaultDomain();
        for (int i = 0; i < nTasks; i++) {
            mbeanNames[i] = new ObjectName(":type=ObservedObject,instance=" + (i + 1));
            monitored[i] = new ObservedObject();
            echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
            server.registerMBean(monitored[i], mbeanNames[i]);
            switch(monitorType) {
                case 1:
                    monitorNames[i] = new ObjectName(":type=CounterMonitor," + "instance=" + (i + 1));
                    monitor[i] = new CounterMonitor();
                    break;
                case 2:
                    monitorNames[i] = new ObjectName(":type=GaugeMonitor," + "instance=" + (i + 1));
                    monitor[i] = new GaugeMonitor();
                    break;
                case 3:
                    monitorNames[i] = new ObjectName(":type=StringMonitor," + "instance=" + (i + 1));
                    monitor[i] = new StringMonitor();
                    break;
                default:
                    echo("Unsupported monitor type");
                    return 1;
            }
            echo(">>> CREATE Monitor = " + monitorNames[i].toString());
            server.registerMBean(monitor[i], monitorNames[i]);
            monitor[i].addObservedObject(mbeanNames[i]);
            monitor[i].setObservedAttribute(attributes[monitorType - 1]);
            monitor[i].setGranularityPeriod(50);
            monitor[i].start();
        }
        if (!waiter.waiting(MAX_WAITING_TIME)) {
            echo("Error, not all " + nTasks + " monitor tasks are called after " + MAX_WAITING_TIME);
            return 1;
        }
    } finally {
        for (int i = 0; i < nTasks; i++) if (monitor[i] != null)
            monitor[i].stop();
    }
    echo("All " + nTasks + " monitors are called.");
    return 0;
}
Also used : GaugeMonitor(javax.management.monitor.GaugeMonitor) ObjectName(javax.management.ObjectName) StringMonitor(javax.management.monitor.StringMonitor) StringMonitor(javax.management.monitor.StringMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) Monitor(javax.management.monitor.Monitor) GaugeMonitor(javax.management.monitor.GaugeMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Example 7 with CounterMonitor

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

the class AttributeArbitraryDataTypeTest method counterMonitorNotification.

/**
     * Update the counter and check for notifications
     */
public int counterMonitorNotification(int testCase) throws Exception {
    counterMessageReceived = false;
    CounterMonitor counterMonitor = null;
    try {
        MBeanServer server = MBeanServerFactory.newMBeanServer();
        String domain = server.getDefaultDomain();
        // Create a new CounterMonitor MBean and add it to the MBeanServer.
        //
        echo(">>> CREATE a new CounterMonitor MBean");
        ObjectName counterMonitorName = new ObjectName(domain + ":type=" + CounterMonitor.class.getName());
        counterMonitor = new CounterMonitor();
        server.registerMBean(counterMonitor, counterMonitorName);
        echo(">>> ADD a listener to the CounterMonitor");
        counterMonitor.addNotificationListener(this, null, null);
        //
        // MANAGEMENT OF A STANDARD MBEAN
        //
        echo(">>> CREATE a new ObservedObject MBean");
        ObjectName obsObjName = ObjectName.getInstance(domain + ":type=ObservedObject");
        ObservedObject obsObj = new ObservedObject();
        ComplexAttribute ca = new ComplexAttribute();
        switch(testCase) {
            case 1:
                obsObj.ia = 0;
                break;
            case 2:
                ca.setIntegerAttribute(0);
                obsObj.setComplexAttribute(ca);
                break;
            case 3:
                ca.setArrayAttribute(new Integer[0]);
                obsObj.setComplexAttribute(ca);
                break;
        }
        server.registerMBean(obsObj, obsObjName);
        echo(">>> SET the attributes of the CounterMonitor:");
        counterMonitor.addObservedObject(obsObjName);
        echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
        switch(testCase) {
            case 1:
                counterMonitor.setObservedAttribute("CompositeDataAttribute.IntegerAttribute");
                echo("\tATTRIBUTE \"ObservedAttribute\" = " + "CompositeDataAttribute.IntegerAttribute");
                break;
            case 2:
                counterMonitor.setObservedAttribute("ComplexAttribute.integerAttribute");
                echo("\tATTRIBUTE \"ObservedAttribute\" = " + "ComplexAttribute.integerAttribute");
                break;
            case 3:
                counterMonitor.setObservedAttribute("ComplexAttribute.arrayAttribute.length");
                echo("\tATTRIBUTE \"ObservedAttribute\" = " + "ComplexAttribute.arrayAttribute.length");
                break;
        }
        counterMonitor.setNotify(true);
        echo("\tATTRIBUTE \"NotifyFlag\"        = true");
        Integer threshold = 2;
        counterMonitor.setInitThreshold(threshold);
        echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
        int granularityperiod = 500;
        counterMonitor.setGranularityPeriod(granularityperiod);
        echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
        echo(">>> START the CounterMonitor");
        counterMonitor.start();
        // Wait for granularity period (multiplied by 2 for sure)
        //
        Thread.sleep(granularityperiod * 2);
        switch(testCase) {
            case 1:
                obsObj.ia = 1;
                break;
            case 2:
                ca.setIntegerAttribute(1);
                break;
            case 3:
                ca.setArrayAttribute(new Integer[1]);
                break;
        }
        // Wait for granularity period (multiplied by 2 for sure)
        //
        Thread.sleep(granularityperiod * 2);
        switch(testCase) {
            case 1:
                obsObj.ia = 2;
                break;
            case 2:
                ca.setIntegerAttribute(2);
                break;
            case 3:
                ca.setArrayAttribute(new Integer[2]);
                break;
        }
        // Wait for granularity period (multiplied by 2 for sure)
        //
        Thread.sleep(granularityperiod * 2);
        switch(testCase) {
            case 1:
                obsObj.ia = 3;
                break;
            case 2:
                ca.setIntegerAttribute(3);
                break;
            case 3:
                ca.setArrayAttribute(new Integer[3]);
                break;
        }
        //
        synchronized (this) {
            while (!counterMessageReceived) {
                try {
                    wait();
                } catch (InterruptedException e) {
                    System.err.println("Got unexpected exception: " + e);
                    e.printStackTrace();
                    break;
                }
            }
        }
        if (counterMessageReceived) {
            echo("\tOK: CounterMonitor notification received");
        } else {
            echo("\tKO: CounterMonitor notification missed or not emitted");
            return 1;
        }
    } finally {
        if (counterMonitor != null)
            counterMonitor.stop();
    }
    return 0;
}
Also used : CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 8 with CounterMonitor

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

the class DerivedGaugeMonitorTest method test.

public static void test(String attr) throws Exception {
    System.err.println("Testing " + attr);
    final MBeanServer server = MBeanServerFactory.createMBeanServer();
    final ObjectName mbean = new ObjectName("ugly:type=cr.p");
    final My my = new My();
    final GaugeMonitor mon2 = new GaugeMonitor();
    final ObjectName mon2n = new ObjectName("mon1:type=GaugeMonitor");
    final CounterMonitor mon1 = new CounterMonitor();
    final ObjectName mon1n = new ObjectName("mon2:type=CounterMonitor");
    server.registerMBean(my, mbean);
    server.registerMBean(mon1, mon1n);
    server.registerMBean(mon2, mon2n);
    mon1.addObservedObject(mbean);
    // 60 sec...
    mon1.setGranularityPeriod(60000);
    mon1.setObservedAttribute(attr);
    mon1.setDifferenceMode(true);
    check(attr, server, mon1n, mbean);
    mon2.addObservedObject(mbean);
    // 60 sec...
    mon2.setGranularityPeriod(60000);
    mon2.setObservedAttribute(attr);
    mon2.setDifferenceMode(true);
    check(attr, server, mon2n, mbean);
    final long approxStart = System.currentTimeMillis();
    mon1.start();
    mon2.start();
    try {
        check(attr, server, mon1n, mbean, approxStart);
        check(attr, server, mon2n, mbean, approxStart);
        check(attr, server, mon1n, mbean, approxStart);
        check(attr, server, mon2n, mbean, approxStart);
        mon1.setGranularityPeriod(5);
        mon2.setGranularityPeriod(5);
        my.cdl.await(1000, TimeUnit.MILLISECONDS);
        if (my.cdl.getCount() > 0)
            throw new Exception(attr + ": Count down not reached!");
        // just check that we don't get an exception now...
        System.err.println(attr + ": [" + mon1n + "] DerivedGauge is now " + server.getAttribute(mon1n, "DerivedGauge"));
        System.err.println(attr + ": [" + mon2n + "] DerivedGauge is now " + server.getAttribute(mon2n, "DerivedGauge"));
    } finally {
        try {
            mon1.stop();
        } catch (Exception x) {
        }
        try {
            mon2.stop();
        } catch (Exception x) {
        }
    }
}
Also used : GaugeMonitor(javax.management.monitor.GaugeMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer) ObjectName(javax.management.ObjectName)

Example 9 with CounterMonitor

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

the class StartStopTest method runTest.

/**
     * Run test
     */
public int runTest(int monitorType) throws Exception {
    int nTasks = maxPoolSize + 2;
    ObjectName[] mbeanNames = new ObjectName[nTasks];
    ObservedObject[] monitored = new ObservedObject[nTasks];
    ObjectName[] monitorNames = new ObjectName[nTasks];
    Monitor[] monitor = new Monitor[nTasks];
    String[] attributes = { "Integer", "Double", "String" };
    try {
        echo(">>> CREATE MBeanServer");
        MBeanServer server = MBeanServerFactory.newMBeanServer();
        String domain = server.getDefaultDomain();
        for (int i = 0; i < nTasks; i++) {
            mbeanNames[i] = new ObjectName(":type=ObservedObject,instance=" + (i + 1));
            monitored[i] = new ObservedObject();
            echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
            server.registerMBean(monitored[i], mbeanNames[i]);
            switch(monitorType) {
                case 1:
                    monitorNames[i] = new ObjectName(":type=CounterMonitor," + "instance=" + (i + 1));
                    monitor[i] = new CounterMonitor();
                    break;
                case 2:
                    monitorNames[i] = new ObjectName(":type=GaugeMonitor," + "instance=" + (i + 1));
                    monitor[i] = new GaugeMonitor();
                    break;
                case 3:
                    monitorNames[i] = new ObjectName(":type=StringMonitor," + "instance=" + (i + 1));
                    monitor[i] = new StringMonitor();
                    break;
                default:
                    echo("Unsupported monitor type");
                    return 1;
            }
            echo(">>> CREATE Monitor = " + monitorNames[i].toString());
            server.registerMBean(monitor[i], monitorNames[i]);
            monitor[i].addObservedObject(mbeanNames[i]);
            monitor[i].setObservedAttribute(attributes[monitorType - 1]);
            monitor[i].setGranularityPeriod(50);
        }
        for (int j = 0; j < 2; j++) {
            echo(">>> Start MONITORS");
            for (int i = 0; i < nTasks; i++) monitor[i].start();
            echo(">>> MONITORS started");
            doSleep(500);
            echo(">>> Check FLAGS true");
            for (int i = 0; i < nTasks; i++) if (!monitored[i].called) {
                echo("KO: At least one attribute was not called");
                return 1;
            }
            echo(">>> FLAGS checked true");
            echo(">>> Stop MONITORS");
            for (int i = 0; i < nTasks; i++) monitor[i].stop();
            echo(">>> MONITORS stopped");
            doSleep(500);
            echo(">>> Set FLAGS to false");
            for (int i = 0; i < nTasks; i++) monitored[i].called = false;
            echo(">>> FLAGS set to false");
            echo(">>> Check FLAGS remain false");
            for (int i = 0; i < nTasks; i++) if (monitored[i].called) {
                echo("KO: At least one attribute " + "continued to get called");
                return 1;
            }
            echo(">>> FLAGS checked false");
        }
    } finally {
        for (int i = 0; i < nTasks; i++) if (monitor[i] != null)
            monitor[i].stop();
    }
    return 0;
}
Also used : GaugeMonitor(javax.management.monitor.GaugeMonitor) ObjectName(javax.management.ObjectName) StringMonitor(javax.management.monitor.StringMonitor) StringMonitor(javax.management.monitor.StringMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) Monitor(javax.management.monitor.Monitor) GaugeMonitor(javax.management.monitor.GaugeMonitor) CounterMonitor(javax.management.monitor.CounterMonitor) MBeanServer(javax.management.MBeanServer)

Example 10 with CounterMonitor

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

the class RuntimeExceptionTest method counterMonitorNotification.

/**
     * Update the counter and check for notifications
     */
public int counterMonitorNotification() throws Exception {
    CounterMonitor counterMonitor = new CounterMonitor();
    try {
        // Create a new CounterMonitor MBean and add it to the MBeanServer.
        //
        echo(">>> CREATE a new CounterMonitor MBean");
        ObjectName counterMonitorName = new ObjectName(domain + ":type=" + CounterMonitor.class.getName());
        server.registerMBean(counterMonitor, counterMonitorName);
        echo(">>> ADD a listener to the CounterMonitor");
        counterMonitor.addNotificationListener(this, null, null);
        //
        // MANAGEMENT OF A STANDARD MBEAN
        //
        echo(">>> SET the attributes of the CounterMonitor:");
        counterMonitor.addObservedObject(obsObjName);
        echo("\tATTRIBUTE \"ObservedObject\"    = " + obsObjName);
        counterMonitor.setObservedAttribute("IntegerAttribute");
        echo("\tATTRIBUTE \"ObservedAttribute\" = IntegerAttribute");
        counterMonitor.setNotify(false);
        echo("\tATTRIBUTE \"NotifyFlag\"        = false");
        Integer threshold = 2;
        counterMonitor.setInitThreshold(threshold);
        echo("\tATTRIBUTE \"Threshold\"         = " + threshold);
        int granularityperiod = 500;
        counterMonitor.setGranularityPeriod(granularityperiod);
        echo("\tATTRIBUTE \"GranularityPeriod\" = " + granularityperiod);
        echo(">>> START the CounterMonitor");
        counterMonitor.start();
        // Check if notification was received
        //
        doWait();
        if (messageReceived) {
            echo("\tOK: CounterMonitor got RUNTIME_ERROR notification!");
        } else {
            echo("\tKO: CounterMonitor did not get " + "RUNTIME_ERROR notification!");
            return 1;
        }
    } finally {
        messageReceived = false;
        if (counterMonitor != null)
            counterMonitor.stop();
    }
    return 0;
}
Also used : CounterMonitor(javax.management.monitor.CounterMonitor) ObjectName(javax.management.ObjectName)

Aggregations

ObjectName (javax.management.ObjectName)10 CounterMonitor (javax.management.monitor.CounterMonitor)10 MBeanServer (javax.management.MBeanServer)7 GaugeMonitor (javax.management.monitor.GaugeMonitor)5 Monitor (javax.management.monitor.Monitor)4 StringMonitor (javax.management.monitor.StringMonitor)4 NotificationEmitter (javax.management.NotificationEmitter)2 NotificationListener (javax.management.NotificationListener)2 CounterMonitorMBean (javax.management.monitor.CounterMonitorMBean)2 PrivilegedAction (java.security.PrivilegedAction)1 NotificationFilter (javax.management.NotificationFilter)1 JMXPrincipal (javax.management.remote.JMXPrincipal)1 Subject (javax.security.auth.Subject)1