Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

ProcessHandler



The ProcessHandler class allows to:
  • Listen to the output and error streams of a Process
  • Write to the Process input stream
This class defer the actual work to a listener.

If you just want to listen to one output stream or error stream, you can use the StreamGobbler class. The ProcessHandler class uses the StreamGobbler class internally.


If you want to listen to both the output stream and the error stream of a Process, it will be easier to use directly the ProcessHandler class.


It is possible when creating the ProcessHandler to specify a Charset to read the input and output streams By default the UTF-8 charset will be used.

Architecture

The ProcessHandler will redirect the output and error streams of the Process to the listener:
processhandler3
If the constructor was set to allow writing to the Process input, it will also allow to write Strings to the input:
processhandler2

ProcessListener interface

The ProcessHandler.ProcessListener interface has three methods:
By default each listener is called in the same Thread as the reading of the InputStream, which means that if the stream receives a lot of inputs in a short time, some of them could be discarded by the listener, leading to problems. You can handle the listener in another thread to avoid this problem.


streamgobbler1

Usage

Creation

The constructor of the ProcessHandler can use a Process or a ProcessBuilder. In the case where a ProcessBuilder has been used, the ProcessHandler will create the Process when starting the handler

The constructor allows to specify if the handler will allow to write to the input stream of the Process.
processhandler1
For example:
   ProcessBuilder pb = new ProcessBuilder("my_executable.exe");
   ProcessHandler phandler = new ProcessHandler(pb, true);

Configuration

After the creation:
It will not be possible to configure the behavior after it has been started.

Starting the handler

Two methods allow to start the ProcessHandler:
  • The ProcessHandler.start() method start the handler. it will only do something if the Process already exist (depending on the constructor used too create the ProcessHandler)
  • The ProcessHandler.createProcessAndStart() method will create the Process (if it does not already exists (depending on the constructor used too create the ProcessHandler), and start the handler
For example with a Process:
   ProcessBuilder pb = new ProcessBuilder("my_executable.exe");
   try {
      Process process = pb.start();
      ProcessHandler phandler = new ProcessHandler(process);
      List<String> lines = new ArrayList<>
      phandler.setListener(new ProcessHandler.ProcessListener() {
      @Override
      public void readOut(String line) {
        lines.add(line);
      }
      
      @Override
      public void readOut(String line) {
      }      

      @Override
      public void close() {
      }
   });
   } catch (IOException e) {
   }
   phandler.start();
And with a ProcessBuilder:
   ProcessBuilder pb = new ProcessBuilder("my_executable.exe");
   try {
      ProcessHandler phandler = new ProcessHandler(pb);
      List<String> lines = new ArrayList<>
      phandler.setListener(new ProcessHandler.ProcessListener() {
      @Override
      public void readOut(String line) {
        lines.add(line);
      }
      
      @Override
      public void readOut(String line) {
      }      

      @Override
      public void close() {
      }
   });
   } catch (IOException e) {
   }
   phandler.start();

Handling the listener in another thread

Main Article: StreamGobbler

By default for each stream the listener is called in the same Thread as the reading of the InputStream, which means that if the stream receives a lot of inputs in a short time, some of them could be discarded by the listener, leading to problems.

The following methods allow to handle the ProcessHandler.ProcessListener.readOut(String) and ProcessHandler.ProcessListener.readOut(String) methods in another Thread using a Queue, which ensures that no event in the InputStream readings will be discarded:
streamgobbler2

Handling the writing in another thread

The following methods allow to handle the ProcessHandler.writeIn(String, boolean) method in another Thread using a Queue, which ensures that to event in the OutputStream writings will be discarded:
These methods will only have an effect if the constructor used was set to allow to write to the input stream of the Process.

Example

The following code will redirect the output of an executable (normally on the console) to a List of Strings:
   ProcessBuilder pb = new ProcessBuilder("my_executable.exe");
      
   ProcessHandler phandler = new ProcessHandler(pb);
   List<String> lines = new ArrayList<>
   phandler.setListener(new ProcessHandler.ProcessListener() {
      @Override
      public void readOut(String line) {
        lines.add(line);
      }
      
      @Override
      public void readOut(String line) {
      }      

      @Override
      public void close() {
      }
   });
   phandler.start();

See also


Categories: Lang | Packages

Copyright 2006-2024 Herve Girod. All Rights Reserved. Documentation and source under the LGPL v2 and Apache 2.0 licences