views:

16

answers:

0

I hava a Java programm that communicates with a C# console app through a pipe. In my dev environment it works like a charm but I am wondering if this is still reliable if the java app is running work days and weeks (It's for a POS Terminal).

What I basically do is: write one line -> read one line.

The C# Code is pretty simple

static void Main(string[] args)
{
    string line;
    do
    {
        line = Console.ReadLine();
        Console.WriteLine("Something else");
    } while (line != null); 
}

For the Java Programm i took this approach

  • Call the method with a boolean parameter retry = false
  • If something goes wrong and retry = false the terminate the process and Recall itself with retry = true
  • If retry = true throw exception

Here is the code:

 private static String GetDataFromPipe(String input, bool retry) throws IOException
 {
    try
    {
        input = input + "\r\n";
        byte[] bytes = input.getBytes();

        if (p == null)
            p = Runtime.getRuntime().exec("process.exe");

        OutputStream sdtOutput = p.getOutputStream();
        BufferedReader stdInput = new BufferedReader(new InputStreamReader(p.getInputStream()));

        sdtOutput.write(bytes);
        sdtOutput.flush();

        String result = stdInput.readLine();
        return result;
    }
    catch (Throwable ex)
    {
        if (p != null) { p.destroy(); }
        p = null;

        if (retry == false)
        {
            Log("GetDataFromPipe() failed (first time)", ex);
            return GetDataFromPipe(input, true);
        }

        throw new CustomException("GetDataFromPipe() failed", ex);            
    }

}

I think this will be pretty reliable. But maybe I am wrong. Are there any pitfalls I did not think of? (Java / Windows Limitations)