org.zeroturnaround.process
Class ProcessUtil

java.lang.Object
  extended by org.zeroturnaround.process.ProcessUtil

public class ProcessUtil
extends Object

Additional helper methods for killing processes and waiting until they finish. Here all methods that use a timeout throw TimeoutException including the given timeout in the message instead of returning a false like SystemProcess does. Also all methods log a message in case the operation succeeded including the time it took.

Notice that methods that destroy a process don't include destroying operation itself in the timeout period. They start measuring time after sending the destroy signal. Also if the current thread gets interrupted it may also interrupt sending the destroy signal itself not just the waiting period after it. So if the current thread gets interrupted there's no guarantee that the target process actually got signaled.

See Also:
SystemProcess

Constructor Summary
ProcessUtil()
           
 
Method Summary
static void destroyForcefullyAndWait(SystemProcess process)
          Destroys the given process forcefully and waits until it finishes or the current thread gets interrupted.
static void destroyForcefullyAndWait(SystemProcess process, long timeout, TimeUnit unit)
          Destroys the given process forcefully and waits until it finishes, a timeout occurs or the current thread gets interrupted.
static void destroyGracefullyAndWait(SystemProcess process)
          Destroys the given process gracefully and waits until it finishes or the current thread gets interrupted.
static void destroyGracefullyAndWait(SystemProcess process, long timeout, TimeUnit unit)
          Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread gets interrupted.
static void destroyGracefullyOrForcefullyAndWait(SystemProcess process)
          Destroys the given process gracefully and waits until it finishes or the current thread gets interrupted.
static void destroyGracefullyOrForcefullyAndWait(SystemProcess process, long gracefulTimeout, TimeUnit gracefulTimeoutUnit)
          Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread gets interrupted.
static void destroyGracefullyOrForcefullyAndWait(SystemProcess process, long gracefulTimeout, TimeUnit gracefulTimeoutUnit, long forcefulTimeout, TimeUnit forcefulTimeoutUnit)
          Destroys the given process gracefully and waits until it finishes, first timeout occurs or the current thread gets interrupted.
static void waitFor(SystemProcess process)
          Waits until the given process finishes or the current thread gets interrupted.
static void waitFor(SystemProcess process, long timeout, TimeUnit unit)
          Waits until the given process finishes, a timeout occurs or the current thread gets interrupted.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

ProcessUtil

public ProcessUtil()
Method Detail

waitFor

public static void waitFor(SystemProcess process)
                    throws InterruptedException
Waits until the given process finishes or the current thread gets interrupted.

Parameters:
process - the target process.
Throws:
InterruptedException - if the current thread gets interrupted.

waitFor

public static void waitFor(SystemProcess process,
                           long timeout,
                           TimeUnit unit)
                    throws InterruptedException,
                           TimeoutException
Waits until the given process finishes, a timeout occurs or the current thread gets interrupted.

Parameters:
process - the target process.
timeout - the maximum time to wait until the process finishes.
unit - the time unit of the timeout argument.
Throws:
InterruptedException - if the current thread gets interrupted.
TimeoutException - the timeout elapsed before the process finished.

destroyGracefullyAndWait

public static void destroyGracefullyAndWait(SystemProcess process)
                                     throws IOException,
                                            InterruptedException
Destroys the given process gracefully and waits until it finishes or the current thread gets interrupted.

Parameters:
process - the target process.
Throws:
InterruptedException - if the current thread gets interrupted.
IOException

destroyGracefullyAndWait

public static void destroyGracefullyAndWait(SystemProcess process,
                                            long timeout,
                                            TimeUnit unit)
                                     throws IOException,
                                            InterruptedException,
                                            TimeoutException
Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread gets interrupted.

Parameters:
process - the target process.
timeout - the maximum time to wait until the process finishes.
unit - the time unit of the timeout argument.
Throws:
InterruptedException - if the current thread gets interrupted.
TimeoutException - the timeout elapsed before the process finished.
IOException

destroyForcefullyAndWait

public static void destroyForcefullyAndWait(SystemProcess process)
                                     throws IOException,
                                            InterruptedException
Destroys the given process forcefully and waits until it finishes or the current thread gets interrupted.

Parameters:
process - the target process.
Throws:
InterruptedException - if the current thread gets interrupted.
IOException

destroyForcefullyAndWait

public static void destroyForcefullyAndWait(SystemProcess process,
                                            long timeout,
                                            TimeUnit unit)
                                     throws IOException,
                                            InterruptedException,
                                            TimeoutException
Destroys the given process forcefully and waits until it finishes, a timeout occurs or the current thread gets interrupted.

Parameters:
process - the target process.
timeout - the maximum time to wait until the process finishes.
unit - the time unit of the timeout argument.
Throws:
InterruptedException - if the current thread gets interrupted.
TimeoutException - the timeout elapsed before the process finished.
IOException

destroyGracefullyOrForcefullyAndWait

public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process)
                                                 throws IOException,
                                                        InterruptedException
Destroys the given process gracefully and waits until it finishes or the current thread gets interrupted. If the gracefully destroying operation throws an exception (e.g. it's unsupported) it destroys the process forcefully and waits until it finishes or the current thread gets interrupted.

Parameters:
process - the target process.
Throws:
InterruptedException - if the current thread gets interrupted.
IOException

destroyGracefullyOrForcefullyAndWait

public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process,
                                                        long gracefulTimeout,
                                                        TimeUnit gracefulTimeoutUnit)
                                                 throws IOException,
                                                        InterruptedException
Destroys the given process gracefully and waits until it finishes, a timeout occurs or the current thread gets interrupted. If the gracefully destroying operation throws an exception (e.g. it's unsupported) or a timeout is reached it destroys the process forcefully and waits until it finishes or the current thread gets interrupted (no timeout is used in this case).

Parameters:
process - the target process.
timeout - the maximum time to wait until the process finishes after the gracefully destroying operation.
unit - the time unit of the timeout argument.
Throws:
InterruptedException - if the current thread gets interrupted.
IOException

destroyGracefullyOrForcefullyAndWait

public static void destroyGracefullyOrForcefullyAndWait(SystemProcess process,
                                                        long gracefulTimeout,
                                                        TimeUnit gracefulTimeoutUnit,
                                                        long forcefulTimeout,
                                                        TimeUnit forcefulTimeoutUnit)
                                                 throws IOException,
                                                        InterruptedException,
                                                        TimeoutException
Destroys the given process gracefully and waits until it finishes, first timeout occurs or the current thread gets interrupted. If the gracefully destroying operation throws an exception (e.g. it's unsupported) or a timeout is reached it destroys the process forcefully and waits until it finishes, second timeout occurs or the current thread gets interrupted.

Parameters:
process - the target process.
gracefulTimeout - the maximum time to wait until the process finishes after the gracefully destroying operation.
gracefulTimeoutUnit - the time unit of the first timeout argument.
forcefulTimeout - the maximum time to wait until the process finishes after the forcefully destroying operation.
forcefulTimeoutUnit - the time unit of the second timeout argument.
Throws:
InterruptedException - if the current thread gets interrupted.
TimeoutException - the timeout elapsed before the process finished after the forcefully destroying operation.
IOException


Copyright © 2015 ZeroTurnaround. All rights reserved.