Class AsynchronousProcess
- java.lang.Object
-
- com.github.toolarium.system.command.process.impl.AbstractProcess
-
- com.github.toolarium.system.command.process.impl.AsynchronousProcess
-
- All Implemented Interfaces:
IAsynchronousProcess,IProcess,java.lang.AutoCloseable
public class AsynchronousProcess extends AbstractProcess implements IAsynchronousProcess, java.lang.AutoCloseable
Implements theIAsynchronousProcess.
-
-
Constructor Summary
Constructors Constructor Description AsynchronousProcess(ISystemCommandGroupList systemCommandGroupList, IProcessLiveness processLiveness)Constructor for AsynchrounousProcess
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description voidclose()Close and cleanup the process.voiddestroy()Destroy the process immediate.java.lang.IntegergetExitValue()Get the exit value for the process.java.io.OutputStreamgetInputStream()Returns the output stream connected to the process input stream.java.lang.LonggetPid()Get the process idprotected java.lang.ProcessgetProcess()Get the processjava.lang.ProcessHandlegetProcessHandle()Get the process handle.protected IProcessLivenessgetProcessLiveness()Get the process livenessjava.time.InstantgetStartTime()Get the start time of the process.java.time.DurationgetTotalCpuDuration()Get the total cpu time accumulated of the process.booleanisAlive()Check whether the process is alive.java.lang.StringtoString()voidtryDestroy()Try to kill the process.intwaitFor()Wait, if necessary, until the process is terminated.booleanwaitFor(long timeout, java.util.concurrent.TimeUnit unit)Wait, if necessary, until the process is terminated.-
Methods inherited from class com.github.toolarium.system.command.process.impl.AbstractProcess
equals, getSystemCommandGroupList, hashCode
-
Methods inherited from class java.lang.Object
clone, finalize, getClass, notify, notifyAll, wait, wait, wait
-
Methods inherited from interface com.github.toolarium.system.command.process.IProcess
getSystemCommandGroupList
-
-
-
-
Constructor Detail
-
AsynchronousProcess
public AsynchronousProcess(ISystemCommandGroupList systemCommandGroupList, IProcessLiveness processLiveness)
Constructor for AsynchrounousProcess- Parameters:
systemCommandGroupList- the system command group listprocessLiveness- the process liveness
-
-
Method Detail
-
getPid
public java.lang.Long getPid()
Description copied from interface:IProcessGet the process id- Specified by:
getPidin interfaceIProcess- Overrides:
getPidin classAbstractProcess- Returns:
- the native process id of the process or null if unknown
- See Also:
IProcess.getPid()
-
getStartTime
public java.time.Instant getStartTime()
Description copied from interface:IProcessGet the start time of the process.- Specified by:
getStartTimein interfaceIProcess- Overrides:
getStartTimein classAbstractProcess- Returns:
- the start time of the process
- See Also:
IProcess.getStartTime()
-
getTotalCpuDuration
public java.time.Duration getTotalCpuDuration()
Description copied from interface:IProcessGet the total cpu time accumulated of the process.- Specified by:
getTotalCpuDurationin interfaceIProcess- Overrides:
getTotalCpuDurationin classAbstractProcess- Returns:
- the accumulated total cpu time
- See Also:
IProcess.getTotalCpuDuration()
-
getExitValue
public java.lang.Integer getExitValue()
Description copied from interface:IProcessGet the exit value for the process.- Specified by:
getExitValuein interfaceIProcess- Overrides:
getExitValuein classAbstractProcess- Returns:
- the exit value of the process, by convention, the value
0indicates normal termination. It return null if it has not exit by now. - See Also:
AbstractProcess.getExitValue()
-
waitFor
public int waitFor() throws java.lang.InterruptedExceptionDescription copied from interface:IAsynchronousProcessWait, if necessary, until the process is terminated. This method returns immediately if the process has already terminated. If the process has not yet terminated, the calling thread will be blocked until the process exits.- Specified by:
waitForin interfaceIAsynchronousProcess- Returns:
- the exit value, by convention, the value
0indicates normal termination - Throws:
java.lang.InterruptedException- if the current thread is interrupted by another thread while it is waiting, then the wait is ended and anInterruptedExceptionis thrown.- See Also:
IAsynchronousProcess.waitFor()
-
waitFor
public boolean waitFor(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedExceptionDescription copied from interface:IAsynchronousProcessWait, if necessary, until the process is terminated. This method returns immediately if the process has already terminated or the specified waiting time elapses.The default implementation of this methods polls the
exitValueto check if the process has terminated. Concrete implementations of this class are strongly encouraged to override this method with a more efficient implementation.- Specified by:
waitForin interfaceIAsynchronousProcess- Parameters:
timeout- the maximum time to waitunit- the time unit of thetimeoutargument- Returns:
- true if the process has exited; othweise false if the waiting time elapsed before the process has exited
- Throws:
java.lang.InterruptedException- if the current thread is interrupted while waiting.- See Also:
IAsynchronousProcess.waitFor(long, java.util.concurrent.TimeUnit)
-
isAlive
public boolean isAlive()
Description copied from interface:IAsynchronousProcessCheck whether the process is alive.- Specified by:
isAlivein interfaceIAsynchronousProcess- Returns:
- true if it is alive; othweise false
- See Also:
IAsynchronousProcess.isAlive()
-
tryDestroy
public void tryDestroy()
Description copied from interface:IAsynchronousProcessTry to kill the process.- Specified by:
tryDestroyin interfaceIAsynchronousProcess- See Also:
IAsynchronousProcess.tryDestroy()
-
destroy
public void destroy()
Description copied from interface:IAsynchronousProcessDestroy the process immediate. If the process is not alive, no action is taken.- Specified by:
destroyin interfaceIAsynchronousProcess- See Also:
IAsynchronousProcess.destroy()
-
getInputStream
public java.io.OutputStream getInputStream()
Description copied from interface:IAsynchronousProcessReturns the output stream connected to the process input stream. If the standard input of the process has been redirected, the stream is not available.- Specified by:
getInputStreamin interfaceIAsynchronousProcess- Returns:
- the input stream
- See Also:
IAsynchronousProcess.getInputStream()
-
getProcessHandle
public java.lang.ProcessHandle getProcessHandle()
Description copied from interface:IAsynchronousProcessGet the process handle.- Specified by:
getProcessHandlein interfaceIAsynchronousProcess- Returns:
- the process handle
- See Also:
IAsynchronousProcess.getProcessHandle()
-
close
public void close()
Description copied from interface:IAsynchronousProcessClose and cleanup the process. If the process is not finalised the close wait until the process is finished.- Specified by:
closein interfacejava.lang.AutoCloseable- Specified by:
closein interfaceIAsynchronousProcess- See Also:
IAsynchronousProcess.close()
-
getProcessLiveness
protected IProcessLiveness getProcessLiveness()
Get the process liveness- Returns:
- the process liveness
-
toString
public java.lang.String toString()
- Overrides:
toStringin classAbstractProcess- See Also:
AbstractProcess.toString()
-
getProcess
protected java.lang.Process getProcess()
Get the process- Returns:
- the process
-
-