• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Will Process.waitFor() hang my web app?

 
Greenhorn
Posts: 13
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all,

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.
 
Marshal
Posts: 28193
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
N Carlo
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Paul,

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.

Thanks!
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic