Package org.apache.commons.exec
Class ShutdownHookProcessDestroyer
- java.lang.Object
-
- org.apache.commons.exec.ShutdownHookProcessDestroyer
-
- All Implemented Interfaces:
java.lang.Runnable,ProcessDestroyer
public class ShutdownHookProcessDestroyer extends java.lang.Object implements ProcessDestroyer, java.lang.Runnable
Destroys all registeredProcesses when the VM exits.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description private classShutdownHookProcessDestroyer.ProcessDestroyerThread
-
Field Summary
Fields Modifier and Type Field Description private java.util.concurrent.atomic.AtomicBooleanaddedWhether or not this ProcessDestroyer has been registered as a shutdown hook.private ShutdownHookProcessDestroyer.ProcessDestroyerThreaddestroyProcessThreadThe thread registered at the JVM to execute the shutdown handler.private java.util.List<java.lang.Process>processesThe list of currently running processes.private java.util.concurrent.atomic.AtomicBooleanrunningWhether or not this ProcessDestroyer is currently running as shutdown hook.
-
Constructor Summary
Constructors Constructor Description ShutdownHookProcessDestroyer()Constructs aProcessDestroyerand obtainsRuntime.addShutdownHook()andRuntime.removeShutdownHook()through reflection.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description booleanadd(java.lang.Process process)Returnstrueif the specifiedProcesswas successfully added to the list of processes to destroy upon VM exit.private voidaddShutdownHook()Registers thisProcessDestroyeras a shutdown hook.booleanisAddedAsShutdownHook()Tests whether or not the ProcessDestroyer is registered as shutdown hook.booleanisEmpty()Tests emptiness (size == 0).booleanremove(java.lang.Process process)Returnstrueif the specifiedProcesswas successfully removed from the list of processes to destroy upon VM exit.private voidremoveShutdownHook()Removes thisProcessDestroyeras a shutdown hook.voidrun()Invoked by the VM when it is exiting.intsize()Returns the number of registered processes.
-
-
-
Field Detail
-
processes
private final java.util.List<java.lang.Process> processes
The list of currently running processes.
-
destroyProcessThread
private ShutdownHookProcessDestroyer.ProcessDestroyerThread destroyProcessThread
The thread registered at the JVM to execute the shutdown handler.
-
added
private final java.util.concurrent.atomic.AtomicBoolean added
Whether or not this ProcessDestroyer has been registered as a shutdown hook.
-
running
private final java.util.concurrent.atomic.AtomicBoolean running
Whether or not this ProcessDestroyer is currently running as shutdown hook.
-
-
Constructor Detail
-
ShutdownHookProcessDestroyer
public ShutdownHookProcessDestroyer()
Constructs aProcessDestroyerand obtainsRuntime.addShutdownHook()andRuntime.removeShutdownHook()through reflection. The ProcessDestroyer manages a list of processes to be destroyed when the VM exits. If a process is added when the list is empty, thisProcessDestroyeris registered as a shutdown hook. If removing a process results in an empty list, theProcessDestroyeris removed as a shutdown hook.
-
-
Method Detail
-
add
public boolean add(java.lang.Process process)
Returnstrueif the specifiedProcesswas successfully added to the list of processes to destroy upon VM exit.- Specified by:
addin interfaceProcessDestroyer- Parameters:
process- the process to add.- Returns:
trueif the specifiedProcesswas successfully added.
-
addShutdownHook
private void addShutdownHook()
Registers thisProcessDestroyeras a shutdown hook.
-
isAddedAsShutdownHook
public boolean isAddedAsShutdownHook()
Tests whether or not the ProcessDestroyer is registered as shutdown hook.- Returns:
- true if this is currently added as shutdown hook.
-
isEmpty
public boolean isEmpty()
Tests emptiness (size == 0).- Returns:
- Whether or not this is empty.
- Since:
- 1.4.0
-
remove
public boolean remove(java.lang.Process process)
Returnstrueif the specifiedProcesswas successfully removed from the list of processes to destroy upon VM exit.- Specified by:
removein interfaceProcessDestroyer- Parameters:
process- the process to remove.- Returns:
trueif the specifiedProcesswas successfully removed.
-
removeShutdownHook
private void removeShutdownHook()
Removes thisProcessDestroyeras a shutdown hook.
-
run
public void run()
Invoked by the VM when it is exiting.- Specified by:
runin interfacejava.lang.Runnable
-
size
public int size()
Returns the number of registered processes.- Specified by:
sizein interfaceProcessDestroyer- Returns:
- the number of register process.
-
-