Java 类javax.management.monitor.CounterMonitor 实例源码

项目:freeVM    文件:ErrorNotificationsTest.java   
/** Actions before every test case (common) */
public void setUp() {

    res = true;
    counter.setInteger(new Integer(0));
    counter.setExceptionFlag(false);
    counter.setNullFlag(false);

    server = MBeanServerFactory.createMBeanServer();
    monitor = new CounterMonitor();

    try {
        monitorName = new ObjectName(
                "org.apache.harmony.test.func.api.javax.management.monitor."
                        + "gaugemonitor:type=GaugeMonitor,id=1");
        counterName = new ObjectName(Counter.COUNTER_NAME_TEMPLATE + "1");
    } catch (Throwable e) {
        e.printStackTrace();
        System.err.println("Test: ERROR: Internal error!");
        System.exit(106);
    }

    monitor.addNotificationListener(nListener, null, "handback");
}
项目:jdk8u-jdk    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:jdk8u-jdk    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:jdk8u-jdk    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:jdk8u-jdk    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:jdk8u-jdk    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:jdk8u-jdk    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
项目:openjdk-jdk10    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:openjdk-jdk10    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:openjdk-jdk10    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:openjdk-jdk10    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:openjdk-jdk10    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:openjdk-jdk10    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
项目:openjdk9    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:openjdk9    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:openjdk9    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:openjdk9    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:openjdk9    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:openjdk9    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
项目:jdk8u_jdk    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:jdk8u_jdk    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:jdk8u_jdk    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:jdk8u_jdk    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:jdk8u_jdk    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:jdk8u_jdk    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
项目:lookaside_java-1.8.0-openjdk    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:lookaside_java-1.8.0-openjdk    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:lookaside_java-1.8.0-openjdk    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:lookaside_java-1.8.0-openjdk    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:lookaside_java-1.8.0-openjdk    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:lookaside_java-1.8.0-openjdk    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);

    final int initGetCount = observedProxy.getGetCount();
    monitorProxy.start();

    System.out.println("Checking GetCount, possible deadlock if timeout.");
    do { // 8038322. Until timeout of testing harness
        Thread.sleep(200);
    } while ((observedProxy.getGetCount()) == initGetCount);
    System.out.println("Done!");

    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        System.out.println("Waiting notifCount.get() != 0, possible deadlock if timeout.");
        do {
            Thread.sleep(200);
        } while(notifCount.get() == 0); // 8038322. Until timeout of testing harness
        System.out.println("Done");
    }

}
项目:Camel    文件:JMXMonitorConsumer.java   
@Override
protected void addNotificationListener() throws Exception {

    JMXEndpoint ep = (JMXEndpoint) getEndpoint();
    // create the monitor bean
    Monitor bean = null;
    if (ep.getMonitorType().equals("counter")) {
        CounterMonitor counter = new CounterMonitor();
        Number initThreshold = convertNumberToAttributeType(ep.getInitThreshold(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number offset = convertNumberToAttributeType(ep.getOffset(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number modulus = convertNumberToAttributeType(ep.getModulus(), ep.getJMXObjectName(), ep.getObservedAttribute());
        counter.setInitThreshold(initThreshold);
        counter.setOffset(offset);
        counter.setModulus(modulus);
        counter.setDifferenceMode(ep.isDifferenceMode());
        counter.setNotify(true);
        bean = counter;
    } else if (ep.getMonitorType().equals("gauge")) {
        GaugeMonitor gm = new GaugeMonitor();
        gm.setNotifyHigh(ep.isNotifyHigh());
        gm.setNotifyLow(ep.isNotifyLow());
        gm.setDifferenceMode(ep.isDifferenceMode());
        Number highValue = convertNumberToAttributeType(ep.getThresholdHigh(), ep.getJMXObjectName(), ep.getObservedAttribute());
        Number lowValue = convertNumberToAttributeType(ep.getThresholdLow(), ep.getJMXObjectName(), ep.getObservedAttribute());
        gm.setThresholds(highValue, lowValue);
        bean = gm;
    } else if (ep.getMonitorType().equals("string")) {
        StringMonitor sm = new StringMonitor();
        sm.setNotifyDiffer(ep.isNotifyDiffer());
        sm.setNotifyMatch(ep.isNotifyMatch());
        sm.setStringToCompare(ep.getStringToCompare());
        bean = sm;
    }

    bean.addObservedObject(ep.getJMXObjectName());
    bean.setObservedAttribute(ep.getObservedAttribute());
    bean.setGranularityPeriod(ep.getGranularityPeriod());

    // register the bean
    mMonitorObjectName = new ObjectName(ep.getObjectDomain(), "name", "camel-jmx-monitor-" + UUID.randomUUID());
    ManagementFactory.getPlatformMBeanServer().registerMBean(bean, mMonitorObjectName); 

    // add ourselves as a listener to it
    NotificationFilter nf = ep.getNotificationFilter();
    getServerConnection().addNotificationListener(mMonitorObjectName, this, nf, bean);
    bean.start();
}
项目:infobip-open-jdk-8    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:infobip-open-jdk-8    文件:RuntimeExceptionTest.java   
/**
 * 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;
}
项目:infobip-open-jdk-8    文件:ThreadPoolTest.java   
/**
 * 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;
}
项目:infobip-open-jdk-8    文件:ReflectionExceptionTest.java   
/**
 * 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;
}
项目:infobip-open-jdk-8    文件:CounterMonitorThresholdTest.java   
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();

    // Play with counter
    //
    for (int i = 0; i < counter.length; i++) {
        mbean.setCounter(counter[i]);
        System.out.println("\nCounter = " + mbean.getCounter());
        Integer derivedGaugeValue;
        // either pass or test timeout (killed by test harness)
        // 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();
}
项目:infobip-open-jdk-8    文件:CounterMonitorDeadlockTest.java   
void run() throws Exception {
    final MBeanServer mbs = ManagementFactory.getPlatformMBeanServer();
    final ObjectName observedName = new ObjectName("a:b=c");
    final ObjectName monitorName = new ObjectName("a:type=Monitor");
    mbs.registerMBean(new CounterMonitor(), monitorName);
    final CounterMonitorMBean monitorProxy =
        JMX.newMBeanProxy(mbs, monitorName, CounterMonitorMBean.class);
    final TestMBean observedProxy =
        JMX.newMBeanProxy(mbs, observedName, TestMBean.class);

    final Runnable sensitiveThing = new Runnable() {
        public void run() {
            doSensitiveThing(monitorProxy, observedName);
        }
    };

    final Runnable nothing = new Runnable() {
        public void run() {}
    };

    final Runnable withinGetAttribute =
        (when == When.IN_GET_ATTRIBUTE) ? sensitiveThing : nothing;

    mbs.registerMBean(new Test(withinGetAttribute), observedName);
    monitorProxy.addObservedObject(observedName);
    monitorProxy.setObservedAttribute("Thing");
    monitorProxy.setInitThreshold(100);
    monitorProxy.setGranularityPeriod(10L); // 10 ms
    monitorProxy.setNotify(true);
    monitorProxy.start();

    final int initGetCount = observedProxy.getGetCount();
    int getCount = initGetCount;
    for (int i = 0; i < 500; i++) { // 500 * 10 = 5 seconds
        getCount = observedProxy.getGetCount();
        if (getCount != initGetCount)
            break;
        Thread.sleep(10);
    }
    if (getCount <= initGetCount)
        throw new Exception("Test failed: presumable deadlock");
    // This won't show up as a deadlock in CTRL-\ or in
    // ThreadMXBean.findDeadlockedThreads(), because they don't
    // see that thread A is waiting for thread B (B.join()), and
    // thread B is waiting for a lock held by thread A

    // Now we know the monitor has observed the initial value,
    // so if we want to test notify behaviour we can trigger by
    // exceeding the threshold.
    if (when == When.IN_NOTIFY) {
        final AtomicInteger notifCount = new AtomicInteger();
        final NotificationListener listener = new NotificationListener() {
            public void handleNotification(Notification n, Object h) {
                Thread t = new Thread(sensitiveThing);
                t.start();
                try {
                    t.join();
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                notifCount.incrementAndGet();
            }
        };
        mbs.addNotificationListener(monitorName, listener, null, null);
        observedProxy.setThing(1000);
        for (int i = 0; i < 500 && notifCount.get() == 0; i++)
            Thread.sleep(10);
        if (notifCount.get() == 0)
            throw new Exception("Test failed: presumable deadlock");
    }

}
项目:jdk8u-dev-jdk    文件:ThreadPoolAccTest.java   
public static void main (String args[]) throws Exception {

        ObjectName[] mbeanNames = new ObjectName[6];
        ObservedObject[] monitored = new ObservedObject[6];
        ObjectName[] monitorNames = new ObjectName[6];
        Monitor[] monitor = new Monitor[6];
        String[] principals = { "role1", "role2" };
        String[] attributes = { "Integer", "Double", "String" };

        try {
            echo(">>> CREATE MBeanServer");
            MBeanServer server = MBeanServerFactory.newMBeanServer();

            for (int i = 0; i < 6; i++) {
                mbeanNames[i] =
                    new ObjectName(":type=ObservedObject,instance=" + i);
                monitored[i] = new ObservedObject();
                echo(">>> CREATE ObservedObject = " + mbeanNames[i].toString());
                server.registerMBean(monitored[i], mbeanNames[i]);

                switch (i) {
                    case 0:
                    case 3:
                        monitorNames[i] =
                            new ObjectName(":type=CounterMonitor,instance=" + i);
                        monitor[i] = new CounterMonitor();
                        break;
                    case 1:
                    case 4:
                        monitorNames[i] =
                            new ObjectName(":type=GaugeMonitor,instance=" + i);
                        monitor[i] = new GaugeMonitor();
                        break;
                    case 2:
                    case 5:
                        monitorNames[i] =
                            new ObjectName(":type=StringMonitor,instance=" + i);
                        monitor[i] = new StringMonitor();
                        break;
                }

                echo(">>> CREATE Monitor = " + monitorNames[i].toString());
                server.registerMBean(monitor[i], monitorNames[i]);
                monitor[i].addObservedObject(mbeanNames[i]);
                monitor[i].setObservedAttribute(attributes[i % 3]);
                monitor[i].setGranularityPeriod(500);
                final Monitor m = monitor[i];
                Subject subject = new Subject();
                echo(">>> RUN Principal = " + principals[i / 3]);
                subject.getPrincipals().add(new JMXPrincipal(principals[i / 3]));
                PrivilegedAction<Void> action = new PrivilegedAction<Void>() {
                    public Void run() {
                        m.start();
                        return null;
                    }
                };
                Subject.doAs(subject, action);
            }

            while(!testPrincipals(monitored, monitorNames, monitor, principals));

        } finally {
            for (int i = 0; i < 6; i++)
                if (monitor[i] != null)
                    monitor[i].stop();
        }
    }
项目:jdk8u-dev-jdk    文件:RuntimeExceptionTest.java   
/**
 * 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;
}