Search in sources :

Example 1 with ScanState

use of com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState in project jdk8u_jdk by JetBrains.

the class ScanManager method switchState.

/**
     * Checks that the current state is one of the allowed states,
     * and if so, switch its value to the new desired state.
     * This operation also enqueue the appropriate state changed
     * notification.
     **/
private ScanState switchState(ScanState desired, EnumSet<ScanState> allowed) {
    final ScanState old;
    final long timestamp;
    final long sequence;
    synchronized (this) {
        old = state;
        if (!allowed.contains(state))
            throw new IllegalStateException(state.toString());
        state = desired;
        timestamp = System.currentTimeMillis();
        sequence = getNextSeqNumber();
    }
    LOG.fine("switched state: " + old + " -> " + desired);
    if (old != desired)
        queueStateChangedNotification(sequence, timestamp, old, desired);
    return old;
}
Also used : ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)

Example 2 with ScanState

use of com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState in project jdk8u_jdk by JetBrains.

the class ScanManager method schedule.

// See ScanManagerMXBean
public void schedule(long delay, long interval) {
    if (!sequencer.tryAcquire()) {
        throw new IllegalStateException("Can't acquire lock");
    }
    try {
        LOG.fine("scheduling new task: state=" + state);
        final ScanState old = switchState(SCHEDULED, "schedule");
        final boolean scheduled = scheduleSession(new SessionTask(interval), delay);
        if (scheduled)
            LOG.fine("new task scheduled: state=" + state);
    } finally {
        sequencer.release();
    }
    sendQueuedNotifications();
}
Also used : ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)

Example 3 with ScanState

use of com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState in project jdk8u_jdk by JetBrains.

the class ScanManagerTest method testGetState.

/**
     * Test of getState method, of class com.sun.jmx.examples.scandir.ScanManager.
     */
public void testGetState() throws IOException, InstanceNotFoundException {
    System.out.println("getState");
    ScanManager instance = new ScanManager();
    ScanState expResult = ScanState.STOPPED;
    ScanState result = instance.getState();
    assertEquals(expResult, result);
    instance.start();
    final ScanState afterStart = instance.getState();
    assertContained(EnumSet.of(RUNNING, SCHEDULED, COMPLETED), afterStart);
    instance.stop();
    assertEquals(STOPPED, instance.getState());
    instance.schedule(1000000L, 1000000L);
    assertEquals(SCHEDULED, instance.getState());
    instance.stop();
    assertEquals(STOPPED, instance.getState());
}
Also used : ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)

Example 4 with ScanState

use of com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState in project jdk8u_jdk by JetBrains.

the class ScanManagerTest method doTestOperation.

/**
     * Test of addNotificationListener method, of class com.sun.jmx.examples.scandir.ScanManager.
     */
private void doTestOperation(ScanManagerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
    System.out.println("doTestOperation: " + testName);
    final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
    NotificationListener listener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            try {
                queue.put(notification);
            } catch (Exception x) {
                System.err.println("Failed to queue notif: " + x);
            }
        }
    };
    NotificationFilter filter = null;
    Object handback = null;
    final ScanState before;
    final NotificationEmitter emitter = (NotificationEmitter) proxy;
    emitter.addNotificationListener(listener, filter, handback);
    before = proxy.getState();
    op.call();
    try {
        final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
        assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
        assertEquals(AttributeChangeNotification.class, notification.getClass());
        assertEquals(ScanManager.SCAN_MANAGER_NAME, notification.getSource());
        AttributeChangeNotification acn = (AttributeChangeNotification) notification;
        assertEquals("State", acn.getAttributeName());
        assertEquals(ScanState.class.getName(), acn.getAttributeType());
        assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
        assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
        emitter.removeNotificationListener(listener, filter, handback);
    } finally {
        try {
            op.cancel();
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) AttributeChangeNotification(javax.management.AttributeChangeNotification) Notification(javax.management.Notification) InstanceNotFoundException(javax.management.InstanceNotFoundException) IOException(java.io.IOException) JMException(javax.management.JMException) ListenerNotFoundException(javax.management.ListenerNotFoundException) NotificationListener(javax.management.NotificationListener)

Example 5 with ScanState

use of com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState in project jdk8u_jdk by JetBrains.

the class DirectoryScannerTest method doTestOperation.

private void doTestOperation(DirectoryScannerMXBean proxy, Call op, EnumSet<ScanState> after, String testName) throws Exception {
    System.out.println("doTestOperation: " + testName);
    final LinkedBlockingQueue<Notification> queue = new LinkedBlockingQueue<Notification>();
    NotificationListener listener = new NotificationListener() {

        public void handleNotification(Notification notification, Object handback) {
            try {
                queue.put(notification);
            } catch (Exception x) {
                System.err.println("Failed to queue notif: " + x);
            }
        }
    };
    NotificationFilter filter = null;
    Object handback = null;
    final ScanState before;
    final NotificationEmitter emitter = (NotificationEmitter) makeNotificationEmitter(proxy, DirectoryScannerMXBean.class);
    emitter.addNotificationListener(listener, filter, handback);
    before = proxy.getState();
    op.call();
    try {
        final Notification notification = queue.poll(3000, TimeUnit.MILLISECONDS);
        assertEquals(AttributeChangeNotification.ATTRIBUTE_CHANGE, notification.getType());
        assertEquals(AttributeChangeNotification.class, notification.getClass());
        assertEquals(getObjectName(proxy), notification.getSource());
        AttributeChangeNotification acn = (AttributeChangeNotification) notification;
        assertEquals("State", acn.getAttributeName());
        assertEquals(ScanState.class.getName(), acn.getAttributeType());
        assertEquals(before, ScanState.valueOf((String) acn.getOldValue()));
        assertContained(after, ScanState.valueOf((String) acn.getNewValue()));
        emitter.removeNotificationListener(listener, filter, handback);
    } finally {
        try {
            op.cancel();
        } catch (Exception x) {
            System.err.println("Failed to cleanup: " + x);
        }
    }
}
Also used : NotificationEmitter(javax.management.NotificationEmitter) AttributeChangeNotification(javax.management.AttributeChangeNotification) ScanState(com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState) LinkedBlockingQueue(java.util.concurrent.LinkedBlockingQueue) NotificationFilter(javax.management.NotificationFilter) Notification(javax.management.Notification) AttributeChangeNotification(javax.management.AttributeChangeNotification) NotificationListener(javax.management.NotificationListener)

Aggregations

ScanState (com.sun.jmx.examples.scandir.ScanManagerMXBean.ScanState)7 AttributeChangeNotification (javax.management.AttributeChangeNotification)3 LinkedBlockingQueue (java.util.concurrent.LinkedBlockingQueue)2 Notification (javax.management.Notification)2 NotificationEmitter (javax.management.NotificationEmitter)2 NotificationFilter (javax.management.NotificationFilter)2 NotificationListener (javax.management.NotificationListener)2 DirectoryScannerConfig (com.sun.jmx.examples.scandir.config.DirectoryScannerConfig)1 IOException (java.io.IOException)1 InstanceNotFoundException (javax.management.InstanceNotFoundException)1 JMException (javax.management.JMException)1 ListenerNotFoundException (javax.management.ListenerNotFoundException)1