Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 1374
  • Last Modified:

How can I keep a ProcessBuilder Process from locking up when processing output streams?

I've found numerous fixes posted for this issue online all stating that the processing must be done inside of a thread.  I setup the code to do this, but it still locks up.  BufferedReader.readLine() seems to be the root of the issue, whenever I'm stepping through the code in debug mode, things freeze up there.  The code is included below:

    public void method {
            ....
            proc = processBuilder.start();
            StreamGobbler outputGobbler = new StreamGobbler(proc.getInputStream(),"OUTPUT");
            StreamGobbler errorGobbler = new StreamGobbler(proc.getErrorStream(),"ERROR");
            outputGobbler.start();
            errorGobbler.start();
            proc.destroy();
            ....
    }

    class StreamGobbler extends Thread {
        InputStream is;
        String type;

        StreamGobbler(InputStream is, String type){
            this.is = is;
            this.type = type;
        }

        @Override
        public void run() {
            try {
                BufferedReader br = new BufferedReader(new InputStreamReader(is));
                String line = null;
                while((line = br.readLine()) != null){
                    System.out.println(type + ">" + line);
                }
                br.close();
            } catch (IOException ioe){
                ioe.printStackTrace();
            }
        }
    }

Open in new window

0
FerrousHombre
Asked:
FerrousHombre
  • 4
  • 4
  • 3
  • +1
2 Solutions
 
sweetfa2Commented:
Are you sure it is not the proc.destroy() causing your problem.

Should you not be waiting for completion of your gobbler threads before destroying proc?
0
 
FerrousHombreAuthor Commented:
I've tried calling proc.waitFor() before the proc.destroy() call, but it doesn't seem to make any difference, the code just chugs and chugs.
0
 
objectsCommented:
>             proc.destroy();

you shouldn't be calling destroy()

> whenever I'm stepping through the code in debug mode, things freeze up there.

it will block there while it waits for output from the process, that is expected. It will block until the process completes.

The problem would seem more likely to be with the process you are running. It sounds like it does not complete.
0
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
FerrousHombreAuthor Commented:
No, when I run the commands for that ProcessBuilder in the command line, they run very quickly.  Also, commenting out the proc.destroy() call didn't change anything.
0
 
objectsCommented:
running from the command line is not the same as running from exec()
whats the command?
0
 
FerrousHombreAuthor Commented:
It's just a .ksh script that returns the status of another process.

I realize that the command line is not the same as running from exec().  I mentioned that the command runs quickly there because you said it sounded like the command doesn't complete.  Also, I'm not running from exec(), I'm starting a Process via ProcessBuilder.start().
0
 
CEHJCommented:
What is the command when you run it from the command line
0
 
objectsCommented:
Be aware that exec()/ProcessBuilder do not use a shell and you need to manage the environment yourself
0
 
FerrousHombreAuthor Commented:
I did go through and make sure that the BufferedReader gets closed, the Process gets destroyed, etc.  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.
0
 
CEHJCommented:
>>BufferedReader.readLine() causes the program to stop responding, though.

That will block until more data are available to read
0
 
objectsCommented:
>  I still have a circumstance where BufferedReader.readLine() causes the program to stop responding, though.

As I mentioned earlier that will stop responding until the process completes. That is expected.

It is simply waiting for output from the process
0
 
CEHJCommented:
:)
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

  • 4
  • 4
  • 3
  • +1
Tackle projects and never again get stuck behind a technical roadblock.
Join Now