类javax.management.monitor.CounterMonitorMBean源码实例Demo

下面列出了怎么用javax.management.monitor.CounterMonitorMBean的API类实例代码及写法,或者点击链接到github查看源代码。

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();
}
 
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");
    }

}
 
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
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();
}
 
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");
    }

}
 
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
源代码7 项目: jdk8u60   文件: 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();
}
 
源代码8 项目: jdk8u60   文件: 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");
    }

}
 
源代码9 项目: jdk8u60   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
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();
}
 
源代码11 项目: openjdk-jdk8u   文件: 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");
    }

}
 
源代码12 项目: openjdk-jdk8u   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
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();
}
 
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");
    }

}
 
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
源代码16 项目: openjdk-jdk9   文件: 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();
}
 
源代码17 项目: openjdk-jdk9   文件: 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");
    }

}
 
源代码18 项目: openjdk-jdk9   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
源代码19 项目: 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();
}
 
源代码20 项目: 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");
    }

}
 
源代码21 项目: jdk8u-jdk   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
源代码22 项目: hottub   文件: 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();
}
 
源代码23 项目: hottub   文件: 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");
    }

}
 
源代码24 项目: hottub   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
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();
}
 
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");
    }

}
 
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
源代码28 项目: openjdk-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();
}
 
源代码29 项目: openjdk-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");
    }

}
 
源代码30 项目: openjdk-8   文件: CounterMonitorDeadlockTest.java
abstract void doSensitiveThing(CounterMonitorMBean monitorProxy,
ObjectName observedName);
 
 类所在包
 同包方法