Class ShutdownHookProcessDestroyer

java.lang.Object
org.apache.commons.exec.ShutdownHookProcessDestroyer
All Implemented Interfaces:
Runnable, ProcessDestroyer

public class ShutdownHookProcessDestroyer extends Object implements ProcessDestroyer, Runnable
Destroys all registered Processes when the VM exits.
  • Nested Class Summary

    Nested Classes
    Modifier and Type
    Class
    Description
    private final class 
     
  • Field Summary

    Fields
    Modifier and Type
    Field
    Description
    private boolean
    Whether or not this ProcessDestroyer has been registered as a shutdown hook.
    The thread registered at the JVM to execute the shutdown handler.
    private final Vector<Process>
    The list of currently running processes.
    private boolean
    Whether or not this ProcessDestroyer is currently running as shutdown hook.
  • Constructor Summary

    Constructors
    Constructor
    Description
    Constructs a ProcessDestroyer and obtains Runtime.addShutdownHook() and Runtime.removeShutdownHook() through reflection.
  • Method Summary

    Modifier and Type
    Method
    Description
    boolean
    add(Process process)
    Returns true if the specified Process was successfully added to the list of processes to destroy upon VM exit.
    private void
    Registers this ProcessDestroyer as a shutdown hook.
    boolean
    Tests whether or not the ProcessDestroyer is registered as shutdown hook.
    boolean
    Tests emptiness (size == 0).
    boolean
    remove(Process process)
    Returns true if the specified Process was successfully removed from the list of processes to destroy upon VM exit.
    private void
    Removes this ProcessDestroyer as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibility
    void
    run()
    Invoked by the VM when it is exiting.
    int
    Returns the number of registered processes.

    Methods inherited from class java.lang.Object

    clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
  • Field Details

    • processes

      private final Vector<Process> processes
      The list of currently running processes.
    • destroyProcessThread

      The thread registered at the JVM to execute the shutdown handler.
    • added

      private boolean added
      Whether or not this ProcessDestroyer has been registered as a shutdown hook.
    • running

      private volatile boolean running
      Whether or not this ProcessDestroyer is currently running as shutdown hook.
  • Constructor Details

    • ShutdownHookProcessDestroyer

      public ShutdownHookProcessDestroyer()
      Constructs a ProcessDestroyer and obtains Runtime.addShutdownHook() and Runtime.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, this ProcessDestroyer is registered as a shutdown hook. If removing a process results in an empty list, the ProcessDestroyer is removed as a shutdown hook.
  • Method Details

    • add

      public boolean add(Process process)
      Returns true if the specified Process was successfully added to the list of processes to destroy upon VM exit.
      Specified by:
      add in interface ProcessDestroyer
      Parameters:
      process - the process to add.
      Returns:
      true if the specified Process was successfully added.
    • addShutdownHook

      private void addShutdownHook()
      Registers this ProcessDestroyer as 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(Process process)
      Returns true if the specified Process was successfully removed from the list of processes to destroy upon VM exit.
      Specified by:
      remove in interface ProcessDestroyer
      Parameters:
      process - the process to remove.
      Returns:
      true if the specified Process was successfully removed.
    • removeShutdownHook

      private void removeShutdownHook()
      Removes this ProcessDestroyer as a shutdown hook, uses reflection to ensure pre-JDK 1.3 compatibility
    • run

      public void run()
      Invoked by the VM when it is exiting.
      Specified by:
      run in interface Runnable
    • size

      public int size()
      Returns the number of registered processes.
      Specified by:
      size in interface ProcessDestroyer
      Returns:
      the number of register process.