Class DefaultShutDownManager
- java.lang.Object
-
- jmri.beans.UnboundBean
-
- jmri.beans.Bean
-
- jmri.managers.DefaultShutDownManager
-
- All Implemented Interfaces:
BeanInterface
,PropertyChangeFirer
,PropertyChangeProvider
,ShutDownManager
public class DefaultShutDownManager extends Bean implements ShutDownManager
The default implementation ofShutDownManager
. This implementation makes the following assumptions:- The
shutdown()
andrestart()
methods are called on the application's main thread. - If the application has a graphical user interface, the application's main thread is the event dispatching thread.
- Application windows may contain code that should be run within a
registered
ShutDownTask.run()
method, but are not. A side effect of this assumption is that all displayable application windows are closed by this implementation when shutdown() or restart() is called and a ShutDownTask has not aborted the shutdown or restart. - It is expected that SIGINT and SIGTERM should trigger a clean application exit.
If another implementation of ShutDownManager has not been registered with the
InstanceManager
, an instance of this implementation will be automatically registered as the ShutDownManager.Developers other applications that cannot accept the above assumptions are recommended to create their own implementations of ShutDownManager that integrates with their application's lifecycle and register that implementation with the InstanceManager as soon as possible in their application.
-
-
Field Summary
Fields Modifier and Type Field Description protected java.lang.Thread
shutdownHook
(package private) int
tasksTimeOutMilliSec
-
Fields inherited from class jmri.beans.Bean
propertyChangeSupport
-
-
Constructor Summary
Constructors Constructor Description DefaultShutDownManager()
Create a new shutdown manager.
-
Method Summary
All Methods Static Methods Instance Methods Concrete Methods Modifier and Type Method Description void
deregister(java.lang.Runnable task)
Deregister a task.void
deregister(java.util.concurrent.Callable<java.lang.Boolean> task)
Deregister a task.void
deregister(ShutDownTask s)
Deregister a task.java.util.List<java.util.concurrent.Callable<java.lang.Boolean>>
getCallables()
java.util.List<java.lang.Runnable>
getRunnables()
boolean
isShutDownComplete()
Flag to indicate when all shutDown tasks completed.boolean
isShuttingDown()
Allow components that normally request confirmation to shutdown to determine if the shutdown is already underway so as not to request confirmation.void
register(java.lang.Runnable task)
Register a task that runs when JMRI is stopping.void
register(java.util.concurrent.Callable<java.lang.Boolean> task)
Register a task for verification that JMRI should stop.void
register(ShutDownTask s)
Register a task object for later execution.void
restart()
Run the shutdown tasks, and then terminate the program with status 100 if not aborted.void
restartOS()
Run the shutdown tasks, and then terminate the program with status 210 if not aborted.void
setBlockingShutdown(boolean value)
Set if shutdown should block GUI/Layout thread.protected void
setShuttingDown(boolean state)
This method is static so that if multiple DefaultShutDownManagers are registered, they are all aware of this state.(package private) static void
setStaticShuttingDown(boolean state)
void
shutdown()
Run the shutdown tasks, and then terminate the program with status 0 if not aborted.void
shutdown(int status, boolean exit)
First asks the shutdown tasks if shutdown is allowed.void
shutdownOS()
Run the shutdown tasks, and then terminate the program with status 200 if not aborted.-
Methods inherited from class jmri.beans.Bean
addPropertyChangeListener, addPropertyChangeListener, fireIndexedPropertyChange, fireIndexedPropertyChange, fireIndexedPropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, firePropertyChange, getPropertyChangeListeners, getPropertyChangeListeners, isNotifyOnEDT, removePropertyChangeListener, removePropertyChangeListener
-
Methods inherited from class jmri.beans.UnboundBean
getIndexedProperty, getProperty, getPropertyNames, hasIndexedProperty, hasProperty, setIndexedProperty, setProperty
-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface jmri.beans.PropertyChangeProvider
addPropertyChangeListener, addPropertyChangeListener, getPropertyChangeListeners, getPropertyChangeListeners, removePropertyChangeListener, removePropertyChangeListener
-
-
-
-
Field Detail
-
shutdownHook
protected final java.lang.Thread shutdownHook
-
tasksTimeOutMilliSec
int tasksTimeOutMilliSec
-
-
Constructor Detail
-
DefaultShutDownManager
public DefaultShutDownManager()
Create a new shutdown manager.
-
-
Method Detail
-
setBlockingShutdown
public void setBlockingShutdown(boolean value)
Set if shutdown should block GUI/Layout thread.- Parameters:
value
- true if blocking, false otherwise
-
register
public void register(ShutDownTask s)
Register a task object for later execution. An attempt to register an already registered task will be silently ignored.- Specified by:
register
in interfaceShutDownManager
- Parameters:
s
- the task to execute
-
register
public void register(java.util.concurrent.Callable<java.lang.Boolean> task)
Register a task for verification that JMRI should stop. An attempt to register an already register task will be silently ignored.- Specified by:
register
in interfaceShutDownManager
- Parameters:
task
- the verification task
-
register
public void register(java.lang.Runnable task)
Register a task that runs when JMRI is stopping. An attempt to register an already register task will be silently ignored.- Specified by:
register
in interfaceShutDownManager
- Parameters:
task
- the execution task
-
deregister
public void deregister(ShutDownTask s)
Deregister a task. Attempts to deregister a task that is not registered are silently ignored.- Specified by:
deregister
in interfaceShutDownManager
- Parameters:
s
- the task not to execute
-
deregister
public void deregister(java.util.concurrent.Callable<java.lang.Boolean> task)
Deregister a task. Attempts to deregister a task that is not registered are silently ignored.- Specified by:
deregister
in interfaceShutDownManager
- Parameters:
task
- the task not to call
-
deregister
public void deregister(java.lang.Runnable task)
Deregister a task. Attempts to deregister a task that is not registered are silently ignored.- Specified by:
deregister
in interfaceShutDownManager
- Parameters:
task
- the task not to run
-
getCallables
public java.util.List<java.util.concurrent.Callable<java.lang.Boolean>> getCallables()
- Specified by:
getCallables
in interfaceShutDownManager
-
getRunnables
public java.util.List<java.lang.Runnable> getRunnables()
- Specified by:
getRunnables
in interfaceShutDownManager
-
shutdown
public void shutdown()
Run the shutdown tasks, and then terminate the program with status 0 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.- Specified by:
shutdown
in interfaceShutDownManager
-
restart
public void restart()
Run the shutdown tasks, and then terminate the program with status 100 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.By exiting the program with status 100, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and restart the JMRI java program.
- Specified by:
restart
in interfaceShutDownManager
-
restartOS
public void restartOS()
Run the shutdown tasks, and then terminate the program with status 210 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.By exiting the program with status 210, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and tell the operating system to restart.
- Specified by:
restartOS
in interfaceShutDownManager
-
shutdownOS
public void shutdownOS()
Run the shutdown tasks, and then terminate the program with status 200 if not aborted. Does not return under normal circumstances. Returns false if the shutdown was aborted by the user, in which case the program should continue to operate.By exiting the program with status 200, the batch file (MS Windows) or shell script (Linux/macOS/UNIX) can catch the exit status and shutdown the OS
- Specified by:
shutdownOS
in interfaceShutDownManager
-
shutdown
public void shutdown(int status, boolean exit)
First asks the shutdown tasks if shutdown is allowed. Returns if the shutdown was aborted by the user, in which case the program should continue to operate.After this check does not return under normal circumstances. Closes any displayable windows. Executes all registered
ShutDownTask
Runs the Early shutdown tasks, the main shutdown tasks, then terminates the program with provided status.- Parameters:
status
- integer status on program exitexit
- true if System.exit() should be called if all tasks are executed correctly; false otherwise
-
isShuttingDown
public boolean isShuttingDown()
Allow components that normally request confirmation to shutdown to determine if the shutdown is already underway so as not to request confirmation.- Specified by:
isShuttingDown
in interfaceShutDownManager
- Returns:
- true if shutting down or restarting
-
isShutDownComplete
public boolean isShutDownComplete()
Flag to indicate when all shutDown tasks completed. For test purposes, the app would normally exit before setting the flag.- Returns:
- true when Shutdown tasks are complete and System.exit is not called.
-
setShuttingDown
protected void setShuttingDown(boolean state)
This method is static so that if multiple DefaultShutDownManagers are registered, they are all aware of this state.- Parameters:
state
- true if shutting down; false otherwise
-
setStaticShuttingDown
static void setStaticShuttingDown(boolean state)
-
-