Search in sources :

Example 1 with Call

use of com.sun.jmx.examples.scandir.ScanManagerTest.Call in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method testAddNotificationListener.

/**
     * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
     */
public void testAddNotificationListener() throws Exception {
    System.out.println("addNotificationListener");
    final ScanManagerMXBean manager = ScanManager.register();
    final Call op = new Call() {

        public void call() throws Exception {
            manager.start();
        }

        public void cancel() throws Exception {
            manager.stop();
        }
    };
    try {
        final String tmpdir = System.getProperty("java.io.tmpdir");
        final ScanDirConfigMXBean config = manager.getConfigurationMBean();
        final DirectoryScannerConfig bean = config.addDirectoryScanner("test1", tmpdir, ".*", 0, 0);
        manager.applyConfiguration(true);
        final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test1");
        doTestOperation(proxy, op, EnumSet.of(RUNNING, SCHEDULED), "scan");
    } finally {
        try {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : Call(com.sun.jmx.examples.scandir.ScanManagerTest.Call) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)

Example 2 with Call

use of com.sun.jmx.examples.scandir.ScanManagerTest.Call in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method testScan.

/**
     * Test of scan method, of class com.sun.jmx.examples.scandir.DirectoryScanner.
     */
public void testScan() throws Exception {
    System.out.println("scan");
    final ScanManagerMXBean manager = ScanManager.register();
    try {
        final String tmpdir = System.getProperty("java.io.tmpdir");
        final ScanDirConfigMXBean config = manager.getConfigurationMBean();
        final DirectoryScannerConfig bean = config.addDirectoryScanner("test1", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test2", tmpdir, ".*", 0, 0);
        config.addDirectoryScanner("test3", tmpdir, ".*", 0, 0);
        manager.applyConfiguration(true);
        final DirectoryScannerMXBean proxy = manager.getDirectoryScanners().get("test1");
        final Call op = new Call() {

            public void call() throws Exception {
                final BlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
                final NotificationListener listener = new NotificationListener() {

                    public void handleNotification(Notification notification, Object handback) {
                        try {
                            queue.put(notification);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                };
                manager.start();
                while (true) {
                    final Notification n = queue.poll(10, TimeUnit.SECONDS);
                    if (n == null)
                        break;
                    final AttributeChangeNotification at = (AttributeChangeNotification) n;
                    if (RUNNING == ScanState.valueOf((String) at.getNewValue()))
                        break;
                    else {
                        System.err.println("New state: " + (String) at.getNewValue() + " isn't " + RUNNING);
                    }
                }
                assertContained(EnumSet.of(SCHEDULED, RUNNING, COMPLETED), proxy.getState());
            }

            public void cancel() throws Exception {
                manager.stop();
            }
        };
        doTestOperation(proxy, op, EnumSet.of(RUNNING, SCHEDULED, COMPLETED), "scan");
    } catch (Exception x) {
        x.printStackTrace();
        throw x;
    } finally {
        try {
            manager.stop();
        } catch (Exception x) {
            System.err.println("Failed to stop: " + x);
        }
        try {
            ManagementFactory.getPlatformMBeanServer().unregisterMBean(ScanManager.SCAN_MANAGER_NAME);
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : Call(com.sun.jmx.examples.scandir.ScanManagerTest.Call) AttributeChangeNotification(javax.management.AttributeChangeNotification) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) DirectoryScannerConfig(com.sun.jmx.examples.scandir.config.DirectoryScannerConfig) NotificationListener(javax.management.NotificationListener)

Aggregations

Call (com.sun.jmx.examples.scandir.ScanManagerTest.Call)2 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)2 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)1 AttributeChangeNotification (javax.management.AttributeChangeNotification)1 Notification (javax.management.Notification)1 NotificationListener (javax.management.NotificationListener)1