aspose file tools*
The moose likes Servlets and the fly likes Servlet does not redirect after waitFor() call on java.lang.Process object Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Servlets
Bookmark "Servlet does not redirect after waitFor() call on java.lang.Process object" Watch "Servlet does not redirect after waitFor() call on java.lang.Process object" New topic
Author

Servlet does not redirect after waitFor() call on java.lang.Process object

Andrei Mura
Greenhorn

Joined: Nov 26, 2009
Posts: 18
Hello everyone. I have a problem with servlets that I cannot resolve. Bellow is my scenario:

In my servlet's post method I have the following:



The Processing.logoutSubscriberByMac(mac) has that code on it:



The problem is that if things are like how are presented above, the servlet does not redirect the page: response.sendRedirect("/page/to/redirect");. Even if the logs are written logger.info("subscriber with mac ..."), the browser tries to redirect, but it gets stuck on redirection and after loading timeout, the browser's failure page appears. If I comment the line that contains command to wait, proc.waitFor();, everything works well.

Question: Can anybody explain me why that behavior? Do you have any suggestions for that scenario?

Note: it seems to me that Process.waitFor() "steals" servlet's OutputStream. But I might be wrong.
Muhammad Saifuddin
Ranch Hand

Joined: Dec 06, 2005
Posts: 1321

Article Why Runtime.exec() hangs worth a read.


Saifuddin..
[Blog][Linkedin] How To Ask Questions On JavaRanch My OpenSource
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
You are not correctly handling the Process stderr and stdout streams so Process is hanging - see the JavaDocs for Process and the more complete explanation in the article Muhammad cited.

Bill
Andrei Mura
Greenhorn

Joined: Nov 26, 2009
Posts: 18
Muhammad Saifuddin wrote:Article Why Runtime.exec() hangs worth a read.


I already read that article. Thanks anyway .

Look bellow at my code, modified according to the pointed article, but the servlet still hangs on redirect.

Andrei Mura
Greenhorn

Joined: Nov 26, 2009
Posts: 18
William Brogden wrote:You are not correctly handling the Process stderr and stdout streams so Process is hanging - see the JavaDocs for Process and the more complete explanation in the article Muhammad cited.

Bill


From Javadocs you can read: Because some native platforms only provide limited buffer size for standard input and output streams, failure to promptly write the input stream or read the output stream of the subprocess may cause the subprocess to block, or even deadlock.

If you look at my post in response to Muhammad Saifuddin, I think it is clear that:

1. My process does not hang as long as the logs are written to files. (I refer to logs that are bellow process.waitFor() call, in servlet doPost(..) method).
2. Taking in account what Javadocs sais : All its standard I/O (i.e. stdin, stdout, stderr) operations will be redirected to the parent process, where they can be accessed via the streams obtained using the methods getOutputStream(), getInputStream(), and getErrorStream(), it seems that closing process's streams you close servlets streams.

If you have some suggestions to guide me, help me please.
William Brogden
Author and all-around good cowpoke
Rancher

Joined: Mar 22, 2000
Posts: 12835
    
    5
it seems that closing process's streams you close servlets streams.


No

Your code still does not handle the Process streams correctly because you try to do all that in a single Thread. Read the article again, this time, all the way to listing 4.5 which shows how to handle each stream in its own Thread.

Bill
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Servlet does not redirect after waitFor() call on java.lang.Process object