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