0 Replies Latest reply on Jun 10, 2013 12:40 PM by meandmycode

    Use output from ProcessBuilder to another ProcessBuilder

    meandmycode

      Hi,

       

      We send two commands to the ProcessBuilder:

       

      1. monitor.sh and outfile is /home/${userid}/monitor.log

       

      Now we wait for a while until we send the second command:

       

      2. decoder.sh input file: is /home/${userid}/monitor.log and the output is: /home/${userid}/decoder.log

       

      They are all sent to a separate worker thread ( TeRunnerTask2).

       

      When I start this I can see ( using visualvm) that monitor thread is in Running STATE ( green) and then when when I start the decoder in a new task if is also in running state for s short while and the it goes into into waiting STATE.

       

      Both monitor and decoder script is running on host and output is still written to monitor.log ( I check with tail -f ).

      So there is still work to be done but it seems like the streams don't get more data.

       

      Any ideas?

       

      br,

       

      //mike

       

       

       

      package server;

       

      import java.io.BufferedReader;

      import java.io.BufferedWriter;

      import java.io.File;

      import java.io.FileWriter;

      import java.io.InputStream;

      import java.io.InputStreamReader;

      import java.lang.ProcessBuilder.Redirect;

      import java.util.concurrent.Callable;

       

       

       

      public class TeRunnerTask2 implements Callable<Boolean> {

          private final static String TERMINATE = "<---TERMINATED--->";

          BufferedWriter processWriter;

          TeBuilder2 teBuilder;

          Process process;

          BufferedReader processOutStream;

          ProcessBuilder processBuilder;

         

          boolean run = true;

          public TeRunnerTask2(TeBuilder2 teBuilder) {

              this.teBuilder = teBuilder;

          }

          @Override

          public Boolean call() throws Exception {

              Thread.currentThread().setName(teBuilder.getName());

              System.out.println("Thread run2: '" + teBuilder.getName()

                      + "', command: '" + teBuilder.getCommand() + "', inFile: '"

                      + teBuilder.getInFile() + "'" + "', outFile: '" + teBuilder.getOutFile()

                      + "'");

              // FileOutputStream fileStream;

              InputStream fileStream = null;

              processBuilder = new ProcessBuilder();

       

              if (teBuilder.getInFile() != null) {

                  System.out.println("open fileInputStream, redirect stream");

             

                  File inFile = new File(teBuilder.getInFile());               

                  Redirect rf = ProcessBuilder.Redirect.from(inFile);

                  processBuilder.redirectInput(rf);

              }

       

              processBuilder.command(teBuilder.getCommand().split(" "));

              processBuilder.redirectErrorStream(true);

              //processBuilder.inheritIO();

       

              process = processBuilder.start();

              processWriter = new BufferedWriter(new FileWriter(teBuilder.getOutFile()));

       

              processOutStream = new BufferedReader(new InputStreamReader(process.getInputStream()));

         

             

              String s = null;

              while (run == true ) {

                  s = processOutStream.readLine();

                  if (s != null) {

                      if (teBuilder.getName().contains("flow")) {

                          System.out.println("flow:" + s + "\n");

                      }

                      processWriter.write(s);

                      processWriter.newLine();

                      processWriter.flush();

                      if (s.contains(TERMINATE)) {

                          System.out.println("terminate string found");

                          run = false;

                      }

                  }

                  else{

                      System.out.println("nothing to do: " + teBuilder.getName());

                      Thread.sleep(1000);

                  }   

              }   

              System.out.println("job done: " + teBuilder.getName() + ", run: "

                      + run);

              process.waitFor();

              processWriter.close();

              processOutStream.close();

              process.destroy();

             

             

              return true;

          }

      }