Servlet request time out error
Hy all
our development environment is:Struts2.0,JSp,Servlet,HTML,JQUERY,
we have issue regarding the request time out in one of our struts action handler method.
Scenario of the issue is below:
Following method is our strut's action handler
public String processDataAndReturn() throws Exception
{
Process1() --in this process we calling server code for perform the some security process on the large data [approx. more than 100mb size data]
//Here Process1 Process on the large data and it is return resulted data...then we pass this resulted data to the Process2() for further processing
//At here we can not improve the Process1() speed...it is up to date.
//Here the process1() take approx. 4 minute for request processing, that's why we get the request timeout error in the browser.
HttpServlet request is time out but the following process is still going on,due to the request time out error ,user can not get the file for download.
Process2()---this process write that data to the file, and then we give that file to user .
return success
}
Above issue is not occurs when the data is not larger.It is working perfect for the small size data.
And for the solution we can not skip the process1() in any case.We get the above issue with more than 100mb size file process.
How can i solve the above issue?Is there any other development strategy to resolve the above issue?
any input will be appreciated for the issue.
Thanks in advance
Yatin Baraiya
Re: Servlet request time out error
So effectively you've got an asynchronous process but you're trying to treat it as a synchronous process.
My recommendation would be two fold. First, no user wants to wait 4 minutes for a result in a web environment. You're going to have users reloading and/or giving up and sending in bug reports. So, at the very least, send the request off from the browser to start the process and then, via JavaScript, poll the back end to see if the process is done. You only have to poll like every 10 seconds or so which isn't very expensive. Of course, that depends on many factors but without support of something like WebSockets a poll is the safest way.
The second recommedation which is closely related to the first is to have process1() be able to post some sort of progress. Even if it's just an estimate your front end can then display "10% done", "20% done" and so on. Again, the last thing you want is for your web page to look like it is hung or dead.
Re: Servlet request time out error
Quote:
Originally Posted by
stdunbar
So effectively you've got an asynchronous process but you're trying to treat it as a synchronous process.
My recommendation would be two fold. First, no user wants to wait 4 minutes for a result in a web environment. You're going to have users reloading and/or giving up and sending in bug reports. So, at the very least, send the request off from the browser to start the process and then, via JavaScript, poll the back end to see if the process is done. You only have to poll like every 10 seconds or so which isn't very expensive. Of course, that depends on many factors but without support of something like WebSockets a poll is the safest way.
The second recommedation which is closely related to the first is to have process1() be able to post some sort of progress. Even if it's just an estimate your front end can then display "10% done", "20% done" and so on. Again, the last thing you want is for your web page to look like it is hung or dead.
hello stdunbar
First of thanks for the reply...
As per your first recommendation , you will be said that divide the whole process in two request. am i right? and what is means of the poll ? i did not fully understand your first recommendation.
and as per the second recommendation , you said that whole operation process should be in some progress.
is there any available api or any code to achieve the progressive operation ?
Hello can you suggest me any api or technical solution which is helpful me for my case?
Thanks & Regards
Yatin Baraiya
Re: Servlet request time out error
Sorry if it wasn't clear. Let's try it again.
In your Servlet you likely have at least a doGet() method - I'm going to guess that that is what you are showing. You need to break up the work into two different parts. Something about like:
Code :
public class ProcessService extends HttpServlet {
// in this example the Result class holds whatever the result of your compuation is
private static HashMap<String, Result> results = new HashMap<String, Result>();
public void doGet(HttpServletRequest request, HttpServletResponse response) {
String action = request.getParameter( "action" );
if( action.equals( "processData" ) )
processDataAndReturn( request, response );
else if( action.equals( "getResults" ) )
getResults( request, response );
}
private String processDataAndReturn(HttpServletRequest request, HttpServletResponse response) throws Exception {
ComputationThread compThread = new ComputationThread( request.getSession().getId() );
compThread.start();
response.sendRedirect( "inProgress.jsp" );
}
private void getResults( HttpServletRequest request, HttpServletResponse response ) {
Result result = results.get( request.getSession().getId() );
if( result != null ) {
// return result to browser in whatever way you do now.
}
}
private static class ComputationThread extends Thread {
private String sessionId;
public ComputationThread( String id ) {
sessionId = id;
}
@Override
public void run() {
Process1();
Result result = Process2();
results.put( sessionId, result );
}
}
}
Now you have the framework to handle multiple simultaneous requests. The request from the user to start the process would be to a URL something like:
Code :
http://your.host.name/compute?action=processData
Then, in a JSP that shows the end user that the system is in the process of working (I called it inProgress.jsp above) you'd have some code that does:
Code :
function doPoll(){
$.get('compute?action=getResults', function(data) {
alert(data); // process results here, may be empty
setTimeout(doPoll,5000);
});
}
This snippet of code tries to get results every 5 seconds - it polls every 5 seconds. If there are none available you would have to handle that and likely do nothing. If there are results then you show them the way you do now.
This isn't too bad to implement but there are a few moving parts. I've also left out error checking and clean up (what if a computation is started but then the user closes their browser? The result is taking memory for nothing).
Does this make any more sense?
Re: Servlet request time out error
Yes sure,Now i am going to implement your way, Thanks stdunbar for describe very clearly. :o