You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Andrei Petru Mura <ma...@gmail.com> on 2012/11/21 10:42:49 UTC
Page not redirected after waitFor() call on Process object.
Hello,
My issue can be related to Servlets, but it also can be related to Tomcat.
I'm using tomcat 7.0.27.
In my servlet's post method I have the following:
protected void doPost(HttpServletRequest request, HttpServletResponse
response) throws ServletException, IOException {
if
(this.hasCredential((String)request.getSession().getAttribute("auth_cookie")))
{
String mac = request.getParameter("subscriber_mac");
Processing.logoutSubscriberByMac(mac); //this is important
request.getSession().setAttribute("subscriber_id",
request.getParameter("subscriber_id"));
response.sendRedirect("/page/to/redirect");
myLogger.info("subscriber with mac " + mac + " logged out by
administrator " + request.getSession().getAttribute("username") + " from
host " + request.getRemoteAddr()); //take care of this also
} else {
//treat this case ...
}
}
TheProcessing.logoutSubscriberByMac(mac) has that code on it:
public static void logoutSubscriberByMac(String mac) {
try {
Process proc = Runtime.getRuntime().exec("logout subscriber
command");
proc.waitFor(); //here is the problem
} catch (IOException e) {
//treat this error
} catch (InterruptedException e) {
//treat this error
}
}
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: mylogger.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.
Even more, I tried to get the exit value from Process and it returns 0.
Question: Do you have any idea where the problem comes from? Can you give
me some suggestions?
Note: I redirect all traffic that comes to Apache HTTP server to Tomcat
through AJP Connector. But I think that shouldn't represent an issue for
that scenario taking in account that all other redirects are working great.
Re: Page not redirected after waitFor() call on Process object.
Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/11/23 Christopher Schultz <ch...@christopherschultz.net>:
> On 11/21/12 4:42 AM, Andrei Petru Mura wrote:
>> TheProcessing.logoutSubscriberByMac(mac) has that code on it:
>>
>> public static void logoutSubscriberByMac(String mac) { try {
>>
>> Process proc = Runtime.getRuntime().exec("logout subscriber
>> command"); proc.waitFor(); //here is the problem
>
> You almost certainly have a problem with stream management: if you
> don't drain both the output stream and error stream of the process,
> then your code can hang. Likewise, if you don't close the standard
> input stream to the process, the child process may block waiting for
> input.
>
> It sounds silly, even if you use little or no standard input or output
> from your child process, but failure to manage these streams can and
> will hang up your Java code.
>
> You can either write multi-threaded code to handle all these streams,
> or you can use one of the several high-quality utility libraries out
> there that can wrap Runtime.exec for you and take care of this kind of
> stuff.
>
+1.
There was also such thread in June as
"Issue with keep-alive connections, when using APR Connector on
Windows and starting Processes from Servlets" where some JRE bugs were
mentioned.
http://markmail.org/thread/hsime5ayjdrg4fwm
I do not know much about your configuration to tell whether this is
affecting you or not.
Best regards,
Konstantin Kolinko
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Page not redirected after waitFor() call on Process object.
Posted by Christopher Schultz <ch...@christopherschultz.net>.
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
Andrei,
On 11/21/12 4:42 AM, Andrei Petru Mura wrote:
> TheProcessing.logoutSubscriberByMac(mac) has that code on it:
>
> public static void logoutSubscriberByMac(String mac) { try {
>
> Process proc = Runtime.getRuntime().exec("logout subscriber
> command"); proc.waitFor(); //here is the problem
You almost certainly have a problem with stream management: if you
don't drain both the output stream and error stream of the process,
then your code can hang. Likewise, if you don't close the standard
input stream to the process, the child process may block waiting for
input.
It sounds silly, even if you use little or no standard input or output
from your child process, but failure to manage these streams can and
will hang up your Java code.
You can either write multi-threaded code to handle all these streams,
or you can use one of the several high-quality utility libraries out
there that can wrap Runtime.exec for you and take care of this kind of
stuff.
- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG/MacGPG2 v2.0.17 (Darwin)
Comment: GPGTools - http://gpgtools.org
Comment: Using GnuPG with undefined - http://www.enigmail.net/
iEYEARECAAYFAlCvwUoACgkQ9CaO5/Lv0PCbcACfXgar8R/7jGDrV6Bjgd0FbXEM
OBcAn2aPFGndEjUmdOynJsjHbJF2RaqD
=QFpl
-----END PGP SIGNATURE-----
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org
Re: Page not redirected after waitFor() call on Process object.
Posted by Andrei Petru Mura <ma...@gmail.com>.
On Thu, Nov 22, 2012 at 10:33 AM, Konstantin Kolinko <knst.kolinko@gmail.com
> wrote:
> 2012/11/21 Andrei Petru Mura <ma...@gmail.com>:
> > In my servlet's post method I have the following:
> >
> > protected void doPost(HttpServletRequest request, HttpServletResponse
> > response) throws ServletException, IOException {
> >
> > if
> >
> (this.hasCredential((String)request.getSession().getAttribute("auth_cookie")))
> > {
> >
> > String mac = request.getParameter("subscriber_mac");
> >
> > Processing.logoutSubscriberByMac(mac); //this is important
>
The process execution (call to Processing.logoutSubscriberByMac(mac))
normally should take less than 1 millisecond.
> >
> > request.getSession().setAttribute("subscriber_id",
> > request.getParameter("subscriber_id"));
> >
> > response.sendRedirect("/page/to/redirect");
> >
> > myLogger.info("subscriber with mac " + mac + " logged out by
> > administrator " + request.getSession().getAttribute("username") + " from
> > host " + request.getRemoteAddr()); //take care of this also
>
the logs are filled in files immediatelly after that servlet is loaded.
> >
> > } else {
> > //treat this case ...
> > }
> >
> > }
> >
> > TheProcessing.logoutSubscriberByMac(mac) has that code on it:
> >
> > public static void logoutSubscriberByMac(String mac) {
> > try {
> >
> > Process proc = Runtime.getRuntime().exec("logout subscriber
> > command");
>
again, if the command is run in CLI, it takes laess than 1 millisecond.
> > proc.waitFor(); //here is the problem
> >
> > 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: mylogger.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.
>
The browser tries to redirect for about 30 second.
> If I comment the line
> > that contains command to wait, proc.waitFor();, everything works well.
> > Even more, I tried to get the exit value from Process and it returns 0.
> >
> > Question: Do you have any idea where the problem comes from? Can you give
> > me some suggestions?
>
> The browser times out = it closes the connection before the response
> arrives = it closes the connection before you write your response.
> There is no wonder that it does not see the response.
>
I doubt the process would take more time to succeed than browser's page
loading timeout only for the reason that I run that command from java.
Actually, if I check to see if command is fulfilled during page loading,
the result is ok.
N.B. I also managed the Process streams, (getInputStream, getErrorStream),
but without result.
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
> For additional commands, e-mail: users-help@tomcat.apache.org
>
>
Re: Page not redirected after waitFor() call on Process object.
Posted by Konstantin Kolinko <kn...@gmail.com>.
2012/11/21 Andrei Petru Mura <ma...@gmail.com>:
> Hello,
>
> My issue can be related to Servlets, but it also can be related to Tomcat.
> I'm using tomcat 7.0.27.
> In my servlet's post method I have the following:
>
> protected void doPost(HttpServletRequest request, HttpServletResponse
> response) throws ServletException, IOException {
>
> if
> (this.hasCredential((String)request.getSession().getAttribute("auth_cookie")))
> {
>
> String mac = request.getParameter("subscriber_mac");
>
> Processing.logoutSubscriberByMac(mac); //this is important
>
> request.getSession().setAttribute("subscriber_id",
> request.getParameter("subscriber_id"));
>
> response.sendRedirect("/page/to/redirect");
>
> myLogger.info("subscriber with mac " + mac + " logged out by
> administrator " + request.getSession().getAttribute("username") + " from
> host " + request.getRemoteAddr()); //take care of this also
>
> } else {
> //treat this case ...
> }
>
> }
>
> TheProcessing.logoutSubscriberByMac(mac) has that code on it:
>
> public static void logoutSubscriberByMac(String mac) {
> try {
>
> Process proc = Runtime.getRuntime().exec("logout subscriber
> command");
> proc.waitFor(); //here is the problem
>
> 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: mylogger.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.
> Even more, I tried to get the exit value from Process and it returns 0.
>
> Question: Do you have any idea where the problem comes from? Can you give
> me some suggestions?
The browser times out = it closes the connection before the response
arrives = it closes the connection before you write your response.
There is no wonder that it does not see the response.
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org