0

I'm having an Process proc running which I need to destroy and recreate if an error occurs.
That is because of an issue with sending commands via stdin to the process which it can't resolve.
My workaround is to reset the "connection" I built(stdin,stdout,stderr) and get new streams from the new process. I'm using a thread to watch, when the process ends. If an error occurs, I shut the process down via a quit-command and interrupt the thread which causes it to finally close the streams.
The main-thread is already building up a new connection. When sending a command after rebuilding the connection an IOException states the stream is closed.
Is there a way I could copy the old stream to close it later on? I already tried it with wrapping it with another Reader but didn't succeed. I'm not sure if it's enough to get the process' streams via getXXXStream() and close them because this wouldn't close the readers above. Also I'm afraid, I could grab the new process and close it's streams.
I don't want it to block though.
EDIT:
As my first comment states, I close the streams immediately, but the problem with the Process remains. If I'm waiting 1000 ms for it to close and reassign proc with the new Process my thread operates with the new one (e.g. calls exitValue()).

Here's my Thread:

private class ProcEndWatcherThread extends Thread
{
    private BufferedReader out, err;
    private PrintWriter wr;

    public ProcEndWatcherThread(BufferedReader out, BufferedReader err,  `PrintWriter wr)`
    {
        this.out = out;
        this.err = err;
        this.wr = wr;

        //this.out = new BufferedReader(out);
        //this.err = new BufferedReader(err);
        //this.wr = new PrintWriter(wr);
    }
    @Override
    public void run()
    {
        boolean closed = false;
        try
        {
            isRunning = true;
            proc.waitFor();
            closed = true;
        }
        catch (InterruptedException e)
        {
            Thread.currentThread().interrupt();
        }
        finally
        {
            if(!closed)
                proc.destroy();         

            try {
                out.close();
                err.close();
            } catch (IOException e) {
                log.throwing("ProcEndWatcherThread", "run", e);
            }
            wr.close();
            isRunning = false;
            log.info("Exitvalue: " + proc.exitValue());
        }
    }
}

The method to build my connection(connect + reconnect):

private void initCon() throws RException, IOException
{
            proc = new ProcessBuilder(pathAndArgs).start();
            System.out.println(proc.toString());
            errRd = new BufferedReader(new InputStreamReader(
                    proc.getErrorStream()));
            outRd = new BufferedReader(new InputStreamReader(
                    proc.getInputStream()));
            BufferedWriter inWr = new BufferedWriter(new


OutputStreamWriter(new BufferedOutputStream(
                        proc.getOutputStream())));
            pWr = new PrintWriter(inWr);

            procEndWatcherThread = new ProcEndWatcherThread(outRd, errRd, pWr);
            procEndWatcherThread.start();
... some initializing
}
Zhedar
  • 3,480
  • 1
  • 21
  • 44
  • The process stops, then you start another one, but your outputstream still appears to be closed, isn't it ? Can you post the code that detects process termination and creates another process, and the code that pushes data to the outputstream ? – Pierre Nov 26 '12 at 22:40
  • I just changed the code to close the streams immediately. So I dont have to close them in the thread anymore. This actually works fine, because I wont need them anymore until the close. But I still have the problem, that I wait for the process to terminate in one thread and reassign a new Process to `proc`. So I can't assure the old process has stopped when the new one starts. – Zhedar Nov 26 '12 at 22:53

0 Answers0