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 void
close()
Close and cleanup the process.void
destroy()
Destroy the process immediate.java.lang.Integer
getExitValue()
Get the exit value for the process.java.io.OutputStream
getInputStream()
Returns the output stream connected to the process input stream.java.lang.Long
getPid()
Get the process idprotected java.lang.Process
getProcess()
Get the processjava.lang.ProcessHandle
getProcessHandle()
Get the process handle.protected IProcessLiveness
getProcessLiveness()
Get the process livenessjava.time.Instant
getStartTime()
Get the start time of the process.java.time.Duration
getTotalCpuDuration()
Get the total cpu time accumulated of the process.boolean
isAlive()
Check whether the process is alive.java.lang.String
toString()
void
tryDestroy()
Try to kill the process.int
waitFor()
Wait, if necessary, until the process is terminated.boolean
waitFor(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:IProcess
Get the process id- Specified by:
getPid
in interfaceIProcess
- Overrides:
getPid
in 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:IProcess
Get the start time of the process.- Specified by:
getStartTime
in interfaceIProcess
- Overrides:
getStartTime
in classAbstractProcess
- Returns:
- the start time of the process
- See Also:
IProcess.getStartTime()
-
getTotalCpuDuration
public java.time.Duration getTotalCpuDuration()
Description copied from interface:IProcess
Get the total cpu time accumulated of the process.- Specified by:
getTotalCpuDuration
in interfaceIProcess
- Overrides:
getTotalCpuDuration
in classAbstractProcess
- Returns:
- the accumulated total cpu time
- See Also:
IProcess.getTotalCpuDuration()
-
getExitValue
public java.lang.Integer getExitValue()
Description copied from interface:IProcess
Get the exit value for the process.- Specified by:
getExitValue
in interfaceIProcess
- Overrides:
getExitValue
in classAbstractProcess
- Returns:
- the exit value of the process, by convention, the value
0
indicates normal termination. It return null if it has not exit by now. - See Also:
AbstractProcess.getExitValue()
-
waitFor
public int waitFor() throws java.lang.InterruptedException
Description copied from interface:IAsynchronousProcess
Wait, 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:
waitFor
in interfaceIAsynchronousProcess
- Returns:
- the exit value, by convention, the value
0
indicates 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 anInterruptedException
is thrown.- See Also:
IAsynchronousProcess.waitFor()
-
waitFor
public boolean waitFor(long timeout, java.util.concurrent.TimeUnit unit) throws java.lang.InterruptedException
Description copied from interface:IAsynchronousProcess
Wait, 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
exitValue
to 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:
waitFor
in interfaceIAsynchronousProcess
- Parameters:
timeout
- the maximum time to waitunit
- the time unit of thetimeout
argument- 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:IAsynchronousProcess
Check whether the process is alive.- Specified by:
isAlive
in interfaceIAsynchronousProcess
- Returns:
- true if it is alive; othweise false
- See Also:
IAsynchronousProcess.isAlive()
-
tryDestroy
public void tryDestroy()
Description copied from interface:IAsynchronousProcess
Try to kill the process.- Specified by:
tryDestroy
in interfaceIAsynchronousProcess
- See Also:
IAsynchronousProcess.tryDestroy()
-
destroy
public void destroy()
Description copied from interface:IAsynchronousProcess
Destroy the process immediate. If the process is not alive, no action is taken.- Specified by:
destroy
in interfaceIAsynchronousProcess
- See Also:
IAsynchronousProcess.destroy()
-
getInputStream
public java.io.OutputStream getInputStream()
Description copied from interface:IAsynchronousProcess
Returns 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:
getInputStream
in interfaceIAsynchronousProcess
- Returns:
- the input stream
- See Also:
IAsynchronousProcess.getInputStream()
-
getProcessHandle
public java.lang.ProcessHandle getProcessHandle()
Description copied from interface:IAsynchronousProcess
Get the process handle.- Specified by:
getProcessHandle
in interfaceIAsynchronousProcess
- Returns:
- the process handle
- See Also:
IAsynchronousProcess.getProcessHandle()
-
close
public void close()
Description copied from interface:IAsynchronousProcess
Close and cleanup the process. If the process is not finalised the close wait until the process is finished.- Specified by:
close
in interfacejava.lang.AutoCloseable
- Specified by:
close
in interfaceIAsynchronousProcess
- See Also:
IAsynchronousProcess.close()
-
getProcessLiveness
protected IProcessLiveness getProcessLiveness()
Get the process liveness- Returns:
- the process liveness
-
toString
public java.lang.String toString()
- Overrides:
toString
in classAbstractProcess
- See Also:
AbstractProcess.toString()
-
getProcess
protected java.lang.Process getProcess()
Get the process- Returns:
- the process
-
-