I am currently developing a servlet to be integrated with a third-party application. At one point, my servlet instantiates a class that calls an external application using Runtime.exec(). Then it calls Process.waitFor() to wait for the external process to complete.
For large input files, this external process can take as much as ten minutes to run. During that time, the entire web application becomes unresponsive, even the parts that have nothing to do with my servlet. The web application slows to a crawl until the external process completes and waitFor() returns. I am trying to determine the reason for this behavior.
While it is possible that the delay is caused by the external process using too much memory and/or CPU time, I just want to know if it is generally a bad idea to call Process.waitFor() from a servlet (or from another class that is instantiated by the servlet). Will that in itself cause the entire web application to hang? Note that the external process being called is a native application, not a Java app.
Thanks in advance, any and all help is greatly appreciated.
When you call the "waitFor" method, that will indeed "wait" for the other process to complete. So yes, it will make the thread which is doing the waiting hang. And for a particular user, that will make the application appear to hang. It won't make any of the other request threads hang, though. Unless they too are waiting for processes to complete.
Edit: oh yes, I forgot to mention, in answer to your question, that anything that causes the response time for a request to be ten minutes is a bad practice.
Joined: May 15, 2009
Thanks for your response. After some further reading on this subject, I realized that while waitFor is OK to use in a servlet, it does have some pitfalls, which I am now accounting for. For example, I don't think my external process will produce any console output, but now I make sure to consume any such output as a precaution to keep the process from blocking. I also spent some time monitoring the resource consumption by the external process while it was running, and realized that it was eating up a lot of memory on the application server. I think that's probably what led to the performance degradation I witnessed.
You are correct, that a servlet should never take that long to respond. I should have clarified that in my case, the servlet responds right away before running the external process, so the client immediately gets an acknowledgment that the request was received and is being processed.