Search in sources :

Example 1 with ShutDownTask

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;
}
Also used : ShutDownTask(jmri.ShutDownTask) ArrayList(java.util.ArrayList) Date(java.util.Date)

Example 2 with ShutDownTask

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());
}
Also used : ShutDownTask(jmri.ShutDownTask) QuietShutDownTask(jmri.implementation.QuietShutDownTask) QuietShutDownTask(jmri.implementation.QuietShutDownTask) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Example 3 with ShutDownTask

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
    }
}
Also used : ShutDownTask(jmri.ShutDownTask) QuietShutDownTask(jmri.implementation.QuietShutDownTask) QuietShutDownTask(jmri.implementation.QuietShutDownTask) ArrayList(java.util.ArrayList) List(java.util.List) Test(org.junit.Test)

Aggregations

ArrayList (java.util.ArrayList)3 ShutDownTask (jmri.ShutDownTask)3 List (java.util.List)2 QuietShutDownTask (jmri.implementation.QuietShutDownTask)2 Test (org.junit.Test)2 Date (java.util.Date)1