use of jmri.ShutDownTask in project JMRI by JMRI.
the class DefaultShutDownManager method runShutDownTasks.
/**
* Run registered shutdown tasks. Any Exceptions are logged and otherwise
* ignored.
*
* @param isParallel true if parallel-capable shutdown tasks are to be run;
* false if shutdown tasks that must be run sequentially
* are to be run
* @return true if shutdown tasks ran; false if a shutdown task aborted the
* shutdown sequence
*/
private boolean runShutDownTasks(boolean isParallel) {
// can't return out of a stream or forEach loop
for (ShutDownTask task : new ArrayList<>(this.tasks)) {
if (task.isParallel() == isParallel) {
log.debug("Calling task \"{}\"", task.getName());
Date timer = new Date();
try {
// if a task aborts the shutdown, stop shutting down
setShuttingDown(task.execute());
if (!shuttingDown) {
log.info("Program termination aborted by \"{}\"", task.getName());
// abort early
return false;
}
} catch (Exception e) {
log.error("Error during processing of ShutDownTask \"{}\"", task.getName(), e);
} catch (Throwable e) {
// try logging the error
log.error("Unrecoverable error during processing of ShutDownTask \"{}\"", task.getName(), e);
log.error("Terminating abnormally");
// also dump error directly to System.err in hopes its more observable
System.err.println("Unrecoverable error during processing of ShutDownTask \"" + task.getName() + "\"");
System.err.println(e);
System.err.println("Terminating abnormally");
// forcably halt, do not restart, even if requested
Runtime.getRuntime().halt(1);
}
log.debug("Task \"{}\" took {} milliseconds to execute", task.getName(), new Date().getTime() - timer.getTime());
}
}
return true;
}
use of jmri.ShutDownTask in project JMRI by JMRI.
the class DefaultShutDownManagerTest method testDeregister.
@Test
public void testDeregister() {
DefaultShutDownManager dsdm = new DefaultShutDownManager();
List tasks = this.exposeTasks(dsdm);
Assert.assertEquals(0, tasks.size());
ShutDownTask task = new QuietShutDownTask("task") {
@Override
public boolean execute() {
return true;
}
};
dsdm.register(task);
Assert.assertEquals(1, tasks.size());
Assert.assertTrue(tasks.contains(task));
dsdm.deregister(task);
Assert.assertEquals(0, tasks.size());
}
use of jmri.ShutDownTask in project JMRI by JMRI.
the class DefaultShutDownManagerTest method testRegister.
@Test
public void testRegister() {
DefaultShutDownManager dsdm = new DefaultShutDownManager();
List tasks = this.exposeTasks(dsdm);
Assert.assertEquals(0, tasks.size());
ShutDownTask task = new QuietShutDownTask("task") {
@Override
public boolean execute() {
return true;
}
};
dsdm.register(task);
Assert.assertEquals(1, tasks.size());
dsdm.register(task);
Assert.assertEquals(1, tasks.size());
try {
dsdm.register(null);
Assert.fail("Expected NullPointerException not thrown");
} catch (NullPointerException ex) {
// ignore since throwing the NPE is passing
}
}
Aggregations