001package jmri;
002
003import java.beans.PropertyChangeListener;
004import java.util.concurrent.Callable;
005
006/**
007 * Execute a specific task before the program terminates.
008 * <p>
009 * Tasks should leave the system in a state that can continue, in case a later
010 * task aborts the shutdown.
011 * <p>
012 * A ShutDownTask can listen to the "shuttingDown" property of the
013 * {@link ShutDownManager} to determine if any other ShutDownTask aborted the
014 * shutdown; the property will change from false to true in that case.
015 *
016 * @author Bob Jacobsen Copyright (C) 2008
017 */
018public interface ShutDownTask extends Callable<Boolean>, Runnable, PropertyChangeListener {
019
020    /**
021     * Ask if shut down is allowed.
022     * <p>
023     * The shut down manager calls this method first on all the tasks before
024     * starting to execute the method {@link #run()} on the tasks.
025     * <p>
026     * If this method returns false on any task, the shut down process must be
027     * aborted.
028     *
029     * @return true if it is OK to shut down, false to abort shut down.
030     * @throws Exception if there is an exception
031     */
032    @Override
033    Boolean call() throws Exception;
034
035    /**
036     * Take the necessary action. This method cannot abort the shutdown, and
037     * must not require user interaction to complete successfully. This method
038     * will be run in parallel to other ShutDownTasks, but will be executed
039     * before any of the {@code run() } methods are executed.
040     */
041    default void runEarly() {
042    }
043
044    /**
045     * Take the necessary action. This method cannot abort the shutdown, and
046     * must not require user interaction to complete successfully. This method
047     * will be run in parallel to other ShutDownTasks.
048     */
049    @Override
050    void run();
051
052    /**
053     * Name to be provided to the user when information about this task is
054     * presented.
055     *
056     * @return the name
057     */
058    String getName();
059}