public class ProcessUtilImpl extends AbstractLoggableComponent implements ProcessUtil
ProcessUtil
interface. execute
-methods spin up multiple threads
, especially when multiple processes are
piped (2*n+1[+1] threads). Therefore you should NOT use the singleton
variant of
this util except you are writing a simple command-line client that does a simple job and then terminates.
When writing a server-application or library, that makes such calls repetitive, you should create your own
instance and configure
a thread-pool as
Executor
.Modifier and Type | Class and Description |
---|---|
protected static class |
ProcessUtilImpl.AsyncProcessExecutorImpl
This inner class is the default implementation of the AsyncProcessExecutor.
|
protected class |
ProcessUtilImpl.ProcessExecutor
This inner class is does the actual execution of the
Process (es). |
Modifier and Type | Field and Description |
---|---|
private Executor |
executor |
private static ProcessUtil |
instance |
private StreamUtil |
streamUtil |
Constructor and Description |
---|
ProcessUtilImpl()
The constructor.
|
Modifier and Type | Method and Description |
---|---|
protected void |
doInitialize()
This method performs the actual
initialization . |
int |
execute(ProcessContext context,
long timeout,
TimeUnit unit,
ProcessBuilder... builders)
This method executes the external
Process es configured by the given builders . |
int |
execute(ProcessContext context,
ProcessBuilder... builders)
This method executes the external
Process es configured by the given builders . |
AsyncProcessExecutor |
executeAsync(ProcessContext context,
ProcessBuilder... builders)
This method executes the external
Process es configured by the given builders as async
task. |
protected Executor |
getExecutor()
This method gets the
Executor used to run asynchronous tasks. |
static ProcessUtil |
getInstance()
This method gets the singleton instance of this
ProcessUtilImpl . |
protected StreamUtil |
getStreamUtil()
This method gets the stream-util that is used by this process-util.
|
void |
setExecutor(Executor executor)
This method sets the
executor . |
void |
setStreamUtil(StreamUtil streamUtil)
This method sets the
stream-util . |
createLogger, getLogger
doInitialized, getInitializationState, initialize
private static ProcessUtil instance
private StreamUtil streamUtil
private Executor executor
public static ProcessUtil getInstance()
ProcessUtilImpl
. Cdi.GET_INSTANCE
before using.protected void doInitialize()
AbstractComponent
initialization
. It is called when AbstractComponent.initialize()
is
invoked for the first time. super.
AbstractComponent.doInitialize()
.doInitialize
in class AbstractLoggableComponent
protected Executor getExecutor()
Executor
used to run asynchronous tasks. It may use a thread-pool.@Inject public void setExecutor(Executor executor)
executor
.executor
- the executor to set.protected StreamUtil getStreamUtil()
@Inject public void setStreamUtil(StreamUtil streamUtil)
stream-util
.streamUtil
- the streamUtil to setpublic int execute(ProcessContext context, ProcessBuilder... builders) throws IOException, InterruptedException
ProcessUtil
Process
es configured by the given builders
. If more
than one builder
is given, the according processes are piped. execute
in interface ProcessUtil
context
- is the context of the process pipe (fist stdin
, last stdout
and
stderr
for all processes as well as a potential timeout).builders
- are the configurations of the Process
(es) to execute. The array needs to have a
length greater than zero.exit-code
of the Process
-pipe configured by the given
builders
.IOException
- if an input/output-error occurred.InterruptedException
- if the calling Thread
was interrupted while waiting for
a Process
to complete.public int execute(ProcessContext context, long timeout, TimeUnit unit, ProcessBuilder... builders) throws IOException, TimeoutException, InterruptedException
ProcessUtil
Process
es configured by the given builders
. If more
than one builder
is given, the according processes are piped. threads
, especially when multiple processes are piped
(2*n+1[+1] threads). Therefore you should NOT use the
singleton
variant of this util except
you are writing a simple command-line client that does a simple job and then terminates. When writing a
server-application or library, that makes such calls repetitive, you should create your own instance of
ProcessUtil
and configure a thread-pool as Executor
.execute
in interface ProcessUtil
context
- is the context of the process pipe (fist stdin
, last stdout
and
stderr
for all processes as well as a potential timeout).timeout
- is the maximum amount of time to wait for the Process
-pipe to finish.unit
- is the TimeUnit
of the given timeout
argument.builders
- are the configurations of the Process
(es) to execute. The array needs to have a
length greater than zero.exit-code
of the Process
-pipe configured by the given
builders
.IOException
- if an input/output-error occurred.TimeoutException
- if the Process
-pipe did NOT complete before the given timeout
(according to unit
).InterruptedException
- if the calling Thread
was interrupted while waiting for the
Process
-pipe to complete and before the timeout
occurred.public AsyncProcessExecutor executeAsync(ProcessContext context, ProcessBuilder... builders) throws IOException
ProcessUtil
Process
es configured by the given builders
as async
task. If more than one builder
is given, the according processes are piped. threads
, especially when multiple processes are piped
(2*n+1[+1] threads). Therefore you should NOT use the
singleton
variant of this util except
you are writing a simple command-line client that does a simple job and then terminates. When writing a
server-application or library, that makes such calls repetitive, you should create your own instance of
ProcessUtil
and configure a thread-pool as Executor
.executeAsync
in interface ProcessUtil
context
- is the context of the process pipe (fist stdin
, last stdout
and
stderr
for all processes as well as a potential timeout).builders
- are the configurations of the Process
(es) to execute. The array needs to have a
length greater than zero.exit-code
of the Process
-pipe configured by the given
builders
.IOException
- if an input/output-error occurred while setting up the Process
(es).Copyright © 2001–2016 mmm-Team. All rights reserved.