Class ProcessManager
- All Implemented Interfaces:
Iterable<ComponentType>,Channel,ComponentType,Eligible,Manager
A process is started
by firing a StartProcess event. In response, the
ProcessManager starts the process and creates a
ProcessManager.ProcessChannel (i.e. an {@link IOSubchannel) for communication
with the process. It fires an {@link Opening} and ProcessStarted
event on the newly created channel.
Data may be sent to the process’s stdin by firing Output
events on the ProcessManager.ProcessChannel. As usual, these events should
be fired using the channels response pipeline. Data generated by the process is provided by
Input events. In order to distinguish between stdout and stderr,
the events have an association with class FileDescriptor as
key and an associated value of 1 (stdout) or 2 (stderr).
When the process terminated, three Closed events are fired on
the ProcessManager.ProcessChannel one each for stdout and stderr (with the
same association as was used for the Input events) and a
as third event a ProcessExited (specialized Closed)
with the process’s exit value. Note that the sequence in which these
events are sent is undefined.
-
Nested Class Summary
Nested ClassesModifier and TypeClassDescriptionfinal classThe Class ProcessChannel.Nested classes/interfaces inherited from interface org.jgrapes.core.Channel
Channel.Default -
Field Summary
-
Constructor Summary
ConstructorsConstructorDescriptionCreates a new connector, using itself as component channel.ProcessManager(Channel componentChannel) Create a new instance using the given channel. -
Method Summary
Modifier and TypeMethodDescriptionvoidCloses the output to the process (the process’s stdin).voidHandles closed events from stdout and stderr.voidonOutput(Output<ByteBuffer> event, ProcessManager.ProcessChannel channel) Writes the data passed in the event.voidonStartProcess(StartProcess event) Start a new process using the data from the event.voidStop all running processes.setExecutorService(ExecutorService executorService) Sets an executor service to be used by the event pipelines that forward data from the process.Methods inherited from class org.jgrapes.core.Component
channel, component, defaultCriterion, isEligibleFor, setNameMethods inherited from class org.jgrapes.core.internal.ComponentVertex
activeEventPipeline, addHandler, attach, channelReplacements, children, componentPath, componentVertex, detach, fire, initComponentsHandlers, iterator, name, newEventPipeline, newEventPipeline, parent, registerAsGenerator, root, toString, unregisterAsGeneratorMethods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, wait, wait, waitMethods inherited from interface java.lang.Iterable
forEach, spliterator
-
Constructor Details
-
ProcessManager
public ProcessManager()Creates a new connector, using itself as component channel. -
ProcessManager
Create a new instance using the given channel.- Parameters:
componentChannel- the component channel
-
-
Method Details
-
setExecutorService
Sets an executor service to be used by the event pipelines that forward data from the process.- Parameters:
executorService- the executorService to set- Returns:
- the process manager for easy chaining
- See Also:
-
onStartProcess
Start a new process using the data from the event.- Parameters:
event- the event
-
onOutput
@Handler public void onOutput(Output<ByteBuffer> event, ProcessManager.ProcessChannel channel) throws InterruptedException, IOException Writes the data passed in the event.The end of record flag is used to determine if a channel is eligible for purging. If the flag is set and all output has been processed, the channel is purgeable until input is received or another output event causes the state to be reevaluated.
- Parameters:
event- the eventchannel- the channel- Throws:
InterruptedException- the interrupted exceptionIOException
-
onClose
Closes the output to the process (the process’s stdin).If the event has an association with key
Process, the event additionally causes the process to be “closed”, i.e. to be terminated (seeProcessHandle.destroy()).- Parameters:
event- the event- Throws:
IOException- if an I/O exception occurredInterruptedException- if the execution was interrupted
-
onStop
Stop all running processes.- Parameters:
event-
-
onClosed
@Handler(priority=-100) public void onClosed(Closed<?> event) throws IOException, InterruptedException Handles closed events from stdout and stderr.- Parameters:
event- the event- Throws:
IOException- if an I/O exception occurredInterruptedException- if the execution was interrupted
-